blob: 9ea407eb6f91c384f61b47267e5d517bf9a138c2 [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
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070063 static void SetVerbose(bool is_verbose);
64
Elliott Hughes5f791332011-09-15 17:45:30 -070065 static uint32_t GetLockOwner(uint32_t raw_lock_word);
66
67 static void MonitorEnter(Thread* thread, Object* obj);
68 static bool MonitorExit(Thread* thread, Object* obj);
69
70 static void Notify(Thread* self, Object* obj);
71 static void NotifyAll(Thread* self, Object* obj);
72 static void Wait(Thread* self, Object* obj, int64_t ms, int32_t ns, bool interruptShouldThrow);
73
74 static void SweepMonitorList(bool (isUnmarkedObject)(void*));
75
76 static void FreeMonitorList();
77
Elliott Hughes8e4aac52011-09-26 17:03:36 -070078 static void DescribeWait(std::ostream& os, const Thread* thread);
79
Elliott Hughes5f791332011-09-15 17:45:30 -070080 private:
81 Monitor(Object* obj);
82
83 void AppendToWaitSet(Thread* thread);
84 void RemoveFromWaitSet(Thread* thread);
85
86 static void Inflate(Thread* self, Object* obj);
87
88 void Lock(Thread* self);
89 bool Unlock(Thread* thread);
90
91 void Notify(Thread* self);
92 void NotifyAll(Thread* self);
93
94 void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow);
95
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070096 static bool is_verbose_;
97
Elliott Hughes5f791332011-09-15 17:45:30 -070098 /* Which thread currently owns the lock? */
99 Thread* owner_;
100
101 /* Owner's recursive lock depth */
102 int lock_count_;
103
104 /* What object are we part of (for debugging). */
105 Object* obj_;
106
107 /* Threads currently waiting on this monitor. */
108 Thread* wait_set_;
109
110 Mutex lock_;
111
112 Monitor* next_;
113
114 /*
115 * Who last acquired this monitor, when lock sampling is enabled.
116 * Even when enabled, ownerFileName may be NULL.
117 */
118 const char* owner_filename_;
119 uint32_t owner_line_number_;
120
121 friend class Object;
122};
123
124/*
125 * Relative timed wait on condition
126 */
127int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex, int64_t msec, int32_t nsec);
128
129} // namespace art
130
Elliott Hughes54e7df12011-09-16 11:47:04 -0700131#endif // ART_SRC_MONITOR_H_