blob: 8c22fb12358e054e64edd4c2b2f3df27c045ead0 [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
Elliott Hughes54e7df12011-09-16 11:47:04 -070017#ifndef ART_SRC_MONITOR_H_
18#define ART_SRC_MONITOR_H_
Elliott Hughes5f791332011-09-15 17:45:30 -070019
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/*
Elliott Hughes5f791332011-09-15 17:45:30 -070047 * Lock owner field. Contains the thread id of the thread currently
48 * holding the lock.
49 */
50#define LW_LOCK_OWNER_MASK 0xffff
51#define LW_LOCK_OWNER_SHIFT 3
52#define LW_LOCK_OWNER(x) (((x) >> LW_LOCK_OWNER_SHIFT) & LW_LOCK_OWNER_MASK)
53
Elliott Hughes54e7df12011-09-16 11:47:04 -070054class Object;
55class Thread;
Elliott Hughes5f791332011-09-15 17:45:30 -070056
57class Monitor {
58 public:
59 ~Monitor();
60
61 static uint32_t GetLockOwner(uint32_t raw_lock_word);
62
63 static void MonitorEnter(Thread* thread, Object* obj);
64 static bool MonitorExit(Thread* thread, Object* obj);
65
66 static void Notify(Thread* self, Object* obj);
67 static void NotifyAll(Thread* self, Object* obj);
68 static void Wait(Thread* self, Object* obj, int64_t ms, int32_t ns, bool interruptShouldThrow);
69
70 static void SweepMonitorList(bool (isUnmarkedObject)(void*));
71
72 static void FreeMonitorList();
73
74 private:
75 Monitor(Object* obj);
76
77 void AppendToWaitSet(Thread* thread);
78 void RemoveFromWaitSet(Thread* thread);
79
80 static void Inflate(Thread* self, Object* obj);
81
82 void Lock(Thread* self);
83 bool Unlock(Thread* thread);
84
85 void Notify(Thread* self);
86 void NotifyAll(Thread* self);
87
88 void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow);
89
90 /* Which thread currently owns the lock? */
91 Thread* owner_;
92
93 /* Owner's recursive lock depth */
94 int lock_count_;
95
96 /* What object are we part of (for debugging). */
97 Object* obj_;
98
99 /* Threads currently waiting on this monitor. */
100 Thread* wait_set_;
101
102 Mutex lock_;
103
104 Monitor* next_;
105
106 /*
107 * Who last acquired this monitor, when lock sampling is enabled.
108 * Even when enabled, ownerFileName may be NULL.
109 */
110 const char* owner_filename_;
111 uint32_t owner_line_number_;
112
113 friend class Object;
114};
115
116/*
117 * Relative timed wait on condition
118 */
119int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex, int64_t msec, int32_t nsec);
120
121} // namespace art
122
Elliott Hughes54e7df12011-09-16 11:47:04 -0700123#endif // ART_SRC_MONITOR_H_