Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | #include "object_lock.h" |
| 18 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 19 | #include "mirror/class_ext.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include "mirror/object-inl.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 21 | #include "monitor.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | template <typename T> |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 26 | ObjectLock<T>::ObjectLock(Thread* self, Handle<T> object) : self_(self), obj_(object) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 27 | CHECK(object != nullptr); |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 28 | obj_->MonitorEnter(self_); |
| 29 | } |
| 30 | |
| 31 | template <typename T> |
| 32 | ObjectLock<T>::~ObjectLock() { |
| 33 | obj_->MonitorExit(self_); |
| 34 | } |
| 35 | |
| 36 | template <typename T> |
| 37 | void ObjectLock<T>::WaitIgnoringInterrupts() { |
| 38 | Monitor::Wait(self_, obj_.Get(), 0, 0, false, kWaiting); |
| 39 | } |
| 40 | |
| 41 | template <typename T> |
| 42 | void ObjectLock<T>::Notify() { |
| 43 | obj_->Notify(self_); |
| 44 | } |
| 45 | |
| 46 | template <typename T> |
| 47 | void ObjectLock<T>::NotifyAll() { |
| 48 | obj_->NotifyAll(self_); |
| 49 | } |
| 50 | |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 51 | template <typename T> |
| 52 | ObjectTryLock<T>::ObjectTryLock(Thread* self, Handle<T> object) : self_(self), obj_(object) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 53 | CHECK(object != nullptr); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 54 | acquired_ = obj_->MonitorTryEnter(self_) != nullptr; |
| 55 | } |
| 56 | |
| 57 | template <typename T> |
| 58 | ObjectTryLock<T>::~ObjectTryLock() { |
| 59 | if (acquired_) { |
| 60 | obj_->MonitorExit(self_); |
| 61 | } |
| 62 | } |
| 63 | |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 64 | template class ObjectLock<mirror::Class>; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 65 | template class ObjectLock<mirror::ClassExt>; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 66 | template class ObjectLock<mirror::Object>; |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 67 | template class ObjectTryLock<mirror::Class>; |
| 68 | template class ObjectTryLock<mirror::Object>; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 69 | |
| 70 | } // namespace art |