Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BARRIER_H_ |
| 18 | #define ART_RUNTIME_BARRIER_H_ |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 21 | #include "base/mutex.h" |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class Barrier { |
| 26 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 27 | explicit Barrier(int count); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 28 | virtual ~Barrier(); |
| 29 | |
| 30 | // Pass through the barrier, decrements the count but does not block. |
| 31 | void Pass(Thread* self); |
| 32 | |
| 33 | // Wait on the barrier, decrement the count. |
| 34 | void Wait(Thread* self); |
| 35 | |
| 36 | // Set the count to a new value, if the value is 0 then everyone waiting on the condition |
| 37 | // variable is resumed. |
| 38 | void Init(Thread* self, int count); |
| 39 | |
| 40 | // Increment the count by delta, wait on condition if count is non zero. |
| 41 | void Increment(Thread* self, int delta); |
| 42 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 43 | // Increment the count by delta, wait on condition if count is non zero, with a timeout |
| 44 | void Increment(Thread* self, int delta, uint32_t timeout_ms) LOCKS_EXCLUDED(lock_); |
| 45 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 46 | private: |
| 47 | void SetCountLocked(Thread* self, int count) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 48 | |
| 49 | // Counter, when this reaches 0 all people blocked on the barrier are signalled. |
| 50 | int count_ GUARDED_BY(lock_); |
| 51 | |
| 52 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 53 | ConditionVariable condition_ GUARDED_BY(lock_); |
| 54 | }; |
| 55 | |
| 56 | } // namespace art |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 57 | #endif // ART_RUNTIME_BARRIER_H_ |