blob: 32e1553891e2245a381855c43f7e8bd160225323 [file] [log] [blame]
Ian Rogersef7d42f2014-01-06 12:55:46 -08001/*
2 * Copyright (C) 2014 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_POOL_H_
18#define ART_RUNTIME_MONITOR_POOL_H_
19
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070020#include "monitor.h"
21
Ian Rogers44d6ff12014-03-06 23:11:11 -080022#ifdef __LP64__
23#include <bitset>
Ian Rogersef7d42f2014-01-06 12:55:46 -080024#include <stdint.h>
25
Ian Rogers44d6ff12014-03-06 23:11:11 -080026#include "runtime.h"
27#include "safe_map.h"
28#endif
29
Ian Rogersef7d42f2014-01-06 12:55:46 -080030namespace art {
31
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*.
34class MonitorPool {
35 public:
36 static MonitorPool* Create() {
37#ifndef __LP64__
38 return nullptr;
39#else
40 return new MonitorPool();
41#endif
42 }
43
44 static Monitor* MonitorFromMonitorId(MonitorId mon_id) {
45#ifndef __LP64__
46 return reinterpret_cast<Monitor*>(mon_id << 3);
47#else
48 return Runtime::Current()->GetMonitorPool()->LookupMonitorFromTable(mon_id);
49#endif
50 }
51
52 static MonitorId MonitorIdFromMonitor(Monitor* mon) {
53#ifndef __LP64__
54 return reinterpret_cast<MonitorId>(mon) >> 3;
55#else
56 return mon->GetMonitorId();
57#endif
58 }
59
60 static MonitorId CreateMonitorId(Thread* self, Monitor* mon) {
61#ifndef __LP64__
62 UNUSED(self);
63 return MonitorIdFromMonitor(mon);
64#else
65 return Runtime::Current()->GetMonitorPool()->AllocMonitorIdFromTable(self, mon);
66#endif
67 }
68
69 static void ReleaseMonitorId(MonitorId mon_id) {
70#ifndef __LP64__
71 UNUSED(mon_id);
72#else
73 Runtime::Current()->GetMonitorPool()->ReleaseMonitorIdFromTable(mon_id);
74#endif
75 }
76
77 private:
78#ifdef __LP64__
79 MonitorPool();
80
81 Monitor* LookupMonitorFromTable(MonitorId mon_id);
82
83 MonitorId LookupMonitorIdFromTable(Monitor* mon);
84
85 MonitorId AllocMonitorIdFromTable(Thread* self, Monitor* mon);
86
87 void ReleaseMonitorIdFromTable(MonitorId mon_id);
88
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_);
93#endif
94};
95
96} // namespace art
97
98#endif // ART_RUNTIME_MONITOR_POOL_H_