blob: 8a38c4c31052c65bd8dd81470d2f421dcb034302 [file] [log] [blame]
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001/*
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
Hans Boehm5567c112014-12-02 18:31:31 -080017// CAUTION: THIS IS NOT A FULLY GENERAL BARRIER API.
18
19// It may either be used as a "latch" or single-use barrier, or it may be reused under
20// very limited conditions, e.g. if only Pass(), but not Wait() is called. Unlike a standard
21// latch API, it is possible to initialize the latch to a count of zero, repeatedly call
22// Pass() or Wait(), and only then set the count using the Increment() method. Threads at
23// a Wait() are only awoken if the count reaches zero AFTER the decrement is applied.
24// This works because, also unlike most latch APIs, there is no way to Wait() without
25// decrementing the count, and thus nobody can spuriosly wake up on the initial zero.
26
Brian Carlstromfc0e3212013-07-17 14:40:12 -070027#ifndef ART_RUNTIME_BARRIER_H_
28#define ART_RUNTIME_BARRIER_H_
Mathieu Chartier858f1c52012-10-17 17:45:55 -070029
Ian Rogers700a4022014-05-19 16:49:03 -070030#include <memory>
Elliott Hughes76b61672012-12-12 17:47:30 -080031#include "base/mutex.h"
Mathieu Chartier858f1c52012-10-17 17:45:55 -070032
33namespace art {
34
Hans Boehm5567c112014-12-02 18:31:31 -080035// TODO: Maybe give this a better name.
Mathieu Chartier858f1c52012-10-17 17:45:55 -070036class Barrier {
37 public:
Alex Lightac5fea02018-03-22 16:50:10 -070038 enum LockHandling {
39 kAllowHoldingLocks,
40 kDisallowHoldingLocks,
41 };
42
Brian Carlstrom93ba8932013-07-17 21:31:49 -070043 explicit Barrier(int count);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070044 virtual ~Barrier();
45
Hans Boehm5567c112014-12-02 18:31:31 -080046 // Pass through the barrier, decrement the count but do not block.
Mathieu Chartier90443472015-07-16 20:32:27 -070047 void Pass(Thread* self) REQUIRES(!lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070048
49 // Wait on the barrier, decrement the count.
Mathieu Chartier90443472015-07-16 20:32:27 -070050 void Wait(Thread* self) REQUIRES(!lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070051
Hans Boehm5567c112014-12-02 18:31:31 -080052 // The following three calls are only safe if we somehow know that no other thread both
53 // - has been woken up, and
54 // - has not left the Wait() or Increment() call.
55 // If these calls are made in that situation, the offending thread is likely to go back
56 // to sleep, resulting in a deadlock.
Mathieu Chartier858f1c52012-10-17 17:45:55 -070057
Alex Lightac5fea02018-03-22 16:50:10 -070058 // Increment the count by delta, wait on condition if count is non zero. If LockHandling is
59 // kAllowHoldingLocks we will not check that all locks are released when waiting.
60 template <Barrier::LockHandling locks = kDisallowHoldingLocks>
Mathieu Chartier90ef3db2015-08-04 15:19:41 -070061 void Increment(Thread* self, int delta) REQUIRES(!lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070062
Ian Rogers7b078e82014-09-10 14:44:24 -070063 // Increment the count by delta, wait on condition if count is non zero, with a timeout. Returns
64 // true if time out occurred.
Mathieu Chartier90443472015-07-16 20:32:27 -070065 bool Increment(Thread* self, int delta, uint32_t timeout_ms) REQUIRES(!lock_);
Dave Allison0aded082013-11-07 13:15:11 -080066
Hans Boehm5567c112014-12-02 18:31:31 -080067 // Set the count to a new value. This should only be used if there is no possibility that
68 // another thread is still in Wait(). See above.
Mathieu Chartier90443472015-07-16 20:32:27 -070069 void Init(Thread* self, int count) REQUIRES(!lock_);
Hans Boehm5567c112014-12-02 18:31:31 -080070
Hiroshi Yamauchia82769c2016-12-02 17:01:51 -080071 int GetCount(Thread* self) REQUIRES(!lock_);
72
Mathieu Chartier858f1c52012-10-17 17:45:55 -070073 private:
Mathieu Chartier90443472015-07-16 20:32:27 -070074 void SetCountLocked(Thread* self, int count) REQUIRES(lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070075
76 // Counter, when this reaches 0 all people blocked on the barrier are signalled.
77 int count_ GUARDED_BY(lock_);
78
Ian Rogers8409ec42014-11-04 17:57:02 -080079 Mutex lock_ ACQUIRED_AFTER(Locks::abort_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070080 ConditionVariable condition_ GUARDED_BY(lock_);
81};
82
83} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -070084#endif // ART_RUNTIME_BARRIER_H_