| 1 | /* |
| 2 | * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "precompiled.hpp" |
| 26 | #include "memory/allocation.hpp" |
| 27 | #include "runtime/init.hpp" |
| 28 | #include "runtime/task.hpp" |
| 29 | #include "runtime/thread.inline.hpp" |
| 30 | #include "runtime/timer.hpp" |
| 31 | |
| 32 | int PeriodicTask::_num_tasks = 0; |
| 33 | PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks]; |
| 34 | |
| 35 | void PeriodicTask::real_time_tick(int delay_time) { |
| 36 | assert(Thread::current()->is_Watcher_thread(), "must be WatcherThread" ); |
| 37 | |
| 38 | // The WatcherThread does not participate in the safepoint protocol |
| 39 | // for the PeriodicTask_lock because it is not a JavaThread. |
| 40 | MutexLocker ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); |
| 41 | int orig_num_tasks = _num_tasks; |
| 42 | |
| 43 | for(int index = 0; index < _num_tasks; index++) { |
| 44 | _tasks[index]->execute_if_pending(delay_time); |
| 45 | if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself |
| 46 | index--; // re-do current slot as it has changed |
| 47 | orig_num_tasks = _num_tasks; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int PeriodicTask::time_to_wait() { |
| 53 | assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required" ); |
| 54 | |
| 55 | if (_num_tasks == 0) { |
| 56 | return 0; // sleep until shutdown or a task is enrolled |
| 57 | } |
| 58 | |
| 59 | int delay = _tasks[0]->time_to_next_interval(); |
| 60 | for (int index = 1; index < _num_tasks; index++) { |
| 61 | delay = MIN2(delay, _tasks[index]->time_to_next_interval()); |
| 62 | } |
| 63 | return delay; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | PeriodicTask::PeriodicTask(size_t interval_time) : |
| 68 | _counter(0), _interval((int) interval_time) { |
| 69 | // Sanity check the interval time |
| 70 | assert(_interval >= PeriodicTask::min_interval && |
| 71 | _interval % PeriodicTask::interval_gran == 0, |
| 72 | "improper PeriodicTask interval time" ); |
| 73 | } |
| 74 | |
| 75 | PeriodicTask::~PeriodicTask() { |
| 76 | // This PeriodicTask may have already been disenrolled by a call |
| 77 | // to disenroll() before the PeriodicTask was deleted. |
| 78 | disenroll(); |
| 79 | } |
| 80 | |
| 81 | // enroll the current PeriodicTask |
| 82 | void PeriodicTask::enroll() { |
| 83 | // Follow normal safepoint aware lock enter protocol if the caller does |
| 84 | // not already own the PeriodicTask_lock. Otherwise, we don't try to |
| 85 | // enter it again because VM internal Mutexes do not support recursion. |
| 86 | // |
| 87 | MutexLocker ml(PeriodicTask_lock->owned_by_self() ? NULL : PeriodicTask_lock); |
| 88 | |
| 89 | if (_num_tasks == PeriodicTask::max_tasks) { |
| 90 | fatal("Overflow in PeriodicTask table" ); |
| 91 | } else { |
| 92 | _tasks[_num_tasks++] = this; |
| 93 | } |
| 94 | |
| 95 | WatcherThread* thread = WatcherThread::watcher_thread(); |
| 96 | if (thread != NULL) { |
| 97 | thread->unpark(); |
| 98 | } else { |
| 99 | WatcherThread::start(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // disenroll the current PeriodicTask |
| 104 | void PeriodicTask::disenroll() { |
| 105 | // Follow normal safepoint aware lock enter protocol if the caller does |
| 106 | // not already own the PeriodicTask_lock. Otherwise, we don't try to |
| 107 | // enter it again because VM internal Mutexes do not support recursion. |
| 108 | // |
| 109 | MutexLocker ml(PeriodicTask_lock->owned_by_self() ? NULL : PeriodicTask_lock); |
| 110 | |
| 111 | int index; |
| 112 | for(index = 0; index < _num_tasks && _tasks[index] != this; index++) |
| 113 | ; |
| 114 | |
| 115 | if (index == _num_tasks) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | _num_tasks--; |
| 120 | |
| 121 | for (; index < _num_tasks; index++) { |
| 122 | _tasks[index] = _tasks[index+1]; |
| 123 | } |
| 124 | } |
| 125 | |