1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "barrier.h"
18 
19 #include <android-base/logging.h>
20 
21 #include "base/aborting.h"
22 #include "base/mutex.h"
23 #include "base/time_utils.h"
24 #include "thread.h"
25 
26 namespace art {
27 
Barrier(int count,bool verify_count_on_shutdown)28 Barrier::Barrier(int count, bool verify_count_on_shutdown)
29     : count_(count),
30       lock_(new Mutex("GC barrier lock", kThreadSuspendCountLock)),
31       condition_(new ConditionVariable("GC barrier condition", *lock_)),
32       verify_count_on_shutdown_(verify_count_on_shutdown) {
33 }
34 
35 template void Barrier::Increment<Barrier::kAllowHoldingLocks>(Thread* self, int delta);
36 template void Barrier::Increment<Barrier::kDisallowHoldingLocks>(Thread* self, int delta);
37 
Pass(Thread * self)38 void Barrier::Pass(Thread* self) {
39   MutexLock mu(self, *GetLock());
40   SetCountLocked(self, count_ - 1);
41 }
42 
Wait(Thread * self)43 void Barrier::Wait(Thread* self) {
44   Increment(self, -1);
45 }
46 
Init(Thread * self,int count)47 void Barrier::Init(Thread* self, int count) {
48   MutexLock mu(self, *GetLock());
49   SetCountLocked(self, count);
50 }
51 
52 template <Barrier::LockHandling locks>
Increment(Thread * self,int delta)53 void Barrier::Increment(Thread* self, int delta) {
54   MutexLock mu(self, *GetLock());
55   SetCountLocked(self, count_ + delta);
56 
57   // Increment the count.  If it becomes zero after the increment
58   // then all the threads have already passed the barrier.  If
59   // it is non-zero then there is still one or more threads
60   // that have not yet called the Pass function.  When the
61   // Pass function is called by the last thread, the count will
62   // be decremented to zero and a Broadcast will be made on the
63   // condition variable, thus waking this up.
64   while (count_ != 0) {
65     if (locks == kAllowHoldingLocks) {
66       condition_->WaitHoldingLocks(self);
67     } else {
68       condition_->Wait(self);
69     }
70   }
71 }
72 
Increment(Thread * self,int delta,uint32_t timeout_ms)73 bool Barrier::Increment(Thread* self, int delta, uint32_t timeout_ms) {
74   MutexLock mu(self, *GetLock());
75   SetCountLocked(self, count_ + delta);
76   bool timed_out = false;
77   if (count_ != 0) {
78     uint32_t timeout_ns = 0;
79     uint64_t abs_timeout = NanoTime() + MsToNs(timeout_ms);
80     for (;;) {
81       timed_out = condition_->TimedWait(self, timeout_ms, timeout_ns);
82       if (timed_out || count_ == 0) return timed_out;
83       // Compute time remaining on timeout.
84       uint64_t now = NanoTime();
85       int64_t time_left = abs_timeout - now;
86       if (time_left <= 0) return true;
87       timeout_ns = time_left % (1000*1000);
88       timeout_ms = time_left / (1000*1000);
89     }
90   }
91   return timed_out;
92 }
93 
GetCount(Thread * self)94 int Barrier::GetCount(Thread* self) {
95   MutexLock mu(self, *GetLock());
96   return count_;
97 }
98 
SetCountLocked(Thread * self,int count)99 void Barrier::SetCountLocked(Thread* self, int count) {
100   count_ = count;
101   if (count == 0) {
102     condition_->Broadcast(self);
103   }
104 }
105 
~Barrier()106 Barrier::~Barrier() {
107   if (count_ != 0) {
108     // Only check when not aborting and if we verify the count on shutdown.
109     LOG((gAborting == 0 && verify_count_on_shutdown_) ? FATAL : WARNING)
110         << "Attempted to destroy barrier with non zero count " << count_;
111   }
112 }
113 
114 }  // namespace art
115