blob: fefc43e04733bc77f170e5866b87dbb4bcd16831 [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
Elliott Hughes8e4aac52011-09-26 17:03:36 -070023#include <iosfwd>
24
Elliott Hughes5f791332011-09-15 17:45:30 -070025#include "mutex.h"
26
27namespace art {
28
29/*
30 * Monitor shape field. Used to distinguish thin locks from fat locks.
31 */
32#define LW_SHAPE_THIN 0
33#define LW_SHAPE_FAT 1
34#define LW_SHAPE_MASK 0x1
35#define LW_SHAPE(x) ((x) & LW_SHAPE_MASK)
36
37/*
38 * Hash state field. Used to signify that an object has had its
39 * identity hash code exposed or relocated.
40 */
41#define LW_HASH_STATE_UNHASHED 0
42#define LW_HASH_STATE_HASHED 1
43#define LW_HASH_STATE_HASHED_AND_MOVED 3
44#define LW_HASH_STATE_MASK 0x3
45#define LW_HASH_STATE_SHIFT 1
46#define LW_HASH_STATE(x) (((x) >> LW_HASH_STATE_SHIFT) & LW_HASH_STATE_MASK)
47
48/*
Elliott Hughes5f791332011-09-15 17:45:30 -070049 * Lock owner field. Contains the thread id of the thread currently
50 * holding the lock.
51 */
52#define LW_LOCK_OWNER_MASK 0xffff
53#define LW_LOCK_OWNER_SHIFT 3
54#define LW_LOCK_OWNER(x) (((x) >> LW_LOCK_OWNER_SHIFT) & LW_LOCK_OWNER_MASK)
55
Elliott Hughes54e7df12011-09-16 11:47:04 -070056class Object;
57class Thread;
Elliott Hughes5f791332011-09-15 17:45:30 -070058
59class Monitor {
60 public:
61 ~Monitor();
62
63 static uint32_t GetLockOwner(uint32_t raw_lock_word);
64
65 static void MonitorEnter(Thread* thread, Object* obj);
66 static bool MonitorExit(Thread* thread, Object* obj);
67
68 static void Notify(Thread* self, Object* obj);
69 static void NotifyAll(Thread* self, Object* obj);
70 static void Wait(Thread* self, Object* obj, int64_t ms, int32_t ns, bool interruptShouldThrow);
71
72 static void SweepMonitorList(bool (isUnmarkedObject)(void*));
73
74 static void FreeMonitorList();
75
Elliott Hughes8e4aac52011-09-26 17:03:36 -070076 static void DescribeWait(std::ostream& os, const Thread* thread);
77
Elliott Hughes5f791332011-09-15 17:45:30 -070078 private:
79 Monitor(Object* obj);
80
81 void AppendToWaitSet(Thread* thread);
82 void RemoveFromWaitSet(Thread* thread);
83
84 static void Inflate(Thread* self, Object* obj);
85
86 void Lock(Thread* self);
87 bool Unlock(Thread* thread);
88
89 void Notify(Thread* self);
90 void NotifyAll(Thread* self);
91
92 void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow);
93
94 /* Which thread currently owns the lock? */
95 Thread* owner_;
96
97 /* Owner's recursive lock depth */
98 int lock_count_;
99
100 /* What object are we part of (for debugging). */
101 Object* obj_;
102
103 /* Threads currently waiting on this monitor. */
104 Thread* wait_set_;
105
106 Mutex lock_;
107
108 Monitor* next_;
109
110 /*
111 * Who last acquired this monitor, when lock sampling is enabled.
112 * Even when enabled, ownerFileName may be NULL.
113 */
114 const char* owner_filename_;
115 uint32_t owner_line_number_;
116
117 friend class Object;
118};
119
120/*
121 * Relative timed wait on condition
122 */
123int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex, int64_t msec, int32_t nsec);
124
125} // namespace art
126
Elliott Hughes54e7df12011-09-16 11:47:04 -0700127#endif // ART_SRC_MONITOR_H_