blob: 520366e2328197d454162b9e09f05ccb4406d514 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 * Object synchronization functions.
18 */
19#ifndef _DALVIK_SYNC
20#define _DALVIK_SYNC
21
Carl Shapiro30aa9972010-01-13 22:07:50 -080022/*
23 * Monitor shape field. Used to distinguish immediate thin locks from
24 * indirecting fat locks.
25 */
Carl Shapiro94338aa2009-12-21 11:42:59 -080026#define LW_SHAPE_THIN 0
27#define LW_SHAPE_FAT 1
28#define LW_SHAPE_MASK 0x1
29#define LW_SHAPE(x) ((x) & LW_SHAPE_MASK)
30
Carl Shapiro30aa9972010-01-13 22:07:50 -080031/*
32 * Hash state field. Used to signify that an object has had its
33 * identity hash code exposed or relocated.
34 */
Carl Shapiro94338aa2009-12-21 11:42:59 -080035#define LW_HASH_STATE_UNHASHED 0
36#define LW_HASH_STATE_HASHED 1
Carl Shapiro30aa9972010-01-13 22:07:50 -080037#define LW_HASH_STATE_HASHED_AND_MOVED 3
Carl Shapiro94338aa2009-12-21 11:42:59 -080038#define LW_HASH_STATE_MASK 0x3
39#define LW_HASH_STATE_SHIFT 1
40#define LW_HASH_STATE(x) (((x) >> LW_HASH_STATE_SHIFT) & LW_HASH_STATE_MASK)
41
Carl Shapiro30aa9972010-01-13 22:07:50 -080042/*
43 * Monitor accessor. Extracts a monitor structure pointer from a fat
44 * lock. Performs no error checking.
45 */
Carl Shapiro94338aa2009-12-21 11:42:59 -080046#define LW_MONITOR(x) \
Carl Shapiro30aa9972010-01-13 22:07:50 -080047 ((Monitor*)((x) & ~((LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT) | \
48 LW_SHAPE_MASK)))
Carl Shapiro94338aa2009-12-21 11:42:59 -080049
Carl Shapiro30aa9972010-01-13 22:07:50 -080050/*
51 * Lock owner field. Contains the thread id of the thread currently
52 * holding the lock.
53 */
Carl Shapiro94338aa2009-12-21 11:42:59 -080054#define LW_LOCK_OWNER_MASK 0xffff
55#define LW_LOCK_OWNER_SHIFT 3
56#define LW_LOCK_OWNER(x) (((x) >> LW_LOCK_OWNER_SHIFT) & LW_LOCK_OWNER_MASK)
57
Carl Shapiro30aa9972010-01-13 22:07:50 -080058/*
59 * Lock recursion count field. Contains a count of the numer of times
60 * a lock has been recursively acquired.
61 */
Carl Shapiro94338aa2009-12-21 11:42:59 -080062#define LW_LOCK_COUNT_MASK 0x1fff
63#define LW_LOCK_COUNT_SHIFT 19
64#define LW_LOCK_COUNT(x) (((x) >> LW_LOCK_COUNT_SHIFT) & LW_LOCK_COUNT_MASK)
65
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080066struct Object;
67struct Monitor;
68struct Thread;
69typedef struct Monitor Monitor;
70
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080071/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080072 * Initialize a Lock to the proper starting value.
73 * This is necessary for thin locking.
74 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080075#define DVM_LOCK_INITIAL_THIN_VALUE (0)
Carl Shapiro94338aa2009-12-21 11:42:59 -080076
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080077#define DVM_LOCK_INIT(lock) \
Carl Shapiro8d7f9b22009-12-21 20:23:45 -080078 do { *(lock) = DVM_LOCK_INITIAL_THIN_VALUE; } while (0)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080079
80/*
81 * Returns true if the lock has been fattened.
82 */
Carl Shapiro8d7f9b22009-12-21 20:23:45 -080083#define IS_LOCK_FAT(lock) (LW_SHAPE(*(lock)) == LW_SHAPE_FAT)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080084
85/*
86 * Acquire the object's monitor.
87 */
88void dvmLockObject(struct Thread* self, struct Object* obj);
89
90/* Returns true if the unlock succeeded.
91 * If the unlock failed, an exception will be pending.
92 */
93bool dvmUnlockObject(struct Thread* self, struct Object* obj);
94
95/*
96 * Implementations of some java/lang/Object calls.
97 */
98void dvmObjectWait(struct Thread* self, struct Object* obj,
99 s8 timeout, s4 nanos, bool interruptShouldThrow);
100void dvmObjectNotify(struct Thread* self, struct Object* obj);
101void dvmObjectNotifyAll(struct Thread* self, struct Object* obj);
102
103/*
Carl Shapiro94338aa2009-12-21 11:42:59 -0800104 * Implementation of System.identityHashCode().
105 */
106u4 dvmIdentityHashCode(struct Object* obj);
107
108/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800109 * Implementation of Thread.sleep().
110 */
111void dvmThreadSleep(u8 msec, u4 nsec);
112
113/*
114 * Implementation of Thread.interrupt().
115 *
116 * Interrupt a thread. If it's waiting on a monitor, wake it up.
117 */
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800118void dvmThreadInterrupt(struct Thread* thread);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800119
120/* create a new Monitor struct */
121Monitor* dvmCreateMonitor(struct Object* obj);
122
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800123/*
124 * Frees unmarked monitors from the monitor list. The given callback
125 * routine should return a non-zero value when passed a pointer to an
126 * unmarked object.
127 */
128void dvmSweepMonitorList(Monitor** mon, int (*isUnmarkedObject)(void*));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800129
130/* free monitor list */
131void dvmFreeMonitorList(void);
132
133/*
134 * Get the object a monitor is part of.
135 *
136 * Returns NULL if "mon" is NULL or the monitor is not part of an object
137 * (which should only happen for Thread.sleep() in the current implementation).
138 */
139struct Object* dvmGetMonitorObject(Monitor* mon);
140
141/*
Andy McFadden0a24ef92010-03-12 13:39:59 -0800142 * Get the thread that holds the lock on the specified object. The
143 * object may be unlocked, thin-locked, or fat-locked.
144 *
145 * The caller must lock the thread list before calling here.
146 */
147struct Thread* dvmGetObjectLockHolder(struct Object* obj);
148
149/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800150 * Checks whether the object is held by the specified thread.
151 */
152bool dvmHoldsLock(struct Thread* thread, struct Object* obj);
153
154/*
Bill Buzbeeeb695c62010-02-04 16:09:55 -0800155 * Relative timed wait on condition
Bill Buzbee94d89f82010-01-29 13:44:19 -0800156 */
Bill Buzbeeeb695c62010-02-04 16:09:55 -0800157int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex,
158 s8 msec, s4 nsec);
Bill Buzbee94d89f82010-01-29 13:44:19 -0800159
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800160#endif /*_DALVIK_SYNC*/