xref: /aosp_15_r20/art/runtime/barrier.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker // CAUTION: THIS IS NOT A FULLY GENERAL BARRIER API. Some names are unconventional.
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker // It may either be used as a "latch" or single-use barrier, or it may be reused under
20*795d594fSAndroid Build Coastguard Worker // very limited conditions, e.g. if only Pass(), but not Wait() is called.  Unlike a standard
21*795d594fSAndroid Build Coastguard Worker // latch API, it is possible to initialize the latch to a count of zero, repeatedly call
22*795d594fSAndroid Build Coastguard Worker // Pass() or Wait(), and only then set the count using the Increment() method.  Threads at
23*795d594fSAndroid Build Coastguard Worker // a Wait() are only awoken if the count reaches zero AFTER the decrement is applied.
24*795d594fSAndroid Build Coastguard Worker // This works because, also unlike most latch APIs, there is no way to Wait() without
25*795d594fSAndroid Build Coastguard Worker // decrementing the count, and thus nobody can spuriously wake up on the initial zero.
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_BARRIER_H_
28*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_BARRIER_H_
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker #include <memory>
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
33*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker class ConditionVariable;
38*795d594fSAndroid Build Coastguard Worker class LOCKABLE Mutex;
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker // TODO: Maybe give this a better name.
41*795d594fSAndroid Build Coastguard Worker class Barrier {
42*795d594fSAndroid Build Coastguard Worker  public:
43*795d594fSAndroid Build Coastguard Worker   enum EXPORT LockHandling {
44*795d594fSAndroid Build Coastguard Worker     kAllowHoldingLocks,
45*795d594fSAndroid Build Coastguard Worker     kDisallowHoldingLocks,
46*795d594fSAndroid Build Coastguard Worker   };
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker   // If verify_count_on_shutdown is true, the destructor verifies that the count is zero in the
49*795d594fSAndroid Build Coastguard Worker   // destructor. This means that all expected threads went through the barrier.
50*795d594fSAndroid Build Coastguard Worker   EXPORT explicit Barrier(int count, bool verify_count_on_shutdown = true);
51*795d594fSAndroid Build Coastguard Worker   EXPORT virtual ~Barrier();
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker   // Pass through the barrier, decrement the count but do not block.
54*795d594fSAndroid Build Coastguard Worker   EXPORT void Pass(Thread* self) REQUIRES(!GetLock());
55*795d594fSAndroid Build Coastguard Worker   // Increment the barrier but do not block. The caller should ensure that it
56*795d594fSAndroid Build Coastguard Worker   // decrements/passes it eventually.
57*795d594fSAndroid Build Coastguard Worker   void IncrementNoWait(Thread* self) REQUIRES(!GetLock());
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker   // Decrement the count, then wait until the count is zero.
60*795d594fSAndroid Build Coastguard Worker   void Wait(Thread* self) REQUIRES(!GetLock());
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker   // The following three calls are only safe if we somehow know that no other thread both
63*795d594fSAndroid Build Coastguard Worker   // - has been woken up, and
64*795d594fSAndroid Build Coastguard Worker   // - has not left the Wait() or Increment() call.
65*795d594fSAndroid Build Coastguard Worker   // If these calls are made in that situation, the offending thread is likely to go back
66*795d594fSAndroid Build Coastguard Worker   // to sleep, resulting in a deadlock.
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker   // Increment the count by delta, wait on condition while count is non zero.  If LockHandling is
69*795d594fSAndroid Build Coastguard Worker   // kAllowHoldingLocks we will not check that all locks are released when waiting.
70*795d594fSAndroid Build Coastguard Worker   template <Barrier::LockHandling locks = kDisallowHoldingLocks>
71*795d594fSAndroid Build Coastguard Worker   EXPORT void Increment(Thread* self, int delta) REQUIRES(!GetLock());
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker   // Increment the count by delta, wait on condition while count is non zero, with a timeout.
74*795d594fSAndroid Build Coastguard Worker   // Returns true if time out occurred.
75*795d594fSAndroid Build Coastguard Worker   bool Increment(Thread* self, int delta, uint32_t timeout_ms) REQUIRES(!GetLock());
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   // Set the count to a new value.  This should only be used if there is no possibility that
78*795d594fSAndroid Build Coastguard Worker   // another thread is still in Wait().  See above.
79*795d594fSAndroid Build Coastguard Worker   void Init(Thread* self, int count) REQUIRES(!GetLock());
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker   int GetCount(Thread* self) REQUIRES(!GetLock());
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker  private:
84*795d594fSAndroid Build Coastguard Worker   void SetCountLocked(Thread* self, int count) REQUIRES(GetLock());
85*795d594fSAndroid Build Coastguard Worker 
GetLock()86*795d594fSAndroid Build Coastguard Worker   Mutex* GetLock() {
87*795d594fSAndroid Build Coastguard Worker     return lock_.get();
88*795d594fSAndroid Build Coastguard Worker   }
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker   // Counter, when this reaches 0 all people blocked on the barrier are signalled.
91*795d594fSAndroid Build Coastguard Worker   int count_ GUARDED_BY(GetLock());
92*795d594fSAndroid Build Coastguard Worker 
93*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<Mutex> lock_ ACQUIRED_AFTER(Locks::abort_lock_);
94*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<ConditionVariable> condition_ GUARDED_BY(GetLock());
95*795d594fSAndroid Build Coastguard Worker   const bool verify_count_on_shutdown_;
96*795d594fSAndroid Build Coastguard Worker };
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker }  // namespace art
99*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_BARRIER_H_
100