blob: 8d3cf450f36c0d6eddd69c4bfbc4465345115f62 [file] [log] [blame]
Elliott Hughes76b61672012-12-12 17:47:30 -08001/*
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
Mathieu Chartier858f1c52012-10-17 17:45:55 -070017#include "barrier.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080018
Andreas Gampe39b378c2017-12-07 15:44:13 -080019#include <android-base/logging.h>
20
21#include "base/aborting.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080022#include "base/mutex.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Mathieu Chartier858f1c52012-10-17 17:45:55 -070024#include "thread.h"
25
26namespace art {
27
Mathieu Chartier35883cc2012-11-13 14:08:12 -080028Barrier::Barrier(int count)
29 : count_(count),
Ian Rogers8409ec42014-11-04 17:57:02 -080030 lock_("GC barrier lock", kThreadSuspendCountLock),
Mathieu Chartier858f1c52012-10-17 17:45:55 -070031 condition_("GC barrier condition", lock_) {
32}
33
Alex Light318afe62018-03-22 16:50:10 -070034template void Barrier::Increment<Barrier::kAllowHoldingLocks>(Thread* self, int delta);
35template void Barrier::Increment<Barrier::kDisallowHoldingLocks>(Thread* self, int delta);
36
Mathieu Chartier858f1c52012-10-17 17:45:55 -070037void Barrier::Pass(Thread* self) {
38 MutexLock mu(self, lock_);
39 SetCountLocked(self, count_ - 1);
40}
41
42void Barrier::Wait(Thread* self) {
43 Increment(self, -1);
44}
45
46void Barrier::Init(Thread* self, int count) {
47 MutexLock mu(self, lock_);
48 SetCountLocked(self, count);
49}
50
Alex Light318afe62018-03-22 16:50:10 -070051template <Barrier::LockHandling locks>
Mathieu Chartier858f1c52012-10-17 17:45:55 -070052void Barrier::Increment(Thread* self, int delta) {
53 MutexLock mu(self, lock_);
54 SetCountLocked(self, count_ + delta);
Dave Allison0aded082013-11-07 13:15:11 -080055
56 // Increment the count. If it becomes zero after the increment
57 // then all the threads have already passed the barrier. If
58 // it is non-zero then there is still one or more threads
59 // that have not yet called the Pass function. When the
60 // Pass function is called by the last thread, the count will
61 // be decremented to zero and a Broadcast will be made on the
62 // condition variable, thus waking this up.
Hans Boehm5567c112014-12-02 18:31:31 -080063 while (count_ != 0) {
Alex Light318afe62018-03-22 16:50:10 -070064 if (locks == kAllowHoldingLocks) {
65 condition_.WaitHoldingLocks(self);
66 } else {
67 condition_.Wait(self);
68 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -070069 }
70}
71
Ian Rogers7b078e82014-09-10 14:44:24 -070072bool Barrier::Increment(Thread* self, int delta, uint32_t timeout_ms) {
Dave Allison0aded082013-11-07 13:15:11 -080073 MutexLock mu(self, lock_);
74 SetCountLocked(self, count_ + delta);
Ian Rogers7b078e82014-09-10 14:44:24 -070075 bool timed_out = false;
Dave Allison0aded082013-11-07 13:15:11 -080076 if (count_ != 0) {
Hans Boehm5567c112014-12-02 18:31:31 -080077 uint32_t timeout_ns = 0;
78 uint64_t abs_timeout = NanoTime() + MsToNs(timeout_ms);
79 for (;;) {
80 timed_out = condition_.TimedWait(self, timeout_ms, timeout_ns);
81 if (timed_out || count_ == 0) return timed_out;
82 // Compute time remaining on timeout.
83 uint64_t now = NanoTime();
84 int64_t time_left = abs_timeout - now;
85 if (time_left <= 0) return true;
86 timeout_ns = time_left % (1000*1000);
87 timeout_ms = time_left / (1000*1000);
88 }
Dave Allison0aded082013-11-07 13:15:11 -080089 }
Ian Rogers7b078e82014-09-10 14:44:24 -070090 return timed_out;
Dave Allison0aded082013-11-07 13:15:11 -080091}
92
Hiroshi Yamauchia82769c2016-12-02 17:01:51 -080093int Barrier::GetCount(Thread* self) {
94 MutexLock mu(self, lock_);
95 return count_;
96}
97
Mathieu Chartier858f1c52012-10-17 17:45:55 -070098void Barrier::SetCountLocked(Thread* self, int count) {
99 count_ = count;
Ian Rogers7b078e82014-09-10 14:44:24 -0700100 if (count == 0) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700101 condition_.Broadcast(self);
102 }
103}
104
105Barrier::~Barrier() {
Andreas Gampe1bb907e2015-06-22 10:04:39 -0700106 if (gAborting == 0) {
107 // Only check when not aborting.
108 CHECK_EQ(count_, 0) << "Attempted to destroy barrier with non zero count";
109 } else {
110 if (count_ != 0) {
111 LOG(WARNING) << "Attempted to destroy barrier with non zero count " << count_;
112 }
113 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700114}
115
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700116} // namespace art