blob: 32f3f4ebe390db5647706ae5975aad17f9b2b74d [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
20#include "monitor.h"
21
22#include "safe_map.h"
23
24#include <stdint.h>
25
26namespace art {
27
28// Abstraction to keep monitors small enough to fit in a lock word (32bits). On 32bit systems the
29// monitor id loses the alignment bits of the Monitor*.
30class MonitorPool {
31 public:
32 static MonitorPool* Create() {
33#ifndef __LP64__
34 return nullptr;
35#else
36 return new MonitorPool();
37#endif
38 }
39
40 static Monitor* MonitorFromMonitorId(MonitorId mon_id) {
41#ifndef __LP64__
42 return reinterpret_cast<Monitor*>(mon_id << 3);
43#else
44 return Runtime::Current()->GetMonitorPool()->LookupMonitorFromTable(mon_id);
45#endif
46 }
47
48 static MonitorId MonitorIdFromMonitor(Monitor* mon) {
49#ifndef __LP64__
50 return reinterpret_cast<MonitorId>(mon) >> 3;
51#else
52 return mon->GetMonitorId();
53#endif
54 }
55
56 static MonitorId CreateMonitorId(Thread* self, Monitor* mon) {
57#ifndef __LP64__
58 UNUSED(self);
59 return MonitorIdFromMonitor(mon);
60#else
61 return Runtime::Current()->GetMonitorPool()->AllocMonitorIdFromTable(self, mon);
62#endif
63 }
64
65 static void ReleaseMonitorId(MonitorId mon_id) {
66#ifndef __LP64__
67 UNUSED(mon_id);
68#else
69 Runtime::Current()->GetMonitorPool()->ReleaseMonitorIdFromTable(mon_id);
70#endif
71 }
72
73 private:
74#ifdef __LP64__
75 MonitorPool();
76
77 Monitor* LookupMonitorFromTable(MonitorId mon_id);
78
79 MonitorId LookupMonitorIdFromTable(Monitor* mon);
80
81 MonitorId AllocMonitorIdFromTable(Thread* self, Monitor* mon);
82
83 void ReleaseMonitorIdFromTable(MonitorId mon_id);
84
85 ReaderWriterMutex allocated_ids_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
86 static constexpr uint32_t kMaxMonitorId = 0xFFFF;
87 std::bitset<kMaxMonitorId> allocated_ids_ GUARDED_BY(allocated_ids_lock_);
88 SafeMap<MonitorId, Monitor*> table_ GUARDED_BY(allocated_ids_lock_);
89#endif
90};
91
92} // namespace art
93
94#endif // ART_RUNTIME_MONITOR_POOL_H_