condition_variable.h (4644B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 // ConditionVariable wraps pthreads condition variable synchronization or, on 8 // Windows, simulates it. This functionality is very helpful for having 9 // several threads wait for an event, as is common with a managed thread pool. 10 // The meaning of such an event in the (worker) thread pool scenario is that 11 // additional tasks are now available for processing. It is used in Chrome in 12 // the DNS prefetching system to notify worker threads that a queue now has 13 // items (tasks) which need to be tended to. A related use would have a pool 14 // manager waiting on a ConditionVariable, waiting for a thread in the pool to 15 // announce (signal) that there is now more room in a (bounded size) 16 // communications queue for the manager to deposit tasks, or, as a second 17 // example, that the queue of tasks is completely empty and all workers are 18 // waiting. 19 // 20 // USAGE NOTE 1: spurious signal events are possible with this and 21 // most implementations of condition variables. As a result, be 22 // *sure* to retest your condition before proceeding. The following 23 // is a good example of doing this correctly: 24 // 25 // while (!work_to_be_done()) Wait(...); 26 // 27 // In contrast do NOT do the following: 28 // 29 // if (!work_to_be_done()) Wait(...); // Don't do this. 30 // 31 // Especially avoid the above if you are relying on some other thread only 32 // issuing a signal up *if* there is work-to-do. There can/will 33 // be spurious signals. Recheck state on waiting thread before 34 // assuming the signal was intentional. Caveat caller ;-). 35 // 36 // USAGE NOTE 2: Broadcast() frees up all waiting threads at once, 37 // which leads to contention for the locks they all held when they 38 // called Wait(). This results in POOR performance. A much better 39 // approach to getting a lot of threads out of Wait() is to have each 40 // thread (upon exiting Wait()) call Signal() to free up another 41 // Wait'ing thread. Look at condition_variable_unittest.cc for 42 // both examples. 43 // 44 // Broadcast() can be used nicely during teardown, as it gets the job 45 // done, and leaves no sleeping threads... and performance is less 46 // critical at that point. 47 // 48 // The semantics of Broadcast() are carefully crafted so that *all* 49 // threads that were waiting when the request was made will indeed 50 // get signaled. Some implementations mess up, and don't signal them 51 // all, while others allow the wait to be effectively turned off (for 52 // a while while waiting threads come around). This implementation 53 // appears correct, as it will not "lose" any signals, and will guarantee 54 // that all threads get signaled by Broadcast(). 55 // 56 // This implementation offers support for "performance" in its selection of 57 // which thread to revive. Performance, in direct contrast with "fairness," 58 // assures that the thread that most recently began to Wait() is selected by 59 // Signal to revive. Fairness would (if publicly supported) assure that the 60 // thread that has Wait()ed the longest is selected. The default policy 61 // may improve performance, as the selected thread may have a greater chance of 62 // having some of its stack data in various CPU caches. 63 // 64 // For a discussion of the many very subtle implementation details, see the FAQ 65 // at the end of condition_variable_win.cc. 66 67 #ifndef BASE_CONDITION_VARIABLE_H_ 68 #define BASE_CONDITION_VARIABLE_H_ 69 70 #include "base/basictypes.h" 71 #include "base/lock.h" 72 73 #if defined(XP_UNIX) 74 # include <pthread.h> 75 #endif 76 77 #if defined(XP_WIN) 78 # include <windows.h> 79 #endif 80 81 namespace base { 82 class TimeDelta; 83 } 84 85 class ConditionVariable { 86 public: 87 // Construct a cv for use with ONLY one user lock. 88 explicit ConditionVariable(Lock* user_lock); 89 90 ~ConditionVariable(); 91 92 // Wait() releases the caller's critical section atomically as it starts to 93 // sleep, and the reacquires it when it is signaled. The wait functions are 94 // susceptible to spurious wakeups. (See usage note 1 for more details.) 95 void Wait(); 96 void TimedWait(const base::TimeDelta& max_time); 97 98 // Broadcast() revives all waiting threads. (See usage note 2 for more 99 // details.) 100 void Broadcast(); 101 // Signal() revives one waiting thread. 102 void Signal(); 103 104 private: 105 #if defined(XP_WIN) 106 CONDITION_VARIABLE cv_; 107 SRWLOCK* const srwlock_; 108 #else 109 pthread_cond_t condition_; 110 pthread_mutex_t* user_mutex_; 111 #endif 112 113 DISALLOW_COPY_AND_ASSIGN(ConditionVariable); 114 }; 115 116 #endif // BASE_CONDITION_VARIABLE_H_