2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_RUNTIME_MONITOR_POOL_H_
18 #define ART_RUNTIME_MONITOR_POOL_H_
32 // Abstraction to keep monitors small enough to fit in a lock word (32bits). On 32bit systems the
33 // monitor id loses the alignment bits of the Monitor*.
36 static MonitorPool* Create() {
40 return new MonitorPool();
44 static Monitor* MonitorFromMonitorId(MonitorId mon_id) {
46 return reinterpret_cast<Monitor*>(mon_id << 3);
48 return Runtime::Current()->GetMonitorPool()->LookupMonitorFromTable(mon_id);
52 static MonitorId MonitorIdFromMonitor(Monitor* mon) {
54 return reinterpret_cast<MonitorId>(mon) >> 3;
56 return mon->GetMonitorId();
60 static MonitorId CreateMonitorId(Thread* self, Monitor* mon) {
63 return MonitorIdFromMonitor(mon);
65 return Runtime::Current()->GetMonitorPool()->AllocMonitorIdFromTable(self, mon);
69 static void ReleaseMonitorId(MonitorId mon_id) {
73 Runtime::Current()->GetMonitorPool()->ReleaseMonitorIdFromTable(mon_id);
81 Monitor* LookupMonitorFromTable(MonitorId mon_id);
83 MonitorId LookupMonitorIdFromTable(Monitor* mon);
85 MonitorId AllocMonitorIdFromTable(Thread* self, Monitor* mon);
87 void ReleaseMonitorIdFromTable(MonitorId mon_id);
89 ReaderWriterMutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
90 static constexpr uint32_t kMaxMonitorId = 0xFFFF;
91 std::bitset<kMaxMonitorId> allocated_ids_ GUARDED_BY(allocated_ids_lock_);
92 SafeMap<MonitorId, Monitor*> table_ GUARDED_BY(allocated_ids_lock_);
98 #endif // ART_RUNTIME_MONITOR_POOL_H_