blob: d144591dafb844818fbcbfe076453bb198611961 [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 Chartier4f1e3282019-03-27 14:41:32 -070028Barrier::Barrier(int count, bool verify_count_on_shutdown)
Mathieu Chartier35883cc2012-11-13 14:08:12 -080029 : count_(count),
Andreas Gampe365a64b2018-11-29 11:24:00 -080030 lock_(new Mutex("GC barrier lock", kThreadSuspendCountLock)),
Mathieu Chartier4f1e3282019-03-27 14:41:32 -070031 condition_(new ConditionVariable("GC barrier condition", *lock_)),
32 verify_count_on_shutdown_(verify_count_on_shutdown) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -070033}
34
Alex Light318afe62018-03-22 16:50:10 -070035template void Barrier::Increment<Barrier::kAllowHoldingLocks>(Thread* self, int delta);
36template void Barrier::Increment<Barrier::kDisallowHoldingLocks>(Thread* self, int delta);
37
Mathieu Chartier858f1c52012-10-17 17:45:55 -070038void Barrier::Pass(Thread* self) {
Andreas Gampe365a64b2018-11-29 11:24:00 -080039 MutexLock mu(self, *GetLock());
Mathieu Chartier858f1c52012-10-17 17:45:55 -070040 SetCountLocked(self, count_ - 1);
41}
42
43void Barrier::Wait(Thread* self) {
44 Increment(self, -1);
45}
46
47void Barrier::Init(Thread* self, int count) {
Andreas Gampe365a64b2018-11-29 11:24:00 -080048 MutexLock mu(self, *GetLock());
Mathieu Chartier858f1c52012-10-17 17:45:55 -070049 SetCountLocked(self, count);
50}
51
Alex Light318afe62018-03-22 16:50:10 -070052template <Barrier::LockHandling locks>
Mathieu Chartier858f1c52012-10-17 17:45:55 -070053void Barrier::Increment(Thread* self, int delta) {
Andreas Gampe365a64b2018-11-29 11:24:00 -080054 MutexLock mu(self, *GetLock());
Mathieu Chartier858f1c52012-10-17 17:45:55 -070055 SetCountLocked(self, count_ + delta);
Dave Allison0aded082013-11-07 13:15:11 -080056
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.
Hans Boehm5567c112014-12-02 18:31:31 -080064 while (count_ != 0) {
Alex Light318afe62018-03-22 16:50:10 -070065 if (locks == kAllowHoldingLocks) {
Andreas Gampe365a64b2018-11-29 11:24:00 -080066 condition_->WaitHoldingLocks(self);
Alex Light318afe62018-03-22 16:50:10 -070067 } else {
Andreas Gampe365a64b2018-11-29 11:24:00 -080068 condition_->Wait(self);
Alex Light318afe62018-03-22 16:50:10 -070069 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -070070 }
71}
72
Ian Rogers7b078e82014-09-10 14:44:24 -070073bool Barrier::Increment(Thread* self, int delta, uint32_t timeout_ms) {
Andreas Gampe365a64b2018-11-29 11:24:00 -080074 MutexLock mu(self, *GetLock());
Dave Allison0aded082013-11-07 13:15:11 -080075 SetCountLocked(self, count_ + delta);
Ian Rogers7b078e82014-09-10 14:44:24 -070076 bool timed_out = false;
Dave Allison0aded082013-11-07 13:15:11 -080077 if (count_ != 0) {
Hans Boehm5567c112014-12-02 18:31:31 -080078 uint32_t timeout_ns = 0;
79 uint64_t abs_timeout = NanoTime() + MsToNs(timeout_ms);
80 for (;;) {
Andreas Gampe365a64b2018-11-29 11:24:00 -080081 timed_out = condition_->TimedWait(self, timeout_ms, timeout_ns);
Hans Boehm5567c112014-12-02 18:31:31 -080082 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 }
Dave Allison0aded082013-11-07 13:15:11 -080090 }
Ian Rogers7b078e82014-09-10 14:44:24 -070091 return timed_out;
Dave Allison0aded082013-11-07 13:15:11 -080092}
93
Hiroshi Yamauchia82769c2016-12-02 17:01:51 -080094int Barrier::GetCount(Thread* self) {
Andreas Gampe365a64b2018-11-29 11:24:00 -080095 MutexLock mu(self, *GetLock());
Hiroshi Yamauchia82769c2016-12-02 17:01:51 -080096 return count_;
97}
98
Mathieu Chartier858f1c52012-10-17 17:45:55 -070099void Barrier::SetCountLocked(Thread* self, int count) {
100 count_ = count;
Ian Rogers7b078e82014-09-10 14:44:24 -0700101 if (count == 0) {
Andreas Gampe365a64b2018-11-29 11:24:00 -0800102 condition_->Broadcast(self);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700103 }
104}
105
106Barrier::~Barrier() {
Mathieu Chartier4f1e3282019-03-27 14:41:32 -0700107 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)
Mathieu Chartier5a832252019-03-28 07:31:16 -0700110 << "Attempted to destroy barrier with non zero count " << count_;
Andreas Gampe1bb907e2015-06-22 10:04:39 -0700111 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700112}
113
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700114} // namespace art