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.
25 typedef struct Monitor Monitor;
27 #define QUIET_ZYGOTE_MONITOR 1
30 * Synchronization lock, included in every object.
32 * We want this to be a 32-bit "thin lock", holding the lock level and
33 * the owner's threadId, that inflates to a Monitor pointer when there
34 * is contention or somebody waits on it.
42 * Initialize a Lock to the proper starting value.
43 * This is necessary for thin locking.
45 #define THIN_LOCKING 1
47 #define DVM_LOCK_INITIAL_THIN_VALUE (0x1)
49 #define DVM_LOCK_INITIAL_THIN_VALUE (0)
51 #define DVM_LOCK_INIT(lock) \
52 do { (lock)->thin = DVM_LOCK_INITIAL_THIN_VALUE; } while (0)
55 * Returns true if the lock has been fattened.
57 #define IS_LOCK_FAT(lock) (((lock)->thin & 1) == 0 && (lock)->mon != NULL)
60 * Acquire the object's monitor.
62 void dvmLockObject(struct Thread* self, struct Object* obj);
64 /* Returns true if the unlock succeeded.
65 * If the unlock failed, an exception will be pending.
67 bool dvmUnlockObject(struct Thread* self, struct Object* obj);
70 * Implementations of some java/lang/Object calls.
72 void dvmObjectWait(struct Thread* self, struct Object* obj,
73 s8 timeout, s4 nanos, bool interruptShouldThrow);
74 void dvmObjectNotify(struct Thread* self, struct Object* obj);
75 void dvmObjectNotifyAll(struct Thread* self, struct Object* obj);
78 * Implementation of Thread.sleep().
80 void dvmThreadSleep(u8 msec, u4 nsec);
83 * Implementation of Thread.interrupt().
85 * Interrupt a thread. If it's waiting on a monitor, wake it up.
87 void dvmThreadInterrupt(volatile struct Thread* thread);
89 /* create a new Monitor struct */
90 Monitor* dvmCreateMonitor(struct Object* obj);
92 /* free an object's monitor during GC */
93 void dvmFreeObjectMonitor_internal(Lock* lock);
94 #define dvmFreeObjectMonitor(obj) \
96 Object *DFM_obj_ = (obj); \
97 if (IS_LOCK_FAT(&DFM_obj_->lock)) { \
98 dvmFreeObjectMonitor_internal(&DFM_obj_->lock); \
102 /* free monitor list */
103 void dvmFreeMonitorList(void);
106 * Get the object a monitor is part of.
108 * Returns NULL if "mon" is NULL or the monitor is not part of an object
109 * (which should only happen for Thread.sleep() in the current implementation).
111 struct Object* dvmGetMonitorObject(Monitor* mon);
114 * Checks whether the object is held by the specified thread.
116 bool dvmHoldsLock(struct Thread* thread, struct Object* obj);
121 void dvmDumpMonitorInfo(const char* msg);
123 #endif /*_DALVIK_SYNC*/