1 /*
2  * Copyright (C) 2008 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 #ifndef ART_RUNTIME_MONITOR_INL_H_
18 #define ART_RUNTIME_MONITOR_INL_H_
19 
20 #include "monitor.h"
21 
22 #include "gc_root-inl.h"
23 #include "obj_ptr-inl.h"
24 
25 namespace art {
26 
27 template<ReadBarrierOption kReadBarrierOption>
GetObject()28 inline ObjPtr<mirror::Object> Monitor::GetObject() REQUIRES_SHARED(Locks::mutator_lock_) {
29   return obj_.Read<kReadBarrierOption>();
30 }
31 
32 // Check for request to set lock owner info.
CheckLockOwnerRequest(Thread * self)33 void Monitor::CheckLockOwnerRequest(Thread* self) {
34   DCHECK(self != nullptr);
35   Thread* request_thread = lock_owner_request_.load(std::memory_order_relaxed);
36   if (request_thread == self) {
37     SetLockingMethod(self);
38     // Only do this the first time after a request.
39     lock_owner_request_.store(nullptr, std::memory_order_relaxed);
40   }
41 }
42 
LockOwnerInfoChecksum(ArtMethod * m,uint32_t dex_pc,Thread * t)43 uintptr_t Monitor::LockOwnerInfoChecksum(ArtMethod* m, uint32_t dex_pc, Thread* t) {
44   uintptr_t dpc_and_thread = static_cast<uintptr_t>(dex_pc << 8) ^ reinterpret_cast<uintptr_t>(t);
45   return reinterpret_cast<uintptr_t>(m) ^ dpc_and_thread
46       ^ (dpc_and_thread << (/* ptr_size / 2 */ (sizeof m) << 2));
47 }
48 
SetLockOwnerInfo(ArtMethod * method,uint32_t dex_pc,Thread * t)49 void Monitor::SetLockOwnerInfo(ArtMethod* method, uint32_t dex_pc, Thread* t) {
50   lock_owner_method_.store(method, std::memory_order_relaxed);
51   lock_owner_dex_pc_.store(dex_pc, std::memory_order_relaxed);
52   lock_owner_.store(t, std::memory_order_relaxed);
53   uintptr_t sum = LockOwnerInfoChecksum(method, dex_pc, t);
54   lock_owner_sum_.store(sum, std::memory_order_relaxed);
55 }
56 
GetLockOwnerInfo(ArtMethod ** method,uint32_t * dex_pc,Thread * t)57 void Monitor::GetLockOwnerInfo(/*out*/ArtMethod** method, /*out*/uint32_t* dex_pc,
58                                Thread* t) {
59   ArtMethod* owners_method;
60   uint32_t owners_dex_pc;
61   Thread* owner;
62   uintptr_t owners_sum;
63   DCHECK(t != nullptr);
64   do {
65     owner = lock_owner_.load(std::memory_order_relaxed);
66     if (owner == nullptr) {
67       break;
68     }
69     owners_method = lock_owner_method_.load(std::memory_order_relaxed);
70     owners_dex_pc = lock_owner_dex_pc_.load(std::memory_order_relaxed);
71     owners_sum = lock_owner_sum_.load(std::memory_order_relaxed);
72   } while (owners_sum != LockOwnerInfoChecksum(owners_method, owners_dex_pc, owner));
73   if (owner == t) {
74     *method = owners_method;
75     *dex_pc = owners_dex_pc;
76   } else {
77     *method = nullptr;
78     *dex_pc = 0;
79   }
80 }
81 
82 
83 }  // namespace art
84 
85 #endif  // ART_RUNTIME_MONITOR_INL_H_
86