xref: /aosp_15_r20/art/runtime/barrier.cc (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 #include "barrier.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "base/aborting.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
24*795d594fSAndroid Build Coastguard Worker #include "thread.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
Barrier(int count,bool verify_count_on_shutdown)28*795d594fSAndroid Build Coastguard Worker Barrier::Barrier(int count, bool verify_count_on_shutdown)
29*795d594fSAndroid Build Coastguard Worker     : count_(count),
30*795d594fSAndroid Build Coastguard Worker       lock_(new Mutex("GC barrier lock", kThreadSuspendCountLock)),
31*795d594fSAndroid Build Coastguard Worker       condition_(new ConditionVariable("GC barrier condition", *lock_)),
32*795d594fSAndroid Build Coastguard Worker       verify_count_on_shutdown_(verify_count_on_shutdown) {
33*795d594fSAndroid Build Coastguard Worker }
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker template void Barrier::Increment<Barrier::kAllowHoldingLocks>(Thread* self, int delta);
36*795d594fSAndroid Build Coastguard Worker template void Barrier::Increment<Barrier::kDisallowHoldingLocks>(Thread* self, int delta);
37*795d594fSAndroid Build Coastguard Worker 
Pass(Thread * self)38*795d594fSAndroid Build Coastguard Worker void Barrier::Pass(Thread* self) {
39*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, *GetLock());
40*795d594fSAndroid Build Coastguard Worker   SetCountLocked(self, count_ - 1);
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker 
IncrementNoWait(Thread * self)43*795d594fSAndroid Build Coastguard Worker void Barrier::IncrementNoWait(Thread* self) {
44*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, *GetLock());
45*795d594fSAndroid Build Coastguard Worker   SetCountLocked(self, count_ + 1);
46*795d594fSAndroid Build Coastguard Worker }
47*795d594fSAndroid Build Coastguard Worker 
Wait(Thread * self)48*795d594fSAndroid Build Coastguard Worker void Barrier::Wait(Thread* self) {
49*795d594fSAndroid Build Coastguard Worker   Increment(self, -1);
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker 
Init(Thread * self,int count)52*795d594fSAndroid Build Coastguard Worker void Barrier::Init(Thread* self, int count) {
53*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, *GetLock());
54*795d594fSAndroid Build Coastguard Worker   SetCountLocked(self, count);
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker template <Barrier::LockHandling locks>
Increment(Thread * self,int delta)58*795d594fSAndroid Build Coastguard Worker void Barrier::Increment(Thread* self, int delta) {
59*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, *GetLock());
60*795d594fSAndroid Build Coastguard Worker   SetCountLocked(self, count_ + delta);
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker   // Increment the count.  If it becomes zero after the increment
63*795d594fSAndroid Build Coastguard Worker   // then all the threads have already passed the barrier.  If
64*795d594fSAndroid Build Coastguard Worker   // it is non-zero then there is still one or more threads
65*795d594fSAndroid Build Coastguard Worker   // that have not yet called the Pass function.  When the
66*795d594fSAndroid Build Coastguard Worker   // Pass function is called by the last thread, the count will
67*795d594fSAndroid Build Coastguard Worker   // be decremented to zero and a Broadcast will be made on the
68*795d594fSAndroid Build Coastguard Worker   // condition variable, thus waking this up.
69*795d594fSAndroid Build Coastguard Worker   while (count_ != 0) {
70*795d594fSAndroid Build Coastguard Worker     if (locks == kAllowHoldingLocks) {
71*795d594fSAndroid Build Coastguard Worker       condition_->WaitHoldingLocks(self);
72*795d594fSAndroid Build Coastguard Worker     } else {
73*795d594fSAndroid Build Coastguard Worker       condition_->Wait(self);
74*795d594fSAndroid Build Coastguard Worker     }
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker 
Increment(Thread * self,int delta,uint32_t timeout_ms)78*795d594fSAndroid Build Coastguard Worker bool Barrier::Increment(Thread* self, int delta, uint32_t timeout_ms) {
79*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, *GetLock());
80*795d594fSAndroid Build Coastguard Worker   SetCountLocked(self, count_ + delta);
81*795d594fSAndroid Build Coastguard Worker   bool timed_out = false;
82*795d594fSAndroid Build Coastguard Worker   if (count_ != 0) {
83*795d594fSAndroid Build Coastguard Worker     uint32_t timeout_ns = 0;
84*795d594fSAndroid Build Coastguard Worker     uint64_t abs_timeout = NanoTime() + MsToNs(timeout_ms);
85*795d594fSAndroid Build Coastguard Worker     for (;;) {
86*795d594fSAndroid Build Coastguard Worker       timed_out = condition_->TimedWait(self, timeout_ms, timeout_ns);
87*795d594fSAndroid Build Coastguard Worker       if (timed_out || count_ == 0) return timed_out;
88*795d594fSAndroid Build Coastguard Worker       // Compute time remaining on timeout.
89*795d594fSAndroid Build Coastguard Worker       uint64_t now = NanoTime();
90*795d594fSAndroid Build Coastguard Worker       int64_t time_left = abs_timeout - now;
91*795d594fSAndroid Build Coastguard Worker       if (time_left <= 0) return true;
92*795d594fSAndroid Build Coastguard Worker       timeout_ns = time_left % (1000*1000);
93*795d594fSAndroid Build Coastguard Worker       timeout_ms = time_left / (1000*1000);
94*795d594fSAndroid Build Coastguard Worker     }
95*795d594fSAndroid Build Coastguard Worker   }
96*795d594fSAndroid Build Coastguard Worker   return timed_out;
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker 
GetCount(Thread * self)99*795d594fSAndroid Build Coastguard Worker int Barrier::GetCount(Thread* self) {
100*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, *GetLock());
101*795d594fSAndroid Build Coastguard Worker   return count_;
102*795d594fSAndroid Build Coastguard Worker }
103*795d594fSAndroid Build Coastguard Worker 
SetCountLocked(Thread * self,int count)104*795d594fSAndroid Build Coastguard Worker void Barrier::SetCountLocked(Thread* self, int count) {
105*795d594fSAndroid Build Coastguard Worker   count_ = count;
106*795d594fSAndroid Build Coastguard Worker   if (count == 0) {
107*795d594fSAndroid Build Coastguard Worker     condition_->Broadcast(self);
108*795d594fSAndroid Build Coastguard Worker   }
109*795d594fSAndroid Build Coastguard Worker }
110*795d594fSAndroid Build Coastguard Worker 
~Barrier()111*795d594fSAndroid Build Coastguard Worker Barrier::~Barrier() {
112*795d594fSAndroid Build Coastguard Worker   if (count_ != 0) {
113*795d594fSAndroid Build Coastguard Worker     // Only check when not aborting and if we verify the count on shutdown.
114*795d594fSAndroid Build Coastguard Worker     LOG((gAborting == 0 && verify_count_on_shutdown_) ? FATAL : WARNING)
115*795d594fSAndroid Build Coastguard Worker         << "Attempted to destroy barrier with non zero count " << count_;
116*795d594fSAndroid Build Coastguard Worker   }
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker }  // namespace art
120