2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Object synchronization functions.
19 #ifndef DALVIK_SYNC_H_
20 #define DALVIK_SYNC_H_
23 * Monitor shape field. Used to distinguish immediate thin locks from
24 * indirecting fat locks.
26 #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)
32 * Hash state field. Used to signify that an object has had its
33 * identity hash code exposed or relocated.
35 #define LW_HASH_STATE_UNHASHED 0
36 #define LW_HASH_STATE_HASHED 1
37 #define LW_HASH_STATE_HASHED_AND_MOVED 3
38 #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)
43 * Monitor accessor. Extracts a monitor structure pointer from a fat
44 * lock. Performs no error checking.
46 #define LW_MONITOR(x) \
47 ((Monitor*)((x) & ~((LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT) | \
51 * Lock owner field. Contains the thread id of the thread currently
54 #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)
59 * Lock recursion count field. Contains a count of the numer of times
60 * a lock has been recursively acquired.
62 #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)
71 * Returns true if the lock has been fattened.
73 #define IS_LOCK_FAT(lock) (LW_SHAPE(*(lock)) == LW_SHAPE_FAT)
76 * Acquire the object's monitor.
78 extern "C" void dvmLockObject(Thread* self, Object* obj);
80 /* Returns true if the unlock succeeded.
81 * If the unlock failed, an exception will be pending.
83 extern "C" bool dvmUnlockObject(Thread* self, Object* obj);
86 * Implementations of some java/lang/Object calls.
88 void dvmObjectWait(Thread* self, Object* obj,
89 s8 timeout, s4 nanos, bool interruptShouldThrow);
90 void dvmObjectNotify(Thread* self, Object* obj);
91 void dvmObjectNotifyAll(Thread* self, Object* obj);
94 * Implementation of System.identityHashCode().
96 u4 dvmIdentityHashCode(Object* obj);
99 * Implementation of Thread.sleep().
101 void dvmThreadSleep(u8 msec, u4 nsec);
104 * Implementation of Thread.interrupt().
106 * Interrupt a thread. If it's waiting on a monitor, wake it up.
108 void dvmThreadInterrupt(Thread* thread);
110 /* create a new Monitor struct */
111 Monitor* dvmCreateMonitor(Object* obj);
114 * Frees unmarked monitors from the monitor list. The given callback
115 * routine should return a non-zero value when passed a pointer to an
118 void dvmSweepMonitorList(Monitor** mon, int (*isUnmarkedObject)(void*));
120 /* free monitor list */
121 void dvmFreeMonitorList(void);
124 * Get the object a monitor is part of.
126 * Returns NULL if "mon" is NULL or the monitor is not part of an object
127 * (which should only happen for Thread.sleep() in the current implementation).
129 Object* dvmGetMonitorObject(Monitor* mon);
132 * Get the thread that holds the lock on the specified object. The
133 * object may be unlocked, thin-locked, or fat-locked.
135 * The caller must lock the thread list before calling here.
137 Thread* dvmGetObjectLockHolder(Object* obj);
140 * Checks whether the object is held by the specified thread.
142 bool dvmHoldsLock(Thread* thread, Object* obj);
145 * Relative timed wait on condition
147 int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex,
150 #endif // DALVIK_SYNC_H_