xref: /aosp_15_r20/external/libchrome/base/synchronization/waitable_event_posix.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <algorithm>
8*635a8641SAndroid Build Coastguard Worker #include <limits>
9*635a8641SAndroid Build Coastguard Worker #include <vector>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include "base/debug/activity_tracker.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/condition_variable.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/lock.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/waitable_event.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/threading/scoped_blocking_call.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h"
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
20*635a8641SAndroid Build Coastguard Worker // A WaitableEvent on POSIX is implemented as a wait-list. Currently we don't
21*635a8641SAndroid Build Coastguard Worker // support cross-process events (where one process can signal an event which
22*635a8641SAndroid Build Coastguard Worker // others are waiting on). Because of this, we can avoid having one thread per
23*635a8641SAndroid Build Coastguard Worker // listener in several cases.
24*635a8641SAndroid Build Coastguard Worker //
25*635a8641SAndroid Build Coastguard Worker // The WaitableEvent maintains a list of waiters, protected by a lock. Each
26*635a8641SAndroid Build Coastguard Worker // waiter is either an async wait, in which case we have a Task and the
27*635a8641SAndroid Build Coastguard Worker // MessageLoop to run it on, or a blocking wait, in which case we have the
28*635a8641SAndroid Build Coastguard Worker // condition variable to signal.
29*635a8641SAndroid Build Coastguard Worker //
30*635a8641SAndroid Build Coastguard Worker // Waiting involves grabbing the lock and adding oneself to the wait list. Async
31*635a8641SAndroid Build Coastguard Worker // waits can be canceled, which means grabbing the lock and removing oneself
32*635a8641SAndroid Build Coastguard Worker // from the list.
33*635a8641SAndroid Build Coastguard Worker //
34*635a8641SAndroid Build Coastguard Worker // Waiting on multiple events is handled by adding a single, synchronous wait to
35*635a8641SAndroid Build Coastguard Worker // the wait-list of many events. An event passes a pointer to itself when
36*635a8641SAndroid Build Coastguard Worker // firing a waiter and so we can store that pointer to find out which event
37*635a8641SAndroid Build Coastguard Worker // triggered.
38*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker namespace base {
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
43*635a8641SAndroid Build Coastguard Worker // This is just an abstract base class for waking the two types of waiters
44*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
WaitableEvent(ResetPolicy reset_policy,InitialState initial_state)45*635a8641SAndroid Build Coastguard Worker WaitableEvent::WaitableEvent(ResetPolicy reset_policy,
46*635a8641SAndroid Build Coastguard Worker                              InitialState initial_state)
47*635a8641SAndroid Build Coastguard Worker     : kernel_(new WaitableEventKernel(reset_policy, initial_state)) {}
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker WaitableEvent::~WaitableEvent() = default;
50*635a8641SAndroid Build Coastguard Worker 
Reset()51*635a8641SAndroid Build Coastguard Worker void WaitableEvent::Reset() {
52*635a8641SAndroid Build Coastguard Worker   base::AutoLock locked(kernel_->lock_);
53*635a8641SAndroid Build Coastguard Worker   kernel_->signaled_ = false;
54*635a8641SAndroid Build Coastguard Worker }
55*635a8641SAndroid Build Coastguard Worker 
Signal()56*635a8641SAndroid Build Coastguard Worker void WaitableEvent::Signal() {
57*635a8641SAndroid Build Coastguard Worker   base::AutoLock locked(kernel_->lock_);
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker   if (kernel_->signaled_)
60*635a8641SAndroid Build Coastguard Worker     return;
61*635a8641SAndroid Build Coastguard Worker 
62*635a8641SAndroid Build Coastguard Worker   if (kernel_->manual_reset_) {
63*635a8641SAndroid Build Coastguard Worker     SignalAll();
64*635a8641SAndroid Build Coastguard Worker     kernel_->signaled_ = true;
65*635a8641SAndroid Build Coastguard Worker   } else {
66*635a8641SAndroid Build Coastguard Worker     // In the case of auto reset, if no waiters were woken, we remain
67*635a8641SAndroid Build Coastguard Worker     // signaled.
68*635a8641SAndroid Build Coastguard Worker     if (!SignalOne())
69*635a8641SAndroid Build Coastguard Worker       kernel_->signaled_ = true;
70*635a8641SAndroid Build Coastguard Worker   }
71*635a8641SAndroid Build Coastguard Worker }
72*635a8641SAndroid Build Coastguard Worker 
IsSignaled()73*635a8641SAndroid Build Coastguard Worker bool WaitableEvent::IsSignaled() {
74*635a8641SAndroid Build Coastguard Worker   base::AutoLock locked(kernel_->lock_);
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker   const bool result = kernel_->signaled_;
77*635a8641SAndroid Build Coastguard Worker   if (result && !kernel_->manual_reset_)
78*635a8641SAndroid Build Coastguard Worker     kernel_->signaled_ = false;
79*635a8641SAndroid Build Coastguard Worker   return result;
80*635a8641SAndroid Build Coastguard Worker }
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
83*635a8641SAndroid Build Coastguard Worker // Synchronous waits
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
86*635a8641SAndroid Build Coastguard Worker // This is a synchronous waiter. The thread is waiting on the given condition
87*635a8641SAndroid Build Coastguard Worker // variable and the fired flag in this object.
88*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
89*635a8641SAndroid Build Coastguard Worker class SyncWaiter : public WaitableEvent::Waiter {
90*635a8641SAndroid Build Coastguard Worker  public:
SyncWaiter()91*635a8641SAndroid Build Coastguard Worker   SyncWaiter()
92*635a8641SAndroid Build Coastguard Worker       : fired_(false), signaling_event_(nullptr), lock_(), cv_(&lock_) {}
93*635a8641SAndroid Build Coastguard Worker 
Fire(WaitableEvent * signaling_event)94*635a8641SAndroid Build Coastguard Worker   bool Fire(WaitableEvent* signaling_event) override {
95*635a8641SAndroid Build Coastguard Worker     base::AutoLock locked(lock_);
96*635a8641SAndroid Build Coastguard Worker 
97*635a8641SAndroid Build Coastguard Worker     if (fired_)
98*635a8641SAndroid Build Coastguard Worker       return false;
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker     fired_ = true;
101*635a8641SAndroid Build Coastguard Worker     signaling_event_ = signaling_event;
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker     cv_.Broadcast();
104*635a8641SAndroid Build Coastguard Worker 
105*635a8641SAndroid Build Coastguard Worker     // Unlike AsyncWaiter objects, SyncWaiter objects are stack-allocated on
106*635a8641SAndroid Build Coastguard Worker     // the blocking thread's stack.  There is no |delete this;| in Fire.  The
107*635a8641SAndroid Build Coastguard Worker     // SyncWaiter object is destroyed when it goes out of scope.
108*635a8641SAndroid Build Coastguard Worker 
109*635a8641SAndroid Build Coastguard Worker     return true;
110*635a8641SAndroid Build Coastguard Worker   }
111*635a8641SAndroid Build Coastguard Worker 
signaling_event() const112*635a8641SAndroid Build Coastguard Worker   WaitableEvent* signaling_event() const {
113*635a8641SAndroid Build Coastguard Worker     return signaling_event_;
114*635a8641SAndroid Build Coastguard Worker   }
115*635a8641SAndroid Build Coastguard Worker 
116*635a8641SAndroid Build Coastguard Worker   // ---------------------------------------------------------------------------
117*635a8641SAndroid Build Coastguard Worker   // These waiters are always stack allocated and don't delete themselves. Thus
118*635a8641SAndroid Build Coastguard Worker   // there's no problem and the ABA tag is the same as the object pointer.
119*635a8641SAndroid Build Coastguard Worker   // ---------------------------------------------------------------------------
Compare(void * tag)120*635a8641SAndroid Build Coastguard Worker   bool Compare(void* tag) override { return this == tag; }
121*635a8641SAndroid Build Coastguard Worker 
122*635a8641SAndroid Build Coastguard Worker   // ---------------------------------------------------------------------------
123*635a8641SAndroid Build Coastguard Worker   // Called with lock held.
124*635a8641SAndroid Build Coastguard Worker   // ---------------------------------------------------------------------------
fired() const125*635a8641SAndroid Build Coastguard Worker   bool fired() const {
126*635a8641SAndroid Build Coastguard Worker     return fired_;
127*635a8641SAndroid Build Coastguard Worker   }
128*635a8641SAndroid Build Coastguard Worker 
129*635a8641SAndroid Build Coastguard Worker   // ---------------------------------------------------------------------------
130*635a8641SAndroid Build Coastguard Worker   // During a TimedWait, we need a way to make sure that an auto-reset
131*635a8641SAndroid Build Coastguard Worker   // WaitableEvent doesn't think that this event has been signaled between
132*635a8641SAndroid Build Coastguard Worker   // unlocking it and removing it from the wait-list. Called with lock held.
133*635a8641SAndroid Build Coastguard Worker   // ---------------------------------------------------------------------------
Disable()134*635a8641SAndroid Build Coastguard Worker   void Disable() {
135*635a8641SAndroid Build Coastguard Worker     fired_ = true;
136*635a8641SAndroid Build Coastguard Worker   }
137*635a8641SAndroid Build Coastguard Worker 
lock()138*635a8641SAndroid Build Coastguard Worker   base::Lock* lock() {
139*635a8641SAndroid Build Coastguard Worker     return &lock_;
140*635a8641SAndroid Build Coastguard Worker   }
141*635a8641SAndroid Build Coastguard Worker 
cv()142*635a8641SAndroid Build Coastguard Worker   base::ConditionVariable* cv() {
143*635a8641SAndroid Build Coastguard Worker     return &cv_;
144*635a8641SAndroid Build Coastguard Worker   }
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker  private:
147*635a8641SAndroid Build Coastguard Worker   bool fired_;
148*635a8641SAndroid Build Coastguard Worker   WaitableEvent* signaling_event_;  // The WaitableEvent which woke us
149*635a8641SAndroid Build Coastguard Worker   base::Lock lock_;
150*635a8641SAndroid Build Coastguard Worker   base::ConditionVariable cv_;
151*635a8641SAndroid Build Coastguard Worker };
152*635a8641SAndroid Build Coastguard Worker 
Wait()153*635a8641SAndroid Build Coastguard Worker void WaitableEvent::Wait() {
154*635a8641SAndroid Build Coastguard Worker   bool result = TimedWaitUntil(TimeTicks::Max());
155*635a8641SAndroid Build Coastguard Worker   DCHECK(result) << "TimedWait() should never fail with infinite timeout";
156*635a8641SAndroid Build Coastguard Worker }
157*635a8641SAndroid Build Coastguard Worker 
TimedWait(const TimeDelta & wait_delta)158*635a8641SAndroid Build Coastguard Worker bool WaitableEvent::TimedWait(const TimeDelta& wait_delta) {
159*635a8641SAndroid Build Coastguard Worker   // TimeTicks takes care of overflow including the cases when wait_delta
160*635a8641SAndroid Build Coastguard Worker   // is a maximum value.
161*635a8641SAndroid Build Coastguard Worker   return TimedWaitUntil(TimeTicks::Now() + wait_delta);
162*635a8641SAndroid Build Coastguard Worker }
163*635a8641SAndroid Build Coastguard Worker 
TimedWaitUntil(const TimeTicks & end_time)164*635a8641SAndroid Build Coastguard Worker bool WaitableEvent::TimedWaitUntil(const TimeTicks& end_time) {
165*635a8641SAndroid Build Coastguard Worker   internal::AssertBaseSyncPrimitivesAllowed();
166*635a8641SAndroid Build Coastguard Worker   ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK);
167*635a8641SAndroid Build Coastguard Worker   // Record the event that this thread is blocking upon (for hang diagnosis).
168*635a8641SAndroid Build Coastguard Worker   base::debug::ScopedEventWaitActivity event_activity(this);
169*635a8641SAndroid Build Coastguard Worker 
170*635a8641SAndroid Build Coastguard Worker   const bool finite_time = !end_time.is_max();
171*635a8641SAndroid Build Coastguard Worker 
172*635a8641SAndroid Build Coastguard Worker   kernel_->lock_.Acquire();
173*635a8641SAndroid Build Coastguard Worker   if (kernel_->signaled_) {
174*635a8641SAndroid Build Coastguard Worker     if (!kernel_->manual_reset_) {
175*635a8641SAndroid Build Coastguard Worker       // In this case we were signaled when we had no waiters. Now that
176*635a8641SAndroid Build Coastguard Worker       // someone has waited upon us, we can automatically reset.
177*635a8641SAndroid Build Coastguard Worker       kernel_->signaled_ = false;
178*635a8641SAndroid Build Coastguard Worker     }
179*635a8641SAndroid Build Coastguard Worker 
180*635a8641SAndroid Build Coastguard Worker     kernel_->lock_.Release();
181*635a8641SAndroid Build Coastguard Worker     return true;
182*635a8641SAndroid Build Coastguard Worker   }
183*635a8641SAndroid Build Coastguard Worker 
184*635a8641SAndroid Build Coastguard Worker   SyncWaiter sw;
185*635a8641SAndroid Build Coastguard Worker   sw.lock()->Acquire();
186*635a8641SAndroid Build Coastguard Worker 
187*635a8641SAndroid Build Coastguard Worker   Enqueue(&sw);
188*635a8641SAndroid Build Coastguard Worker   kernel_->lock_.Release();
189*635a8641SAndroid Build Coastguard Worker   // We are violating locking order here by holding the SyncWaiter lock but not
190*635a8641SAndroid Build Coastguard Worker   // the WaitableEvent lock. However, this is safe because we don't lock @lock_
191*635a8641SAndroid Build Coastguard Worker   // again before unlocking it.
192*635a8641SAndroid Build Coastguard Worker 
193*635a8641SAndroid Build Coastguard Worker   for (;;) {
194*635a8641SAndroid Build Coastguard Worker     const TimeTicks current_time(TimeTicks::Now());
195*635a8641SAndroid Build Coastguard Worker 
196*635a8641SAndroid Build Coastguard Worker     if (sw.fired() || (finite_time && current_time >= end_time)) {
197*635a8641SAndroid Build Coastguard Worker       const bool return_value = sw.fired();
198*635a8641SAndroid Build Coastguard Worker 
199*635a8641SAndroid Build Coastguard Worker       // We can't acquire @lock_ before releasing the SyncWaiter lock (because
200*635a8641SAndroid Build Coastguard Worker       // of locking order), however, in between the two a signal could be fired
201*635a8641SAndroid Build Coastguard Worker       // and @sw would accept it, however we will still return false, so the
202*635a8641SAndroid Build Coastguard Worker       // signal would be lost on an auto-reset WaitableEvent. Thus we call
203*635a8641SAndroid Build Coastguard Worker       // Disable which makes sw::Fire return false.
204*635a8641SAndroid Build Coastguard Worker       sw.Disable();
205*635a8641SAndroid Build Coastguard Worker       sw.lock()->Release();
206*635a8641SAndroid Build Coastguard Worker 
207*635a8641SAndroid Build Coastguard Worker       // This is a bug that has been enshrined in the interface of
208*635a8641SAndroid Build Coastguard Worker       // WaitableEvent now: |Dequeue| is called even when |sw.fired()| is true,
209*635a8641SAndroid Build Coastguard Worker       // even though it'll always return false in that case. However, taking
210*635a8641SAndroid Build Coastguard Worker       // the lock ensures that |Signal| has completed before we return and
211*635a8641SAndroid Build Coastguard Worker       // means that a WaitableEvent can synchronise its own destruction.
212*635a8641SAndroid Build Coastguard Worker       kernel_->lock_.Acquire();
213*635a8641SAndroid Build Coastguard Worker       kernel_->Dequeue(&sw, &sw);
214*635a8641SAndroid Build Coastguard Worker       kernel_->lock_.Release();
215*635a8641SAndroid Build Coastguard Worker 
216*635a8641SAndroid Build Coastguard Worker       return return_value;
217*635a8641SAndroid Build Coastguard Worker     }
218*635a8641SAndroid Build Coastguard Worker 
219*635a8641SAndroid Build Coastguard Worker     if (finite_time) {
220*635a8641SAndroid Build Coastguard Worker       const TimeDelta max_wait(end_time - current_time);
221*635a8641SAndroid Build Coastguard Worker       sw.cv()->TimedWait(max_wait);
222*635a8641SAndroid Build Coastguard Worker     } else {
223*635a8641SAndroid Build Coastguard Worker       sw.cv()->Wait();
224*635a8641SAndroid Build Coastguard Worker     }
225*635a8641SAndroid Build Coastguard Worker   }
226*635a8641SAndroid Build Coastguard Worker }
227*635a8641SAndroid Build Coastguard Worker 
228*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
229*635a8641SAndroid Build Coastguard Worker // Synchronous waiting on multiple objects.
230*635a8641SAndroid Build Coastguard Worker 
231*635a8641SAndroid Build Coastguard Worker static bool  // StrictWeakOrdering
cmp_fst_addr(const std::pair<WaitableEvent *,unsigned> & a,const std::pair<WaitableEvent *,unsigned> & b)232*635a8641SAndroid Build Coastguard Worker cmp_fst_addr(const std::pair<WaitableEvent*, unsigned> &a,
233*635a8641SAndroid Build Coastguard Worker              const std::pair<WaitableEvent*, unsigned> &b) {
234*635a8641SAndroid Build Coastguard Worker   return a.first < b.first;
235*635a8641SAndroid Build Coastguard Worker }
236*635a8641SAndroid Build Coastguard Worker 
237*635a8641SAndroid Build Coastguard Worker // static
WaitMany(WaitableEvent ** raw_waitables,size_t count)238*635a8641SAndroid Build Coastguard Worker size_t WaitableEvent::WaitMany(WaitableEvent** raw_waitables,
239*635a8641SAndroid Build Coastguard Worker                                size_t count) {
240*635a8641SAndroid Build Coastguard Worker   internal::AssertBaseSyncPrimitivesAllowed();
241*635a8641SAndroid Build Coastguard Worker   DCHECK(count) << "Cannot wait on no events";
242*635a8641SAndroid Build Coastguard Worker   ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK);
243*635a8641SAndroid Build Coastguard Worker   // Record an event (the first) that this thread is blocking upon.
244*635a8641SAndroid Build Coastguard Worker   base::debug::ScopedEventWaitActivity event_activity(raw_waitables[0]);
245*635a8641SAndroid Build Coastguard Worker 
246*635a8641SAndroid Build Coastguard Worker   // We need to acquire the locks in a globally consistent order. Thus we sort
247*635a8641SAndroid Build Coastguard Worker   // the array of waitables by address. We actually sort a pairs so that we can
248*635a8641SAndroid Build Coastguard Worker   // map back to the original index values later.
249*635a8641SAndroid Build Coastguard Worker   std::vector<std::pair<WaitableEvent*, size_t> > waitables;
250*635a8641SAndroid Build Coastguard Worker   waitables.reserve(count);
251*635a8641SAndroid Build Coastguard Worker   for (size_t i = 0; i < count; ++i)
252*635a8641SAndroid Build Coastguard Worker     waitables.push_back(std::make_pair(raw_waitables[i], i));
253*635a8641SAndroid Build Coastguard Worker 
254*635a8641SAndroid Build Coastguard Worker   DCHECK_EQ(count, waitables.size());
255*635a8641SAndroid Build Coastguard Worker 
256*635a8641SAndroid Build Coastguard Worker   sort(waitables.begin(), waitables.end(), cmp_fst_addr);
257*635a8641SAndroid Build Coastguard Worker 
258*635a8641SAndroid Build Coastguard Worker   // The set of waitables must be distinct. Since we have just sorted by
259*635a8641SAndroid Build Coastguard Worker   // address, we can check this cheaply by comparing pairs of consecutive
260*635a8641SAndroid Build Coastguard Worker   // elements.
261*635a8641SAndroid Build Coastguard Worker   for (size_t i = 0; i < waitables.size() - 1; ++i) {
262*635a8641SAndroid Build Coastguard Worker     DCHECK(waitables[i].first != waitables[i+1].first);
263*635a8641SAndroid Build Coastguard Worker   }
264*635a8641SAndroid Build Coastguard Worker 
265*635a8641SAndroid Build Coastguard Worker   SyncWaiter sw;
266*635a8641SAndroid Build Coastguard Worker 
267*635a8641SAndroid Build Coastguard Worker   const size_t r = EnqueueMany(&waitables[0], count, &sw);
268*635a8641SAndroid Build Coastguard Worker   if (r < count) {
269*635a8641SAndroid Build Coastguard Worker     // One of the events is already signaled. The SyncWaiter has not been
270*635a8641SAndroid Build Coastguard Worker     // enqueued anywhere.
271*635a8641SAndroid Build Coastguard Worker     return waitables[r].second;
272*635a8641SAndroid Build Coastguard Worker   }
273*635a8641SAndroid Build Coastguard Worker 
274*635a8641SAndroid Build Coastguard Worker   // At this point, we hold the locks on all the WaitableEvents and we have
275*635a8641SAndroid Build Coastguard Worker   // enqueued our waiter in them all.
276*635a8641SAndroid Build Coastguard Worker   sw.lock()->Acquire();
277*635a8641SAndroid Build Coastguard Worker     // Release the WaitableEvent locks in the reverse order
278*635a8641SAndroid Build Coastguard Worker     for (size_t i = 0; i < count; ++i) {
279*635a8641SAndroid Build Coastguard Worker       waitables[count - (1 + i)].first->kernel_->lock_.Release();
280*635a8641SAndroid Build Coastguard Worker     }
281*635a8641SAndroid Build Coastguard Worker 
282*635a8641SAndroid Build Coastguard Worker     for (;;) {
283*635a8641SAndroid Build Coastguard Worker       if (sw.fired())
284*635a8641SAndroid Build Coastguard Worker         break;
285*635a8641SAndroid Build Coastguard Worker 
286*635a8641SAndroid Build Coastguard Worker       sw.cv()->Wait();
287*635a8641SAndroid Build Coastguard Worker     }
288*635a8641SAndroid Build Coastguard Worker   sw.lock()->Release();
289*635a8641SAndroid Build Coastguard Worker 
290*635a8641SAndroid Build Coastguard Worker   // The address of the WaitableEvent which fired is stored in the SyncWaiter.
291*635a8641SAndroid Build Coastguard Worker   WaitableEvent *const signaled_event = sw.signaling_event();
292*635a8641SAndroid Build Coastguard Worker   // This will store the index of the raw_waitables which fired.
293*635a8641SAndroid Build Coastguard Worker   size_t signaled_index = 0;
294*635a8641SAndroid Build Coastguard Worker 
295*635a8641SAndroid Build Coastguard Worker   // Take the locks of each WaitableEvent in turn (except the signaled one) and
296*635a8641SAndroid Build Coastguard Worker   // remove our SyncWaiter from the wait-list
297*635a8641SAndroid Build Coastguard Worker   for (size_t i = 0; i < count; ++i) {
298*635a8641SAndroid Build Coastguard Worker     if (raw_waitables[i] != signaled_event) {
299*635a8641SAndroid Build Coastguard Worker       raw_waitables[i]->kernel_->lock_.Acquire();
300*635a8641SAndroid Build Coastguard Worker         // There's no possible ABA issue with the address of the SyncWaiter here
301*635a8641SAndroid Build Coastguard Worker         // because it lives on the stack. Thus the tag value is just the pointer
302*635a8641SAndroid Build Coastguard Worker         // value again.
303*635a8641SAndroid Build Coastguard Worker         raw_waitables[i]->kernel_->Dequeue(&sw, &sw);
304*635a8641SAndroid Build Coastguard Worker       raw_waitables[i]->kernel_->lock_.Release();
305*635a8641SAndroid Build Coastguard Worker     } else {
306*635a8641SAndroid Build Coastguard Worker       // By taking this lock here we ensure that |Signal| has completed by the
307*635a8641SAndroid Build Coastguard Worker       // time we return, because |Signal| holds this lock. This matches the
308*635a8641SAndroid Build Coastguard Worker       // behaviour of |Wait| and |TimedWait|.
309*635a8641SAndroid Build Coastguard Worker       raw_waitables[i]->kernel_->lock_.Acquire();
310*635a8641SAndroid Build Coastguard Worker       raw_waitables[i]->kernel_->lock_.Release();
311*635a8641SAndroid Build Coastguard Worker       signaled_index = i;
312*635a8641SAndroid Build Coastguard Worker     }
313*635a8641SAndroid Build Coastguard Worker   }
314*635a8641SAndroid Build Coastguard Worker 
315*635a8641SAndroid Build Coastguard Worker   return signaled_index;
316*635a8641SAndroid Build Coastguard Worker }
317*635a8641SAndroid Build Coastguard Worker 
318*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
319*635a8641SAndroid Build Coastguard Worker // If return value == count:
320*635a8641SAndroid Build Coastguard Worker //   The locks of the WaitableEvents have been taken in order and the Waiter has
321*635a8641SAndroid Build Coastguard Worker //   been enqueued in the wait-list of each. None of the WaitableEvents are
322*635a8641SAndroid Build Coastguard Worker //   currently signaled
323*635a8641SAndroid Build Coastguard Worker // else:
324*635a8641SAndroid Build Coastguard Worker //   None of the WaitableEvent locks are held. The Waiter has not been enqueued
325*635a8641SAndroid Build Coastguard Worker //   in any of them and the return value is the index of the WaitableEvent which
326*635a8641SAndroid Build Coastguard Worker //   was signaled with the lowest input index from the original WaitMany call.
327*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
328*635a8641SAndroid Build Coastguard Worker // static
EnqueueMany(std::pair<WaitableEvent *,size_t> * waitables,size_t count,Waiter * waiter)329*635a8641SAndroid Build Coastguard Worker size_t WaitableEvent::EnqueueMany(std::pair<WaitableEvent*, size_t>* waitables,
330*635a8641SAndroid Build Coastguard Worker                                   size_t count,
331*635a8641SAndroid Build Coastguard Worker                                   Waiter* waiter) {
332*635a8641SAndroid Build Coastguard Worker   size_t winner = count;
333*635a8641SAndroid Build Coastguard Worker   size_t winner_index = count;
334*635a8641SAndroid Build Coastguard Worker   for (size_t i = 0; i < count; ++i) {
335*635a8641SAndroid Build Coastguard Worker     auto& kernel = waitables[i].first->kernel_;
336*635a8641SAndroid Build Coastguard Worker     kernel->lock_.Acquire();
337*635a8641SAndroid Build Coastguard Worker     if (kernel->signaled_ && waitables[i].second < winner) {
338*635a8641SAndroid Build Coastguard Worker       winner = waitables[i].second;
339*635a8641SAndroid Build Coastguard Worker       winner_index = i;
340*635a8641SAndroid Build Coastguard Worker     }
341*635a8641SAndroid Build Coastguard Worker   }
342*635a8641SAndroid Build Coastguard Worker 
343*635a8641SAndroid Build Coastguard Worker   // No events signaled. All locks acquired. Enqueue the Waiter on all of them
344*635a8641SAndroid Build Coastguard Worker   // and return.
345*635a8641SAndroid Build Coastguard Worker   if (winner == count) {
346*635a8641SAndroid Build Coastguard Worker     for (size_t i = 0; i < count; ++i)
347*635a8641SAndroid Build Coastguard Worker       waitables[i].first->Enqueue(waiter);
348*635a8641SAndroid Build Coastguard Worker     return count;
349*635a8641SAndroid Build Coastguard Worker   }
350*635a8641SAndroid Build Coastguard Worker 
351*635a8641SAndroid Build Coastguard Worker   // Unlock in reverse order and possibly clear the chosen winner's signal
352*635a8641SAndroid Build Coastguard Worker   // before returning its index.
353*635a8641SAndroid Build Coastguard Worker   for (auto* w = waitables + count - 1; w >= waitables; --w) {
354*635a8641SAndroid Build Coastguard Worker     auto& kernel = w->first->kernel_;
355*635a8641SAndroid Build Coastguard Worker     if (w->second == winner) {
356*635a8641SAndroid Build Coastguard Worker       if (!kernel->manual_reset_)
357*635a8641SAndroid Build Coastguard Worker         kernel->signaled_ = false;
358*635a8641SAndroid Build Coastguard Worker     }
359*635a8641SAndroid Build Coastguard Worker     kernel->lock_.Release();
360*635a8641SAndroid Build Coastguard Worker   }
361*635a8641SAndroid Build Coastguard Worker 
362*635a8641SAndroid Build Coastguard Worker   return winner_index;
363*635a8641SAndroid Build Coastguard Worker }
364*635a8641SAndroid Build Coastguard Worker 
365*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
366*635a8641SAndroid Build Coastguard Worker 
367*635a8641SAndroid Build Coastguard Worker 
368*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
369*635a8641SAndroid Build Coastguard Worker // Private functions...
370*635a8641SAndroid Build Coastguard Worker 
WaitableEventKernel(ResetPolicy reset_policy,InitialState initial_state)371*635a8641SAndroid Build Coastguard Worker WaitableEvent::WaitableEventKernel::WaitableEventKernel(
372*635a8641SAndroid Build Coastguard Worker     ResetPolicy reset_policy,
373*635a8641SAndroid Build Coastguard Worker     InitialState initial_state)
374*635a8641SAndroid Build Coastguard Worker     : manual_reset_(reset_policy == ResetPolicy::MANUAL),
375*635a8641SAndroid Build Coastguard Worker       signaled_(initial_state == InitialState::SIGNALED) {}
376*635a8641SAndroid Build Coastguard Worker 
377*635a8641SAndroid Build Coastguard Worker WaitableEvent::WaitableEventKernel::~WaitableEventKernel() = default;
378*635a8641SAndroid Build Coastguard Worker 
379*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
380*635a8641SAndroid Build Coastguard Worker // Wake all waiting waiters. Called with lock held.
381*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
SignalAll()382*635a8641SAndroid Build Coastguard Worker bool WaitableEvent::SignalAll() {
383*635a8641SAndroid Build Coastguard Worker   bool signaled_at_least_one = false;
384*635a8641SAndroid Build Coastguard Worker 
385*635a8641SAndroid Build Coastguard Worker   for (std::list<Waiter*>::iterator
386*635a8641SAndroid Build Coastguard Worker        i = kernel_->waiters_.begin(); i != kernel_->waiters_.end(); ++i) {
387*635a8641SAndroid Build Coastguard Worker     if ((*i)->Fire(this))
388*635a8641SAndroid Build Coastguard Worker       signaled_at_least_one = true;
389*635a8641SAndroid Build Coastguard Worker   }
390*635a8641SAndroid Build Coastguard Worker 
391*635a8641SAndroid Build Coastguard Worker   kernel_->waiters_.clear();
392*635a8641SAndroid Build Coastguard Worker   return signaled_at_least_one;
393*635a8641SAndroid Build Coastguard Worker }
394*635a8641SAndroid Build Coastguard Worker 
395*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
396*635a8641SAndroid Build Coastguard Worker // Try to wake a single waiter. Return true if one was woken. Called with lock
397*635a8641SAndroid Build Coastguard Worker // held.
398*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
SignalOne()399*635a8641SAndroid Build Coastguard Worker bool WaitableEvent::SignalOne() {
400*635a8641SAndroid Build Coastguard Worker   for (;;) {
401*635a8641SAndroid Build Coastguard Worker     if (kernel_->waiters_.empty())
402*635a8641SAndroid Build Coastguard Worker       return false;
403*635a8641SAndroid Build Coastguard Worker 
404*635a8641SAndroid Build Coastguard Worker     const bool r = (*kernel_->waiters_.begin())->Fire(this);
405*635a8641SAndroid Build Coastguard Worker     kernel_->waiters_.pop_front();
406*635a8641SAndroid Build Coastguard Worker     if (r)
407*635a8641SAndroid Build Coastguard Worker       return true;
408*635a8641SAndroid Build Coastguard Worker   }
409*635a8641SAndroid Build Coastguard Worker }
410*635a8641SAndroid Build Coastguard Worker 
411*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
412*635a8641SAndroid Build Coastguard Worker // Add a waiter to the list of those waiting. Called with lock held.
413*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
Enqueue(Waiter * waiter)414*635a8641SAndroid Build Coastguard Worker void WaitableEvent::Enqueue(Waiter* waiter) {
415*635a8641SAndroid Build Coastguard Worker   kernel_->waiters_.push_back(waiter);
416*635a8641SAndroid Build Coastguard Worker }
417*635a8641SAndroid Build Coastguard Worker 
418*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
419*635a8641SAndroid Build Coastguard Worker // Remove a waiter from the list of those waiting. Return true if the waiter was
420*635a8641SAndroid Build Coastguard Worker // actually removed. Called with lock held.
421*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
Dequeue(Waiter * waiter,void * tag)422*635a8641SAndroid Build Coastguard Worker bool WaitableEvent::WaitableEventKernel::Dequeue(Waiter* waiter, void* tag) {
423*635a8641SAndroid Build Coastguard Worker   for (std::list<Waiter*>::iterator
424*635a8641SAndroid Build Coastguard Worker        i = waiters_.begin(); i != waiters_.end(); ++i) {
425*635a8641SAndroid Build Coastguard Worker     if (*i == waiter && (*i)->Compare(tag)) {
426*635a8641SAndroid Build Coastguard Worker       waiters_.erase(i);
427*635a8641SAndroid Build Coastguard Worker       return true;
428*635a8641SAndroid Build Coastguard Worker     }
429*635a8641SAndroid Build Coastguard Worker   }
430*635a8641SAndroid Build Coastguard Worker 
431*635a8641SAndroid Build Coastguard Worker   return false;
432*635a8641SAndroid Build Coastguard Worker }
433*635a8641SAndroid Build Coastguard Worker 
434*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
435*635a8641SAndroid Build Coastguard Worker 
436*635a8641SAndroid Build Coastguard Worker }  // namespace base
437