brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 5 | // ConditionVariable wraps pthreads condition variable synchronization or, on |
| 6 | // Windows, simulates it. This functionality is very helpful for having |
| 7 | // several threads wait for an event, as is common with a thread pool managed |
| 8 | // by a master. The meaning of such an event in the (worker) thread pool |
| 9 | // scenario is that additional tasks are now available for processing. It is |
| 10 | // used in Chrome in the DNS prefetching system to notify worker threads that |
| 11 | // a queue now has items (tasks) which need to be tended to. A related use |
| 12 | // would have a pool manager waiting on a ConditionVariable, waiting for a |
| 13 | // thread in the pool to announce (signal) that there is now more room in a |
| 14 | // (bounded size) communications queue for the manager to deposit tasks, or, |
| 15 | // as a second example, that the queue of tasks is completely empty and all |
| 16 | // workers are waiting. |
| 17 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 18 | // USAGE NOTE 1: spurious signal events are possible with this and |
| 19 | // most implementations of condition variables. As a result, be |
| 20 | // *sure* to retest your condition before proceeding. The following |
| 21 | // is a good example of doing this correctly: |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 22 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 23 | // while (!work_to_be_done()) Wait(...); |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 24 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 25 | // In contrast do NOT do the following: |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 26 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 27 | // if (!work_to_be_done()) Wait(...); // Don't do this. |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 28 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 29 | // Especially avoid the above if you are relying on some other thread only |
| 30 | // issuing a signal up *if* there is work-to-do. There can/will |
| 31 | // be spurious signals. Recheck state on waiting thread before |
| 32 | // assuming the signal was intentional. Caveat caller ;-). |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 33 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 34 | // USAGE NOTE 2: Broadcast() frees up all waiting threads at once, |
| 35 | // which leads to contention for the locks they all held when they |
| 36 | // called Wait(). This results in POOR performance. A much better |
| 37 | // approach to getting a lot of threads out of Wait() is to have each |
| 38 | // thread (upon exiting Wait()) call Signal() to free up another |
| 39 | // Wait'ing thread. Look at condition_variable_unittest.cc for |
| 40 | // both examples. |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 41 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 42 | // Broadcast() can be used nicely during teardown, as it gets the job |
| 43 | // done, and leaves no sleeping threads... and performance is less |
| 44 | // critical at that point. |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 45 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 46 | // The semantics of Broadcast() are carefully crafted so that *all* |
| 47 | // threads that were waiting when the request was made will indeed |
| 48 | // get signaled. Some implementations mess up, and don't signal them |
| 49 | // all, while others allow the wait to be effectively turned off (for |
ericroman@google.com | dbff4f5 | 2008-08-19 01:00:38 +0900 | [diff] [blame] | 50 | // a while while waiting threads come around). This implementation |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 51 | // appears correct, as it will not "lose" any signals, and will guarantee |
| 52 | // that all threads get signaled by Broadcast(). |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 53 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 54 | // This implementation offers support for "performance" in its selection of |
| 55 | // which thread to revive. Performance, in direct contrast with "fairness," |
| 56 | // assures that the thread that most recently began to Wait() is selected by |
| 57 | // Signal to revive. Fairness would (if publicly supported) assure that the |
| 58 | // thread that has Wait()ed the longest is selected. The default policy |
| 59 | // may improve performance, as the selected thread may have a greater chance of |
| 60 | // having some of its stack data in various CPU caches. |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 61 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 62 | // For a discussion of the many very subtle implementation details, see the FAQ |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 63 | // at the end of condition_variable_win.cc. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 64 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 65 | #ifndef BASE_SYNCHRONIZATION_CONDITION_VARIABLE_H_ |
| 66 | #define BASE_SYNCHRONIZATION_CONDITION_VARIABLE_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 67 | #pragma once |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 68 | |
phajdan.jr@chromium.org | 6dce3c2 | 2009-06-04 00:01:29 +0900 | [diff] [blame] | 69 | #include "build/build_config.h" |
| 70 | |
| 71 | #if defined(OS_WIN) |
| 72 | #include <windows.h> |
| 73 | #elif defined(OS_POSIX) |
| 74 | #include <pthread.h> |
| 75 | #endif |
| 76 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 77 | #include "base/base_export.h" |
phajdan.jr@chromium.org | 6dce3c2 | 2009-06-04 00:01:29 +0900 | [diff] [blame] | 78 | #include "base/basictypes.h" |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 79 | #include "base/synchronization/lock.h" |
thestig@chromium.org | 370154d | 2009-03-17 05:02:42 +0900 | [diff] [blame] | 80 | |
| 81 | namespace base { |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 82 | |
| 83 | class TimeDelta; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 84 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 85 | class BASE_EXPORT ConditionVariable { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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. |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 94 | void Wait(); |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 95 | void TimedWait(const TimeDelta& max_time); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 96 | |
| 97 | // Broadcast() revives all waiting threads. |
| 98 | void Broadcast(); |
| 99 | // Signal() revives one waiting thread. |
| 100 | void Signal(); |
| 101 | |
| 102 | private: |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 103 | |
| 104 | #if defined(OS_WIN) |
| 105 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 106 | // Define Event class that is used to form circularly linked lists. |
| 107 | // The list container is an element with NULL as its handle_ value. |
| 108 | // The actual list elements have a non-zero handle_ value. |
| 109 | // All calls to methods MUST be done under protection of a lock so that links |
| 110 | // can be validated. Without the lock, some links might asynchronously |
| 111 | // change, and the assertions would fail (as would list change operations). |
| 112 | class Event { |
| 113 | public: |
| 114 | // Default constructor with no arguments creates a list container. |
| 115 | Event(); |
| 116 | ~Event(); |
| 117 | |
| 118 | // InitListElement transitions an instance from a container, to an element. |
| 119 | void InitListElement(); |
| 120 | |
| 121 | // Methods for use on lists. |
| 122 | bool IsEmpty() const; |
| 123 | void PushBack(Event* other); |
| 124 | Event* PopFront(); |
| 125 | Event* PopBack(); |
| 126 | |
| 127 | // Methods for use on list elements. |
| 128 | // Accessor method. |
| 129 | HANDLE handle() const; |
| 130 | // Pull an element from a list (if it's in one). |
| 131 | Event* Extract(); |
| 132 | |
| 133 | // Method for use on a list element or on a list. |
| 134 | bool IsSingleton() const; |
| 135 | |
| 136 | private: |
| 137 | // Provide pre/post conditions to validate correct manipulations. |
| 138 | bool ValidateAsDistinct(Event* other) const; |
| 139 | bool ValidateAsItem() const; |
| 140 | bool ValidateAsList() const; |
| 141 | bool ValidateLinks() const; |
| 142 | |
| 143 | HANDLE handle_; |
| 144 | Event* next_; |
| 145 | Event* prev_; |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 146 | DISALLOW_COPY_AND_ASSIGN(Event); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | // Note that RUNNING is an unlikely number to have in RAM by accident. |
| 150 | // This helps with defensive destructor coding in the face of user error. |
| 151 | enum RunState { SHUTDOWN = 0, RUNNING = 64213 }; |
| 152 | |
| 153 | // Internal implementation methods supporting Wait(). |
| 154 | Event* GetEventForWaiting(); |
| 155 | void RecycleEvent(Event* used_event); |
| 156 | |
| 157 | RunState run_state_; |
| 158 | |
| 159 | // Private critical section for access to member data. |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 160 | base::Lock internal_lock_; |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 161 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 162 | // Lock that is acquired before calling Wait(). |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 163 | base::Lock& user_lock_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 164 | |
| 165 | // Events that threads are blocked on. |
| 166 | Event waiting_list_; |
| 167 | |
| 168 | // Free list for old events. |
| 169 | Event recycling_list_; |
| 170 | int recycling_list_size_; |
| 171 | |
| 172 | // The number of allocated, but not yet deleted events. |
| 173 | int allocation_counter_; |
| 174 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 175 | #elif defined(OS_POSIX) |
| 176 | |
| 177 | pthread_cond_t condition_; |
| 178 | pthread_mutex_t* user_mutex_; |
rdsmith@google.com | 356a63c | 2010-06-16 21:09:48 +0900 | [diff] [blame] | 179 | #if !defined(NDEBUG) |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 180 | base::Lock* user_lock_; // Needed to adjust shadow lock state on wait. |
rdsmith@google.com | 356a63c | 2010-06-16 21:09:48 +0900 | [diff] [blame] | 181 | #endif |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 182 | |
| 183 | #endif |
| 184 | |
| 185 | DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 186 | }; |
| 187 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 188 | } // namespace base |
| 189 | |
| 190 | #endif // BASE_SYNCHRONIZATION_CONDITION_VARIABLE_H_ |