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