|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
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 // |
|
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: |
|
22 // |
|
23 // while (!work_to_be_done()) Wait(...); |
|
24 // |
|
25 // In contrast do NOT do the following: |
|
26 // |
|
27 // if (!work_to_be_done()) Wait(...); // Don't do this. |
|
28 // |
|
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 ;-). |
|
33 // |
|
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. |
|
41 // |
|
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. |
|
45 // |
|
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 |
|
50 // a while while waiting threads come around). This implementation |
|
51 // appears correct, as it will not "lose" any signals, and will guarantee |
|
52 // that all threads get signaled by Broadcast(). |
|
53 // |
|
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. |
|
61 // |
|
62 // For a discussion of the many very subtle implementation details, see the FAQ |
|
63 // at the end of condition_variable_win.cc. |
|
64 |
|
65 #ifndef BASE_CONDITION_VARIABLE_H_ |
|
66 #define BASE_CONDITION_VARIABLE_H_ |
|
67 |
|
68 #include "base/lock.h" |
|
69 |
|
70 namespace base { |
|
71 class TimeDelta; |
|
72 } |
|
73 |
|
74 class ConditionVariable { |
|
75 public: |
|
76 // Construct a cv for use with ONLY one user lock. |
|
77 explicit ConditionVariable(Lock* user_lock); |
|
78 |
|
79 ~ConditionVariable(); |
|
80 |
|
81 // Wait() releases the caller's critical section atomically as it starts to |
|
82 // sleep, and the reacquires it when it is signaled. |
|
83 void Wait(); |
|
84 void TimedWait(const base::TimeDelta& max_time); |
|
85 |
|
86 // Broadcast() revives all waiting threads. |
|
87 void Broadcast(); |
|
88 // Signal() revives one waiting thread. |
|
89 void Signal(); |
|
90 |
|
91 private: |
|
92 |
|
93 #if defined(OS_WIN) |
|
94 |
|
95 // Define Event class that is used to form circularly linked lists. |
|
96 // The list container is an element with NULL as its handle_ value. |
|
97 // The actual list elements have a non-zero handle_ value. |
|
98 // All calls to methods MUST be done under protection of a lock so that links |
|
99 // can be validated. Without the lock, some links might asynchronously |
|
100 // change, and the assertions would fail (as would list change operations). |
|
101 class Event { |
|
102 public: |
|
103 // Default constructor with no arguments creates a list container. |
|
104 Event(); |
|
105 ~Event(); |
|
106 |
|
107 // InitListElement transitions an instance from a container, to an element. |
|
108 void InitListElement(); |
|
109 |
|
110 // Methods for use on lists. |
|
111 bool IsEmpty() const; |
|
112 void PushBack(Event* other); |
|
113 Event* PopFront(); |
|
114 Event* PopBack(); |
|
115 |
|
116 // Methods for use on list elements. |
|
117 // Accessor method. |
|
118 HANDLE handle() const; |
|
119 // Pull an element from a list (if it's in one). |
|
120 Event* Extract(); |
|
121 |
|
122 // Method for use on a list element or on a list. |
|
123 bool IsSingleton() const; |
|
124 |
|
125 private: |
|
126 // Provide pre/post conditions to validate correct manipulations. |
|
127 bool ValidateAsDistinct(Event* other) const; |
|
128 bool ValidateAsItem() const; |
|
129 bool ValidateAsList() const; |
|
130 bool ValidateLinks() const; |
|
131 |
|
132 HANDLE handle_; |
|
133 Event* next_; |
|
134 Event* prev_; |
|
135 DISALLOW_COPY_AND_ASSIGN(Event); |
|
136 }; |
|
137 |
|
138 // Note that RUNNING is an unlikely number to have in RAM by accident. |
|
139 // This helps with defensive destructor coding in the face of user error. |
|
140 enum RunState { SHUTDOWN = 0, RUNNING = 64213 }; |
|
141 |
|
142 // Internal implementation methods supporting Wait(). |
|
143 Event* GetEventForWaiting(); |
|
144 void RecycleEvent(Event* used_event); |
|
145 |
|
146 RunState run_state_; |
|
147 |
|
148 // Private critical section for access to member data. |
|
149 Lock internal_lock_; |
|
150 |
|
151 // Lock that is acquired before calling Wait(). |
|
152 Lock& user_lock_; |
|
153 |
|
154 // Events that threads are blocked on. |
|
155 Event waiting_list_; |
|
156 |
|
157 // Free list for old events. |
|
158 Event recycling_list_; |
|
159 int recycling_list_size_; |
|
160 |
|
161 // The number of allocated, but not yet deleted events. |
|
162 int allocation_counter_; |
|
163 |
|
164 #elif defined(OS_POSIX) |
|
165 |
|
166 pthread_cond_t condition_; |
|
167 pthread_mutex_t* user_mutex_; |
|
168 |
|
169 #endif |
|
170 |
|
171 DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
|
172 }; |
|
173 |
|
174 #endif // BASE_CONDITION_VARIABLE_H_ |