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/mutexLocker.inline.hpp" 29 #include "runtime/task.hpp" 30 #include "runtime/thread.inline.hpp" 31 #include "runtime/timer.hpp" 32 33 int PeriodicTask::_num_tasks = 0; 34 PeriodicTask* PeriodicTask::_tasks[PeriodicTask::max_tasks]; 35 36 void PeriodicTask::real_time_tick(int delay_time) { 37 assert(Thread::current()->is_Watcher_thread(), "must be WatcherThread"); 38 39 // The WatcherThread does not participate in the safepoint protocol 40 // for the PeriodicTask_lock because it is not a JavaThread. 41 MutexLocker ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); 42 int orig_num_tasks = _num_tasks; 43 44 for(int index = 0; index < _num_tasks; index++) { 45 _tasks[index]->execute_if_pending(delay_time); 46 if (_num_tasks < orig_num_tasks) { // task dis-enrolled itself 47 index--; // re-do current slot as it has changed 48 orig_num_tasks = _num_tasks; 49 } 50 } 51 } 52 53 int PeriodicTask::time_to_wait() { 54 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); 55 56 if (_num_tasks == 0) { 57 return 0; // sleep until shutdown or a task is enrolled 58 } 59 60 int delay = _tasks[0]->time_to_next_interval(); 61 for (int index = 1; index < _num_tasks; index++) { 62 delay = MIN2(delay, _tasks[index]->time_to_next_interval()); 63 } 64 return delay; 65 } 66 67 68 PeriodicTask::PeriodicTask(size_t interval_time) : 69 _counter(0), _interval((int) interval_time) { 70 // Sanity check the interval time 71 assert(_interval >= PeriodicTask::min_interval && 72 _interval % PeriodicTask::interval_gran == 0, 73 "improper PeriodicTask interval time"); 74 } 75 76 PeriodicTask::~PeriodicTask() { 77 // This PeriodicTask may have already been disenrolled by a call 78 // to disenroll() before the PeriodicTask was deleted. 79 disenroll(); 80 } 81 82 // enroll the current PeriodicTask 83 void PeriodicTask::enroll() { 84 // Follow normal safepoint aware lock enter protocol if the caller does 85 // not already own the PeriodicTask_lock. Otherwise, we don't try to 86 // enter it again because VM internal Mutexes do not support recursion. 87 // 88 MutexLocker ml(PeriodicTask_lock->owned_by_self() ? NULL : PeriodicTask_lock); 89 90 if (_num_tasks == PeriodicTask::max_tasks) { 91 fatal("Overflow in PeriodicTask table"); 92 } else { 93 _tasks[_num_tasks++] = this; 94 } 95 96 WatcherThread* thread = WatcherThread::watcher_thread(); 97 if (thread != NULL) { 98 thread->unpark(); 99 } else { 100 WatcherThread::start(); 101 } 102 } 103 104 // disenroll the current PeriodicTask 105 void PeriodicTask::disenroll() { 106 // Follow normal safepoint aware lock enter protocol if the caller does 107 // not already own the PeriodicTask_lock. Otherwise, we don't try to 108 // enter it again because VM internal Mutexes do not support recursion. 109 // 110 MutexLocker ml(PeriodicTask_lock->owned_by_self() ? NULL : PeriodicTask_lock); 111 112 int index; 113 for(index = 0; index < _num_tasks && _tasks[index] != this; index++) 114 ; 115 116 if (index == _num_tasks) { 117 return; 118 } 119 120 _num_tasks--; 121 122 for (; index < _num_tasks; index++) { 123 _tasks[index] = _tasks[index+1]; 124 } 125 }