hotspot/src/share/vm/runtime/task.cpp
changeset 29321 b7582a690cb9
parent 25468 5331df506290
child 30608 d79880a5cf2f
equal deleted inserted replaced
29320:d4bd9341ded3 29321:b7582a690cb9
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    45   }
    45   }
    46 }
    46 }
    47 #endif
    47 #endif
    48 
    48 
    49 void PeriodicTask::real_time_tick(int delay_time) {
    49 void PeriodicTask::real_time_tick(int delay_time) {
       
    50   assert(Thread::current()->is_Watcher_thread(), "must be WatcherThread");
       
    51 
    50 #ifndef PRODUCT
    52 #ifndef PRODUCT
    51   if (ProfilerCheckIntervals) {
    53   if (ProfilerCheckIntervals) {
    52     _ticks++;
    54     _ticks++;
    53     _timer.stop();
    55     _timer.stop();
    54     int ms = (int)(_timer.seconds() * 1000.0);
    56     int ms = (int)(_timer.seconds() * 1000.0);
    58     _intervalHistogram[ms]++;
    60     _intervalHistogram[ms]++;
    59   }
    61   }
    60 #endif
    62 #endif
    61 
    63 
    62   {
    64   {
       
    65     // The WatcherThread does not participate in the safepoint protocol
       
    66     // for the PeriodicTask_lock because it is not a JavaThread.
    63     MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
    67     MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
    64     int orig_num_tasks = _num_tasks;
    68     int orig_num_tasks = _num_tasks;
    65 
    69 
    66     for(int index = 0; index < _num_tasks; index++) {
    70     for(int index = 0; index < _num_tasks; index++) {
    67       _tasks[index]->execute_if_pending(delay_time);
    71       _tasks[index]->execute_if_pending(delay_time);
    72     }
    76     }
    73   }
    77   }
    74 }
    78 }
    75 
    79 
    76 int PeriodicTask::time_to_wait() {
    80 int PeriodicTask::time_to_wait() {
    77   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
    81   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
    78                      NULL : PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
       
    79 
    82 
    80   if (_num_tasks == 0) {
    83   if (_num_tasks == 0) {
    81     return 0; // sleep until shutdown or a task is enrolled
    84     return 0; // sleep until shutdown or a task is enrolled
    82   }
    85   }
    83 
    86 
    96          _interval %  PeriodicTask::interval_gran == 0,
    99          _interval %  PeriodicTask::interval_gran == 0,
    97               "improper PeriodicTask interval time");
   100               "improper PeriodicTask interval time");
    98 }
   101 }
    99 
   102 
   100 PeriodicTask::~PeriodicTask() {
   103 PeriodicTask::~PeriodicTask() {
       
   104   // This PeriodicTask may have already been disenrolled by a call
       
   105   // to disenroll() before the PeriodicTask was deleted.
   101   disenroll();
   106   disenroll();
   102 }
   107 }
   103 
   108 
   104 /* enroll could be called from a JavaThread, so we have to check for
   109 // enroll the current PeriodicTask
   105  * safepoint when taking the lock to avoid deadlocking */
       
   106 void PeriodicTask::enroll() {
   110 void PeriodicTask::enroll() {
   107   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
   111   // Follow normal safepoint aware lock enter protocol if the caller does
   108                      NULL : PeriodicTask_lock);
   112   // not already own the PeriodicTask_lock. Otherwise, we don't try to
       
   113   // enter it again because VM internal Mutexes do not support recursion.
       
   114   //
       
   115   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL
       
   116                                                       : PeriodicTask_lock);
   109 
   117 
   110   if (_num_tasks == PeriodicTask::max_tasks) {
   118   if (_num_tasks == PeriodicTask::max_tasks) {
   111     fatal("Overflow in PeriodicTask table");
   119     fatal("Overflow in PeriodicTask table");
   112   }
   120   }
   113   _tasks[_num_tasks++] = this;
   121   _tasks[_num_tasks++] = this;
   114 
   122 
   115   WatcherThread* thread = WatcherThread::watcher_thread();
   123   WatcherThread* thread = WatcherThread::watcher_thread();
   116   if (thread) {
   124   if (thread != NULL) {
   117     thread->unpark();
   125     thread->unpark();
   118   } else {
   126   } else {
   119     WatcherThread::start();
   127     WatcherThread::start();
   120   }
   128   }
   121 }
   129 }
   122 
   130 
   123 /* disenroll could be called from a JavaThread, so we have to check for
   131 // disenroll the current PeriodicTask
   124  * safepoint when taking the lock to avoid deadlocking */
       
   125 void PeriodicTask::disenroll() {
   132 void PeriodicTask::disenroll() {
   126   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ?
   133   // Follow normal safepoint aware lock enter protocol if the caller does
   127                      NULL : PeriodicTask_lock);
   134   // not already own the PeriodicTask_lock. Otherwise, we don't try to
       
   135   // enter it again because VM internal Mutexes do not support recursion.
       
   136   //
       
   137   MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL
       
   138                                                       : PeriodicTask_lock);
   128 
   139 
   129   int index;
   140   int index;
   130   for(index = 0; index < _num_tasks && _tasks[index] != this; index++)
   141   for(index = 0; index < _num_tasks && _tasks[index] != this; index++)
   131     ;
   142     ;
   132 
   143