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