ipc/chromium/src/base/condition_variable.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial