blob: f19237e0fba89bca5759454ec2282401edab68f2 [file] [log] [blame]
Elliott Hughes5f791332011-09-15 17:45:30 -07001/*
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_SRC_SYNC_H_
18#define ART_SRC_SYNC_H_
19
20#include <pthread.h>
21#include <stdint.h>
22
23#include "mutex.h"
24
25namespace art {
26
27/*
28 * Monitor shape field. Used to distinguish thin locks from fat locks.
29 */
30#define LW_SHAPE_THIN 0
31#define LW_SHAPE_FAT 1
32#define LW_SHAPE_MASK 0x1
33#define LW_SHAPE(x) ((x) & LW_SHAPE_MASK)
34
35/*
36 * Hash state field. Used to signify that an object has had its
37 * identity hash code exposed or relocated.
38 */
39#define LW_HASH_STATE_UNHASHED 0
40#define LW_HASH_STATE_HASHED 1
41#define LW_HASH_STATE_HASHED_AND_MOVED 3
42#define LW_HASH_STATE_MASK 0x3
43#define LW_HASH_STATE_SHIFT 1
44#define LW_HASH_STATE(x) (((x) >> LW_HASH_STATE_SHIFT) & LW_HASH_STATE_MASK)
45
46/*
47 * Monitor accessor. Extracts a monitor structure pointer from a fat
48 * lock. Performs no error checking.
49 */
50#define LW_MONITOR(x) \
51 ((Monitor*)((x) & ~((LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT) | LW_SHAPE_MASK)))
52
53/*
54 * Lock owner field. Contains the thread id of the thread currently
55 * holding the lock.
56 */
57#define LW_LOCK_OWNER_MASK 0xffff
58#define LW_LOCK_OWNER_SHIFT 3
59#define LW_LOCK_OWNER(x) (((x) >> LW_LOCK_OWNER_SHIFT) & LW_LOCK_OWNER_MASK)
60
61/*
62 * Lock recursion count field. Contains a count of the numer of times
63 * a lock has been recursively acquired.
64 */
65#define LW_LOCK_COUNT_MASK 0x1fff
66#define LW_LOCK_COUNT_SHIFT 19
67#define LW_LOCK_COUNT(x) (((x) >> LW_LOCK_COUNT_SHIFT) & LW_LOCK_COUNT_MASK)
68
69struct Object;
70struct Thread;
71
72class Monitor {
73 public:
74 ~Monitor();
75
76 static uint32_t GetLockOwner(uint32_t raw_lock_word);
77
78 static void MonitorEnter(Thread* thread, Object* obj);
79 static bool MonitorExit(Thread* thread, Object* obj);
80
81 static void Notify(Thread* self, Object* obj);
82 static void NotifyAll(Thread* self, Object* obj);
83 static void Wait(Thread* self, Object* obj, int64_t ms, int32_t ns, bool interruptShouldThrow);
84
85 static void SweepMonitorList(bool (isUnmarkedObject)(void*));
86
87 static void FreeMonitorList();
88
89 private:
90 Monitor(Object* obj);
91
92 void AppendToWaitSet(Thread* thread);
93 void RemoveFromWaitSet(Thread* thread);
94
95 static void Inflate(Thread* self, Object* obj);
96
97 void Lock(Thread* self);
98 bool Unlock(Thread* thread);
99
100 void Notify(Thread* self);
101 void NotifyAll(Thread* self);
102
103 void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow);
104
105 /* Which thread currently owns the lock? */
106 Thread* owner_;
107
108 /* Owner's recursive lock depth */
109 int lock_count_;
110
111 /* What object are we part of (for debugging). */
112 Object* obj_;
113
114 /* Threads currently waiting on this monitor. */
115 Thread* wait_set_;
116
117 Mutex lock_;
118
119 Monitor* next_;
120
121 /*
122 * Who last acquired this monitor, when lock sampling is enabled.
123 * Even when enabled, ownerFileName may be NULL.
124 */
125 const char* owner_filename_;
126 uint32_t owner_line_number_;
127
128 friend class Object;
129};
130
131/*
132 * Relative timed wait on condition
133 */
134int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex, int64_t msec, int32_t nsec);
135
136} // namespace art
137
138#endif // ART_SRC_SYNC_H_