OSDN Git Service

Further conservation of newlines.
[android-x86/dalvik.git] / vm / Sync.cpp
1 /*
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 #include "Dalvik.h"
18
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <pthread.h>
23 #include <time.h>
24 #include <errno.h>
25
26 /*
27  * Every Object has a monitor associated with it, but not every Object is
28  * actually locked.  Even the ones that are locked do not need a
29  * full-fledged monitor until a) there is actual contention or b) wait()
30  * is called on the Object.
31  *
32  * For Dalvik, we have implemented a scheme similar to the one described
33  * in Bacon et al.'s "Thin locks: featherweight synchronization for Java"
34  * (ACM 1998).  Things are even easier for us, though, because we have
35  * a full 32 bits to work with.
36  *
37  * The two states of an Object's lock are referred to as "thin" and
38  * "fat".  A lock may transition from the "thin" state to the "fat"
39  * state and this transition is referred to as inflation.  Once a lock
40  * has been inflated it remains in the "fat" state indefinitely.
41  *
42  * The lock value itself is stored in Object.lock.  The LSB of the
43  * lock encodes its state.  When cleared, the lock is in the "thin"
44  * state and its bits are formatted as follows:
45  *
46  *    [31 ---- 19] [18 ---- 3] [2 ---- 1] [0]
47  *     lock count   thread id  hash state  0
48  *
49  * When set, the lock is in the "fat" state and its bits are formatted
50  * as follows:
51  *
52  *    [31 ---- 3] [2 ---- 1] [0]
53  *      pointer   hash state  1
54  *
55  * For an in-depth description of the mechanics of thin-vs-fat locking,
56  * read the paper referred to above.
57  */
58
59 /*
60  * Monitors provide:
61  *  - mutually exclusive access to resources
62  *  - a way for multiple threads to wait for notification
63  *
64  * In effect, they fill the role of both mutexes and condition variables.
65  *
66  * Only one thread can own the monitor at any time.  There may be several
67  * threads waiting on it (the wait call unlocks it).  One or more waiting
68  * threads may be getting interrupted or notified at any given time.
69  *
70  * TODO: the various members of monitor are not SMP-safe.
71  */
72 struct Monitor {
73     Thread*     owner;          /* which thread currently owns the lock? */
74     int         lockCount;      /* owner's recursive lock depth */
75     Object*     obj;            /* what object are we part of [debug only] */
76
77     Thread*     waitSet;        /* threads currently waiting on this monitor */
78
79     pthread_mutex_t lock;
80
81     Monitor*    next;
82
83     /*
84      * Who last acquired this monitor, when lock sampling is enabled.
85      * Even when enabled, ownerFileName may be NULL.
86      */
87     const char* ownerFileName;
88     u4          ownerLineNumber;
89 };
90
91
92 /*
93  * Create and initialize a monitor.
94  */
95 Monitor* dvmCreateMonitor(Object* obj)
96 {
97     Monitor* mon;
98
99     mon = (Monitor*) calloc(1, sizeof(Monitor));
100     if (mon == NULL) {
101         LOGE("Unable to allocate monitor");
102         dvmAbort();
103     }
104     if (((u4)mon & 7) != 0) {
105         LOGE("Misaligned monitor: %p", mon);
106         dvmAbort();
107     }
108     mon->obj = obj;
109     dvmInitMutex(&mon->lock);
110
111     /* replace the head of the list with the new monitor */
112     do {
113         mon->next = gDvm.monitorList;
114     } while (android_atomic_release_cas((int32_t)mon->next, (int32_t)mon,
115             (int32_t*)(void*)&gDvm.monitorList) != 0);
116
117     return mon;
118 }
119
120 /*
121  * Free the monitor list.  Only used when shutting the VM down.
122  */
123 void dvmFreeMonitorList()
124 {
125     Monitor* mon;
126     Monitor* nextMon;
127
128     mon = gDvm.monitorList;
129     while (mon != NULL) {
130         nextMon = mon->next;
131         free(mon);
132         mon = nextMon;
133     }
134 }
135
136 /*
137  * Get the object that a monitor is part of.
138  */
139 Object* dvmGetMonitorObject(Monitor* mon)
140 {
141     if (mon == NULL)
142         return NULL;
143     else
144         return mon->obj;
145 }
146
147 /*
148  * Returns the thread id of the thread owning the given lock.
149  */
150 static u4 lockOwner(Object* obj)
151 {
152     Thread *owner;
153     u4 lock;
154
155     assert(obj != NULL);
156     /*
157      * Since we're reading the lock value multiple times, latch it so
158      * that it doesn't change out from under us if we get preempted.
159      */
160     lock = obj->lock;
161     if (LW_SHAPE(lock) == LW_SHAPE_THIN) {
162         return LW_LOCK_OWNER(lock);
163     } else {
164         owner = LW_MONITOR(lock)->owner;
165         return owner ? owner->threadId : 0;
166     }
167 }
168
169 /*
170  * Get the thread that holds the lock on the specified object.  The
171  * object may be unlocked, thin-locked, or fat-locked.
172  *
173  * The caller must lock the thread list before calling here.
174  */
175 Thread* dvmGetObjectLockHolder(Object* obj)
176 {
177     u4 threadId = lockOwner(obj);
178
179     if (threadId == 0)
180         return NULL;
181     return dvmGetThreadByThreadId(threadId);
182 }
183
184 /*
185  * Checks whether the given thread holds the given
186  * objects's lock.
187  */
188 bool dvmHoldsLock(Thread* thread, Object* obj)
189 {
190     if (thread == NULL || obj == NULL) {
191         return false;
192     } else {
193         return thread->threadId == lockOwner(obj);
194     }
195 }
196
197 /*
198  * Free the monitor associated with an object and make the object's lock
199  * thin again.  This is called during garbage collection.
200  */
201 static void freeMonitor(Monitor *mon)
202 {
203     assert(mon != NULL);
204     assert(mon->obj != NULL);
205     assert(LW_SHAPE(mon->obj->lock) == LW_SHAPE_FAT);
206
207     /* This lock is associated with an object
208      * that's being swept.  The only possible way
209      * anyone could be holding this lock would be
210      * if some JNI code locked but didn't unlock
211      * the object, in which case we've got some bad
212      * native code somewhere.
213      */
214     assert(pthread_mutex_trylock(&mon->lock) == 0);
215     assert(pthread_mutex_unlock(&mon->lock) == 0);
216     dvmDestroyMutex(&mon->lock);
217     free(mon);
218 }
219
220 /*
221  * Frees monitor objects belonging to unmarked objects.
222  */
223 void dvmSweepMonitorList(Monitor** mon, int (*isUnmarkedObject)(void*))
224 {
225     Monitor handle;
226     Monitor *prev, *curr;
227     Object *obj;
228
229     assert(mon != NULL);
230     assert(isUnmarkedObject != NULL);
231     prev = &handle;
232     prev->next = curr = *mon;
233     while (curr != NULL) {
234         obj = curr->obj;
235         if (obj != NULL && (*isUnmarkedObject)(obj) != 0) {
236             prev->next = curr->next;
237             freeMonitor(curr);
238             curr = prev->next;
239         } else {
240             prev = curr;
241             curr = curr->next;
242         }
243     }
244     *mon = handle.next;
245 }
246
247 static char *logWriteInt(char *dst, int value)
248 {
249     *dst++ = EVENT_TYPE_INT;
250     set4LE((u1 *)dst, value);
251     return dst + 4;
252 }
253
254 static char *logWriteString(char *dst, const char *value, size_t len)
255 {
256     *dst++ = EVENT_TYPE_STRING;
257     len = len < 32 ? len : 32;
258     set4LE((u1 *)dst, len);
259     dst += 4;
260     memcpy(dst, value, len);
261     return dst + len;
262 }
263
264 #define EVENT_LOG_TAG_dvm_lock_sample 20003
265
266 static void logContentionEvent(Thread *self, u4 waitMs, u4 samplePercent,
267                                const char *ownerFileName, u4 ownerLineNumber)
268 {
269     const StackSaveArea *saveArea;
270     const Method *meth;
271     u4 relativePc;
272     char eventBuffer[174];
273     const char *fileName;
274     char procName[33], *selfName;
275     char *cp;
276     size_t len;
277     int fd;
278
279     saveArea = SAVEAREA_FROM_FP(self->interpSave.curFrame);
280     meth = saveArea->method;
281     cp = eventBuffer;
282
283     /* Emit the event list length, 1 byte. */
284     *cp++ = 9;
285
286     /* Emit the process name, <= 37 bytes. */
287     fd = open("/proc/self/cmdline", O_RDONLY);
288     memset(procName, 0, sizeof(procName));
289     read(fd, procName, sizeof(procName) - 1);
290     close(fd);
291     len = strlen(procName);
292     cp = logWriteString(cp, procName, len);
293
294     /* Emit the sensitive thread ("main thread") status, 5 bytes. */
295     bool isSensitive = false;
296     if (gDvm.isSensitiveThreadHook != NULL) {
297         isSensitive = gDvm.isSensitiveThreadHook();
298     }
299     cp = logWriteInt(cp, isSensitive);
300
301     /* Emit self thread name string, <= 37 bytes. */
302     selfName = dvmGetThreadName(self);
303     cp = logWriteString(cp, selfName, strlen(selfName));
304     free(selfName);
305
306     /* Emit the wait time, 5 bytes. */
307     cp = logWriteInt(cp, waitMs);
308
309     /* Emit the source code file name, <= 37 bytes. */
310     fileName = dvmGetMethodSourceFile(meth);
311     if (fileName == NULL) fileName = "";
312     cp = logWriteString(cp, fileName, strlen(fileName));
313
314     /* Emit the source code line number, 5 bytes. */
315     relativePc = saveArea->xtra.currentPc - saveArea->method->insns;
316     cp = logWriteInt(cp, dvmLineNumFromPC(meth, relativePc));
317
318     /* Emit the lock owner source code file name, <= 37 bytes. */
319     if (ownerFileName == NULL) {
320         ownerFileName = "";
321     } else if (strcmp(fileName, ownerFileName) == 0) {
322         /* Common case, so save on log space. */
323         ownerFileName = "-";
324     }
325     cp = logWriteString(cp, ownerFileName, strlen(ownerFileName));
326
327     /* Emit the source code line number, 5 bytes. */
328     cp = logWriteInt(cp, ownerLineNumber);
329
330     /* Emit the sample percentage, 5 bytes. */
331     cp = logWriteInt(cp, samplePercent);
332
333     assert((size_t)(cp - eventBuffer) <= sizeof(eventBuffer));
334     android_btWriteLog(EVENT_LOG_TAG_dvm_lock_sample,
335                        EVENT_TYPE_LIST,
336                        eventBuffer,
337                        (size_t)(cp - eventBuffer));
338 }
339
340 /*
341  * Lock a monitor.
342  */
343 static void lockMonitor(Thread* self, Monitor* mon)
344 {
345     ThreadStatus oldStatus;
346     u4 waitThreshold, samplePercent;
347     u8 waitStart, waitEnd, waitMs;
348
349     if (mon->owner == self) {
350         mon->lockCount++;
351         return;
352     }
353     if (dvmTryLockMutex(&mon->lock) != 0) {
354         oldStatus = dvmChangeStatus(self, THREAD_MONITOR);
355         waitThreshold = gDvm.lockProfThreshold;
356         if (waitThreshold) {
357             waitStart = dvmGetRelativeTimeUsec();
358         }
359         const char* currentOwnerFileName = mon->ownerFileName;
360         u4 currentOwnerLineNumber = mon->ownerLineNumber;
361
362         dvmLockMutex(&mon->lock);
363         if (waitThreshold) {
364             waitEnd = dvmGetRelativeTimeUsec();
365         }
366         dvmChangeStatus(self, oldStatus);
367         if (waitThreshold) {
368             waitMs = (waitEnd - waitStart) / 1000;
369             if (waitMs >= waitThreshold) {
370                 samplePercent = 100;
371             } else {
372                 samplePercent = 100 * waitMs / waitThreshold;
373             }
374             if (samplePercent != 0 && ((u4)rand() % 100 < samplePercent)) {
375                 logContentionEvent(self, waitMs, samplePercent,
376                                    currentOwnerFileName, currentOwnerLineNumber);
377             }
378         }
379     }
380     mon->owner = self;
381     assert(mon->lockCount == 0);
382
383     // When debugging, save the current monitor holder for future
384     // acquisition failures to use in sampled logging.
385     if (gDvm.lockProfThreshold > 0) {
386         const StackSaveArea *saveArea;
387         const Method *meth;
388         mon->ownerLineNumber = 0;
389         if (self->interpSave.curFrame == NULL) {
390             mon->ownerFileName = "no_frame";
391         } else if ((saveArea =
392                    SAVEAREA_FROM_FP(self->interpSave.curFrame)) == NULL) {
393             mon->ownerFileName = "no_save_area";
394         } else if ((meth = saveArea->method) == NULL) {
395             mon->ownerFileName = "no_method";
396         } else {
397             u4 relativePc = saveArea->xtra.currentPc - saveArea->method->insns;
398             mon->ownerFileName = (char*) dvmGetMethodSourceFile(meth);
399             if (mon->ownerFileName == NULL) {
400                 mon->ownerFileName = "no_method_file";
401             } else {
402                 mon->ownerLineNumber = dvmLineNumFromPC(meth, relativePc);
403             }
404         }
405     }
406 }
407
408 /*
409  * Try to lock a monitor.
410  *
411  * Returns "true" on success.
412  */
413 #ifdef WITH_COPYING_GC
414 static bool tryLockMonitor(Thread* self, Monitor* mon)
415 {
416     if (mon->owner == self) {
417         mon->lockCount++;
418         return true;
419     } else {
420         if (dvmTryLockMutex(&mon->lock) == 0) {
421             mon->owner = self;
422             assert(mon->lockCount == 0);
423             return true;
424         } else {
425             return false;
426         }
427     }
428 }
429 #endif
430
431 /*
432  * Unlock a monitor.
433  *
434  * Returns true if the unlock succeeded.
435  * If the unlock failed, an exception will be pending.
436  */
437 static bool unlockMonitor(Thread* self, Monitor* mon)
438 {
439     assert(self != NULL);
440     assert(mon != NULL);
441     if (mon->owner == self) {
442         /*
443          * We own the monitor, so nobody else can be in here.
444          */
445         if (mon->lockCount == 0) {
446             mon->owner = NULL;
447             mon->ownerFileName = "unlocked";
448             mon->ownerLineNumber = 0;
449             dvmUnlockMutex(&mon->lock);
450         } else {
451             mon->lockCount--;
452         }
453     } else {
454         /*
455          * We don't own this, so we're not allowed to unlock it.
456          * The JNI spec says that we should throw IllegalMonitorStateException
457          * in this case.
458          */
459         dvmThrowIllegalMonitorStateException("unlock of unowned monitor");
460         return false;
461     }
462     return true;
463 }
464
465 /*
466  * Checks the wait set for circular structure.  Returns 0 if the list
467  * is not circular.  Otherwise, returns 1.  Used only by asserts.
468  */
469 #ifndef NDEBUG
470 static int waitSetCheck(Monitor *mon)
471 {
472     Thread *fast, *slow;
473     size_t n;
474
475     assert(mon != NULL);
476     fast = slow = mon->waitSet;
477     n = 0;
478     for (;;) {
479         if (fast == NULL) return 0;
480         if (fast->waitNext == NULL) return 0;
481         if (fast == slow && n > 0) return 1;
482         n += 2;
483         fast = fast->waitNext->waitNext;
484         slow = slow->waitNext;
485     }
486 }
487 #endif
488
489 /*
490  * Links a thread into a monitor's wait set.  The monitor lock must be
491  * held by the caller of this routine.
492  */
493 static void waitSetAppend(Monitor *mon, Thread *thread)
494 {
495     Thread *elt;
496
497     assert(mon != NULL);
498     assert(mon->owner == dvmThreadSelf());
499     assert(thread != NULL);
500     assert(thread->waitNext == NULL);
501     assert(waitSetCheck(mon) == 0);
502     if (mon->waitSet == NULL) {
503         mon->waitSet = thread;
504         return;
505     }
506     elt = mon->waitSet;
507     while (elt->waitNext != NULL) {
508         elt = elt->waitNext;
509     }
510     elt->waitNext = thread;
511 }
512
513 /*
514  * Unlinks a thread from a monitor's wait set.  The monitor lock must
515  * be held by the caller of this routine.
516  */
517 static void waitSetRemove(Monitor *mon, Thread *thread)
518 {
519     Thread *elt;
520
521     assert(mon != NULL);
522     assert(mon->owner == dvmThreadSelf());
523     assert(thread != NULL);
524     assert(waitSetCheck(mon) == 0);
525     if (mon->waitSet == NULL) {
526         return;
527     }
528     if (mon->waitSet == thread) {
529         mon->waitSet = thread->waitNext;
530         thread->waitNext = NULL;
531         return;
532     }
533     elt = mon->waitSet;
534     while (elt->waitNext != NULL) {
535         if (elt->waitNext == thread) {
536             elt->waitNext = thread->waitNext;
537             thread->waitNext = NULL;
538             return;
539         }
540         elt = elt->waitNext;
541     }
542 }
543
544 /*
545  * Converts the given relative waiting time into an absolute time.
546  */
547 static void absoluteTime(s8 msec, s4 nsec, struct timespec *ts)
548 {
549     s8 endSec;
550
551 #ifdef HAVE_TIMEDWAIT_MONOTONIC
552     clock_gettime(CLOCK_MONOTONIC, ts);
553 #else
554     {
555         struct timeval tv;
556         gettimeofday(&tv, NULL);
557         ts->tv_sec = tv.tv_sec;
558         ts->tv_nsec = tv.tv_usec * 1000;
559     }
560 #endif
561     endSec = ts->tv_sec + msec / 1000;
562     if (endSec >= 0x7fffffff) {
563         LOGV("NOTE: end time exceeds epoch");
564         endSec = 0x7ffffffe;
565     }
566     ts->tv_sec = endSec;
567     ts->tv_nsec = (ts->tv_nsec + (msec % 1000) * 1000000) + nsec;
568
569     /* catch rollover */
570     if (ts->tv_nsec >= 1000000000L) {
571         ts->tv_sec++;
572         ts->tv_nsec -= 1000000000L;
573     }
574 }
575
576 int dvmRelativeCondWait(pthread_cond_t* cond, pthread_mutex_t* mutex,
577                         s8 msec, s4 nsec)
578 {
579     int ret;
580     struct timespec ts;
581     absoluteTime(msec, nsec, &ts);
582 #if defined(HAVE_TIMEDWAIT_MONOTONIC)
583     ret = pthread_cond_timedwait_monotonic(cond, mutex, &ts);
584 #else
585     ret = pthread_cond_timedwait(cond, mutex, &ts);
586 #endif
587     assert(ret == 0 || ret == ETIMEDOUT);
588     return ret;
589 }
590
591 /*
592  * Wait on a monitor until timeout, interrupt, or notification.  Used for
593  * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
594  *
595  * If another thread calls Thread.interrupt(), we throw InterruptedException
596  * and return immediately if one of the following are true:
597  *  - blocked in wait(), wait(long), or wait(long, int) methods of Object
598  *  - blocked in join(), join(long), or join(long, int) methods of Thread
599  *  - blocked in sleep(long), or sleep(long, int) methods of Thread
600  * Otherwise, we set the "interrupted" flag.
601  *
602  * Checks to make sure that "nsec" is in the range 0-999999
603  * (i.e. fractions of a millisecond) and throws the appropriate
604  * exception if it isn't.
605  *
606  * The spec allows "spurious wakeups", and recommends that all code using
607  * Object.wait() do so in a loop.  This appears to derive from concerns
608  * about pthread_cond_wait() on multiprocessor systems.  Some commentary
609  * on the web casts doubt on whether these can/should occur.
610  *
611  * Since we're allowed to wake up "early", we clamp extremely long durations
612  * to return at the end of the 32-bit time epoch.
613  */
614 static void waitMonitor(Thread* self, Monitor* mon, s8 msec, s4 nsec,
615     bool interruptShouldThrow)
616 {
617     struct timespec ts;
618     bool wasInterrupted = false;
619     bool timed;
620     int ret;
621     const char *savedFileName;
622     u4 savedLineNumber;
623
624     assert(self != NULL);
625     assert(mon != NULL);
626
627     /* Make sure that we hold the lock. */
628     if (mon->owner != self) {
629         dvmThrowIllegalMonitorStateException(
630             "object not locked by thread before wait()");
631         return;
632     }
633
634     /*
635      * Enforce the timeout range.
636      */
637     if (msec < 0 || nsec < 0 || nsec > 999999) {
638         dvmThrowIllegalArgumentException("timeout arguments out of range");
639         return;
640     }
641
642     /*
643      * Compute absolute wakeup time, if necessary.
644      */
645     if (msec == 0 && nsec == 0) {
646         timed = false;
647     } else {
648         absoluteTime(msec, nsec, &ts);
649         timed = true;
650     }
651
652     /*
653      * Add ourselves to the set of threads waiting on this monitor, and
654      * release our hold.  We need to let it go even if we're a few levels
655      * deep in a recursive lock, and we need to restore that later.
656      *
657      * We append to the wait set ahead of clearing the count and owner
658      * fields so the subroutine can check that the calling thread owns
659      * the monitor.  Aside from that, the order of member updates is
660      * not order sensitive as we hold the pthread mutex.
661      */
662     waitSetAppend(mon, self);
663     int prevLockCount = mon->lockCount;
664     mon->lockCount = 0;
665     mon->owner = NULL;
666     savedFileName = mon->ownerFileName;
667     mon->ownerFileName = NULL;
668     savedLineNumber = mon->ownerLineNumber;
669     mon->ownerLineNumber = 0;
670
671     /*
672      * Update thread status.  If the GC wakes up, it'll ignore us, knowing
673      * that we won't touch any references in this state, and we'll check
674      * our suspend mode before we transition out.
675      */
676     if (timed)
677         dvmChangeStatus(self, THREAD_TIMED_WAIT);
678     else
679         dvmChangeStatus(self, THREAD_WAIT);
680
681     dvmLockMutex(&self->waitMutex);
682
683     /*
684      * Set waitMonitor to the monitor object we will be waiting on.
685      * When waitMonitor is non-NULL a notifying or interrupting thread
686      * must signal the thread's waitCond to wake it up.
687      */
688     assert(self->waitMonitor == NULL);
689     self->waitMonitor = mon;
690
691     /*
692      * Handle the case where the thread was interrupted before we called
693      * wait().
694      */
695     if (self->interrupted) {
696         wasInterrupted = true;
697         self->waitMonitor = NULL;
698         dvmUnlockMutex(&self->waitMutex);
699         goto done;
700     }
701
702     /*
703      * Release the monitor lock and wait for a notification or
704      * a timeout to occur.
705      */
706     dvmUnlockMutex(&mon->lock);
707
708     if (!timed) {
709         ret = pthread_cond_wait(&self->waitCond, &self->waitMutex);
710         assert(ret == 0);
711     } else {
712 #ifdef HAVE_TIMEDWAIT_MONOTONIC
713         ret = pthread_cond_timedwait_monotonic(&self->waitCond, &self->waitMutex, &ts);
714 #else
715         ret = pthread_cond_timedwait(&self->waitCond, &self->waitMutex, &ts);
716 #endif
717         assert(ret == 0 || ret == ETIMEDOUT);
718     }
719     if (self->interrupted) {
720         wasInterrupted = true;
721     }
722
723     self->interrupted = false;
724     self->waitMonitor = NULL;
725
726     dvmUnlockMutex(&self->waitMutex);
727
728     /* Reacquire the monitor lock. */
729     lockMonitor(self, mon);
730
731 done:
732     /*
733      * We remove our thread from wait set after restoring the count
734      * and owner fields so the subroutine can check that the calling
735      * thread owns the monitor. Aside from that, the order of member
736      * updates is not order sensitive as we hold the pthread mutex.
737      */
738     mon->owner = self;
739     mon->lockCount = prevLockCount;
740     mon->ownerFileName = savedFileName;
741     mon->ownerLineNumber = savedLineNumber;
742     waitSetRemove(mon, self);
743
744     /* set self->status back to THREAD_RUNNING, and self-suspend if needed */
745     dvmChangeStatus(self, THREAD_RUNNING);
746
747     if (wasInterrupted) {
748         /*
749          * We were interrupted while waiting, or somebody interrupted an
750          * un-interruptible thread earlier and we're bailing out immediately.
751          *
752          * The doc sayeth: "The interrupted status of the current thread is
753          * cleared when this exception is thrown."
754          */
755         self->interrupted = false;
756         if (interruptShouldThrow) {
757             dvmThrowInterruptedException(NULL);
758         }
759     }
760 }
761
762 /*
763  * Notify one thread waiting on this monitor.
764  */
765 static void notifyMonitor(Thread* self, Monitor* mon)
766 {
767     Thread* thread;
768
769     assert(self != NULL);
770     assert(mon != NULL);
771
772     /* Make sure that we hold the lock. */
773     if (mon->owner != self) {
774         dvmThrowIllegalMonitorStateException(
775             "object not locked by thread before notify()");
776         return;
777     }
778     /* Signal the first waiting thread in the wait set. */
779     while (mon->waitSet != NULL) {
780         thread = mon->waitSet;
781         mon->waitSet = thread->waitNext;
782         thread->waitNext = NULL;
783         dvmLockMutex(&thread->waitMutex);
784         /* Check to see if the thread is still waiting. */
785         if (thread->waitMonitor != NULL) {
786             pthread_cond_signal(&thread->waitCond);
787             dvmUnlockMutex(&thread->waitMutex);
788             return;
789         }
790         dvmUnlockMutex(&thread->waitMutex);
791     }
792 }
793
794 /*
795  * Notify all threads waiting on this monitor.
796  */
797 static void notifyAllMonitor(Thread* self, Monitor* mon)
798 {
799     Thread* thread;
800
801     assert(self != NULL);
802     assert(mon != NULL);
803
804     /* Make sure that we hold the lock. */
805     if (mon->owner != self) {
806         dvmThrowIllegalMonitorStateException(
807             "object not locked by thread before notifyAll()");
808         return;
809     }
810     /* Signal all threads in the wait set. */
811     while (mon->waitSet != NULL) {
812         thread = mon->waitSet;
813         mon->waitSet = thread->waitNext;
814         thread->waitNext = NULL;
815         dvmLockMutex(&thread->waitMutex);
816         /* Check to see if the thread is still waiting. */
817         if (thread->waitMonitor != NULL) {
818             pthread_cond_signal(&thread->waitCond);
819         }
820         dvmUnlockMutex(&thread->waitMutex);
821     }
822 }
823
824 /*
825  * Changes the shape of a monitor from thin to fat, preserving the
826  * internal lock state.  The calling thread must own the lock.
827  */
828 static void inflateMonitor(Thread *self, Object *obj)
829 {
830     Monitor *mon;
831     u4 thin;
832
833     assert(self != NULL);
834     assert(obj != NULL);
835     assert(LW_SHAPE(obj->lock) == LW_SHAPE_THIN);
836     assert(LW_LOCK_OWNER(obj->lock) == self->threadId);
837     /* Allocate and acquire a new monitor. */
838     mon = dvmCreateMonitor(obj);
839     lockMonitor(self, mon);
840     /* Propagate the lock state. */
841     thin = obj->lock;
842     mon->lockCount = LW_LOCK_COUNT(thin);
843     thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT;
844     thin |= (u4)mon | LW_SHAPE_FAT;
845     /* Publish the updated lock word. */
846     android_atomic_release_store(thin, (int32_t *)&obj->lock);
847 }
848
849 /*
850  * Implements monitorenter for "synchronized" stuff.
851  *
852  * This does not fail or throw an exception (unless deadlock prediction
853  * is enabled and set to "err" mode).
854  */
855 void dvmLockObject(Thread* self, Object *obj)
856 {
857     volatile u4 *thinp;
858     ThreadStatus oldStatus;
859     struct timespec tm;
860     long sleepDelayNs;
861     long minSleepDelayNs = 1000000;  /* 1 millisecond */
862     long maxSleepDelayNs = 1000000000;  /* 1 second */
863     u4 thin, newThin, threadId;
864
865     assert(self != NULL);
866     assert(obj != NULL);
867     threadId = self->threadId;
868     thinp = &obj->lock;
869 retry:
870     thin = *thinp;
871     if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
872         /*
873          * The lock is a thin lock.  The owner field is used to
874          * determine the acquire method, ordered by cost.
875          */
876         if (LW_LOCK_OWNER(thin) == threadId) {
877             /*
878              * The calling thread owns the lock.  Increment the
879              * value of the recursion count field.
880              */
881             obj->lock += 1 << LW_LOCK_COUNT_SHIFT;
882             if (LW_LOCK_COUNT(obj->lock) == LW_LOCK_COUNT_MASK) {
883                 /*
884                  * The reacquisition limit has been reached.  Inflate
885                  * the lock so the next acquire will not overflow the
886                  * recursion count field.
887                  */
888                 inflateMonitor(self, obj);
889             }
890         } else if (LW_LOCK_OWNER(thin) == 0) {
891             /*
892              * The lock is unowned.  Install the thread id of the
893              * calling thread into the owner field.  This is the
894              * common case.  In performance critical code the JIT
895              * will have tried this before calling out to the VM.
896              */
897             newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
898             if (android_atomic_acquire_cas(thin, newThin,
899                     (int32_t*)thinp) != 0) {
900                 /*
901                  * The acquire failed.  Try again.
902                  */
903                 goto retry;
904             }
905         } else {
906             LOGV("(%d) spin on lock %p: %#x (%#x) %#x",
907                  threadId, &obj->lock, 0, *thinp, thin);
908             /*
909              * The lock is owned by another thread.  Notify the VM
910              * that we are about to wait.
911              */
912             oldStatus = dvmChangeStatus(self, THREAD_MONITOR);
913             /*
914              * Spin until the thin lock is released or inflated.
915              */
916             sleepDelayNs = 0;
917             for (;;) {
918                 thin = *thinp;
919                 /*
920                  * Check the shape of the lock word.  Another thread
921                  * may have inflated the lock while we were waiting.
922                  */
923                 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
924                     if (LW_LOCK_OWNER(thin) == 0) {
925                         /*
926                          * The lock has been released.  Install the
927                          * thread id of the calling thread into the
928                          * owner field.
929                          */
930                         newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
931                         if (android_atomic_acquire_cas(thin, newThin,
932                                 (int32_t *)thinp) == 0) {
933                             /*
934                              * The acquire succeed.  Break out of the
935                              * loop and proceed to inflate the lock.
936                              */
937                             break;
938                         }
939                     } else {
940                         /*
941                          * The lock has not been released.  Yield so
942                          * the owning thread can run.
943                          */
944                         if (sleepDelayNs == 0) {
945                             sched_yield();
946                             sleepDelayNs = minSleepDelayNs;
947                         } else {
948                             tm.tv_sec = 0;
949                             tm.tv_nsec = sleepDelayNs;
950                             nanosleep(&tm, NULL);
951                             /*
952                              * Prepare the next delay value.  Wrap to
953                              * avoid once a second polls for eternity.
954                              */
955                             if (sleepDelayNs < maxSleepDelayNs / 2) {
956                                 sleepDelayNs *= 2;
957                             } else {
958                                 sleepDelayNs = minSleepDelayNs;
959                             }
960                         }
961                     }
962                 } else {
963                     /*
964                      * The thin lock was inflated by another thread.
965                      * Let the VM know we are no longer waiting and
966                      * try again.
967                      */
968                     LOGV("(%d) lock %p surprise-fattened",
969                              threadId, &obj->lock);
970                     dvmChangeStatus(self, oldStatus);
971                     goto retry;
972                 }
973             }
974             LOGV("(%d) spin on lock done %p: %#x (%#x) %#x",
975                  threadId, &obj->lock, 0, *thinp, thin);
976             /*
977              * We have acquired the thin lock.  Let the VM know that
978              * we are no longer waiting.
979              */
980             dvmChangeStatus(self, oldStatus);
981             /*
982              * Fatten the lock.
983              */
984             inflateMonitor(self, obj);
985             LOGV("(%d) lock %p fattened", threadId, &obj->lock);
986         }
987     } else {
988         /*
989          * The lock is a fat lock.
990          */
991         assert(LW_MONITOR(obj->lock) != NULL);
992         lockMonitor(self, LW_MONITOR(obj->lock));
993     }
994 }
995
996 /*
997  * Implements monitorexit for "synchronized" stuff.
998  *
999  * On failure, throws an exception and returns "false".
1000  */
1001 bool dvmUnlockObject(Thread* self, Object *obj)
1002 {
1003     u4 thin;
1004
1005     assert(self != NULL);
1006     assert(self->status == THREAD_RUNNING);
1007     assert(obj != NULL);
1008     /*
1009      * Cache the lock word as its value can change while we are
1010      * examining its state.
1011      */
1012     thin = obj->lock;
1013     if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1014         /*
1015          * The lock is thin.  We must ensure that the lock is owned
1016          * by the given thread before unlocking it.
1017          */
1018         if (LW_LOCK_OWNER(thin) == self->threadId) {
1019             /*
1020              * We are the lock owner.  It is safe to update the lock
1021              * without CAS as lock ownership guards the lock itself.
1022              */
1023             if (LW_LOCK_COUNT(thin) == 0) {
1024                 /*
1025                  * The lock was not recursively acquired, the common
1026                  * case.  Unlock by clearing all bits except for the
1027                  * hash state.
1028                  */
1029                 obj->lock &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT);
1030             } else {
1031                 /*
1032                  * The object was recursively acquired.  Decrement the
1033                  * lock recursion count field.
1034                  */
1035                 obj->lock -= 1 << LW_LOCK_COUNT_SHIFT;
1036             }
1037         } else {
1038             /*
1039              * We do not own the lock.  The JVM spec requires that we
1040              * throw an exception in this case.
1041              */
1042             dvmThrowIllegalMonitorStateException("unlock of unowned monitor");
1043             return false;
1044         }
1045     } else {
1046         /*
1047          * The lock is fat.  We must check to see if unlockMonitor has
1048          * raised any exceptions before continuing.
1049          */
1050         assert(LW_MONITOR(obj->lock) != NULL);
1051         if (!unlockMonitor(self, LW_MONITOR(obj->lock))) {
1052             /*
1053              * An exception has been raised.  Do not fall through.
1054              */
1055             return false;
1056         }
1057     }
1058     return true;
1059 }
1060
1061 /*
1062  * Object.wait().  Also called for class init.
1063  */
1064 void dvmObjectWait(Thread* self, Object *obj, s8 msec, s4 nsec,
1065     bool interruptShouldThrow)
1066 {
1067     Monitor* mon;
1068     u4 thin = obj->lock;
1069
1070     /* If the lock is still thin, we need to fatten it.
1071      */
1072     if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1073         /* Make sure that 'self' holds the lock.
1074          */
1075         if (LW_LOCK_OWNER(thin) != self->threadId) {
1076             dvmThrowIllegalMonitorStateException(
1077                 "object not locked by thread before wait()");
1078             return;
1079         }
1080
1081         /* This thread holds the lock.  We need to fatten the lock
1082          * so 'self' can block on it.  Don't update the object lock
1083          * field yet, because 'self' needs to acquire the lock before
1084          * any other thread gets a chance.
1085          */
1086         inflateMonitor(self, obj);
1087         LOGV("(%d) lock %p fattened by wait()", self->threadId, &obj->lock);
1088     }
1089     mon = LW_MONITOR(obj->lock);
1090     waitMonitor(self, mon, msec, nsec, interruptShouldThrow);
1091 }
1092
1093 /*
1094  * Object.notify().
1095  */
1096 void dvmObjectNotify(Thread* self, Object *obj)
1097 {
1098     u4 thin = obj->lock;
1099
1100     /* If the lock is still thin, there aren't any waiters;
1101      * waiting on an object forces lock fattening.
1102      */
1103     if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1104         /* Make sure that 'self' holds the lock.
1105          */
1106         if (LW_LOCK_OWNER(thin) != self->threadId) {
1107             dvmThrowIllegalMonitorStateException(
1108                 "object not locked by thread before notify()");
1109             return;
1110         }
1111
1112         /* no-op;  there are no waiters to notify.
1113          */
1114     } else {
1115         /* It's a fat lock.
1116          */
1117         notifyMonitor(self, LW_MONITOR(thin));
1118     }
1119 }
1120
1121 /*
1122  * Object.notifyAll().
1123  */
1124 void dvmObjectNotifyAll(Thread* self, Object *obj)
1125 {
1126     u4 thin = obj->lock;
1127
1128     /* If the lock is still thin, there aren't any waiters;
1129      * waiting on an object forces lock fattening.
1130      */
1131     if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
1132         /* Make sure that 'self' holds the lock.
1133          */
1134         if (LW_LOCK_OWNER(thin) != self->threadId) {
1135             dvmThrowIllegalMonitorStateException(
1136                 "object not locked by thread before notifyAll()");
1137             return;
1138         }
1139
1140         /* no-op;  there are no waiters to notify.
1141          */
1142     } else {
1143         /* It's a fat lock.
1144          */
1145         notifyAllMonitor(self, LW_MONITOR(thin));
1146     }
1147 }
1148
1149 /*
1150  * This implements java.lang.Thread.sleep(long msec, int nsec).
1151  *
1152  * The sleep is interruptible by other threads, which means we can't just
1153  * plop into an OS sleep call.  (We probably could if we wanted to send
1154  * signals around and rely on EINTR, but that's inefficient and relies
1155  * on native code respecting our signal mask.)
1156  *
1157  * We have to do all of this stuff for Object.wait() as well, so it's
1158  * easiest to just sleep on a private Monitor.
1159  *
1160  * It appears that we want sleep(0,0) to go through the motions of sleeping
1161  * for a very short duration, rather than just returning.
1162  */
1163 void dvmThreadSleep(u8 msec, u4 nsec)
1164 {
1165     Thread* self = dvmThreadSelf();
1166     Monitor* mon = gDvm.threadSleepMon;
1167
1168     /* sleep(0,0) wakes up immediately, wait(0,0) means wait forever; adjust */
1169     if (msec == 0 && nsec == 0)
1170         nsec++;
1171
1172     lockMonitor(self, mon);
1173     waitMonitor(self, mon, msec, nsec, true);
1174     unlockMonitor(self, mon);
1175 }
1176
1177 /*
1178  * Implement java.lang.Thread.interrupt().
1179  */
1180 void dvmThreadInterrupt(Thread* thread)
1181 {
1182     assert(thread != NULL);
1183
1184     dvmLockMutex(&thread->waitMutex);
1185
1186     /*
1187      * If the interrupted flag is already set no additional action is
1188      * required.
1189      */
1190     if (thread->interrupted == true) {
1191         dvmUnlockMutex(&thread->waitMutex);
1192         return;
1193     }
1194
1195     /*
1196      * Raise the "interrupted" flag.  This will cause it to bail early out
1197      * of the next wait() attempt, if it's not currently waiting on
1198      * something.
1199      */
1200     thread->interrupted = true;
1201
1202     /*
1203      * Is the thread waiting?
1204      *
1205      * Note that fat vs. thin doesn't matter here;  waitMonitor
1206      * is only set when a thread actually waits on a monitor,
1207      * which implies that the monitor has already been fattened.
1208      */
1209     if (thread->waitMonitor != NULL) {
1210         pthread_cond_signal(&thread->waitCond);
1211     }
1212
1213     dvmUnlockMutex(&thread->waitMutex);
1214 }
1215
1216 #ifndef WITH_COPYING_GC
1217 u4 dvmIdentityHashCode(Object *obj)
1218 {
1219     return (u4)obj;
1220 }
1221 #else
1222 /*
1223  * Returns the identity hash code of the given object.
1224  */
1225 u4 dvmIdentityHashCode(Object *obj)
1226 {
1227     Thread *self, *thread;
1228     volatile u4 *lw;
1229     size_t size;
1230     u4 lock, owner, hashState;
1231
1232     if (obj == NULL) {
1233         /*
1234          * Null is defined to have an identity hash code of 0.
1235          */
1236         return 0;
1237     }
1238     lw = &obj->lock;
1239 retry:
1240     hashState = LW_HASH_STATE(*lw);
1241     if (hashState == LW_HASH_STATE_HASHED) {
1242         /*
1243          * The object has been hashed but has not had its hash code
1244          * relocated by the garbage collector.  Use the raw object
1245          * address.
1246          */
1247         return (u4)obj >> 3;
1248     } else if (hashState == LW_HASH_STATE_HASHED_AND_MOVED) {
1249         /*
1250          * The object has been hashed and its hash code has been
1251          * relocated by the collector.  Use the value of the naturally
1252          * aligned word following the instance data.
1253          */
1254         assert(!dvmIsClassObject(obj));
1255         if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
1256             size = dvmArrayObjectSize((ArrayObject *)obj);
1257             size = (size + 2) & ~2;
1258         } else {
1259             size = obj->clazz->objectSize;
1260         }
1261         return *(u4 *)(((char *)obj) + size);
1262     } else if (hashState == LW_HASH_STATE_UNHASHED) {
1263         /*
1264          * The object has never been hashed.  Change the hash state to
1265          * hashed and use the raw object address.
1266          */
1267         self = dvmThreadSelf();
1268         if (self->threadId == lockOwner(obj)) {
1269             /*
1270              * We already own the lock so we can update the hash state
1271              * directly.
1272              */
1273             *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1274             return (u4)obj >> 3;
1275         }
1276         /*
1277          * We do not own the lock.  Try acquiring the lock.  Should
1278          * this fail, we must suspend the owning thread.
1279          */
1280         if (LW_SHAPE(*lw) == LW_SHAPE_THIN) {
1281             /*
1282              * If the lock is thin assume it is unowned.  We simulate
1283              * an acquire, update, and release with a single CAS.
1284              */
1285             lock = (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1286             if (android_atomic_acquire_cas(
1287                                 0,
1288                                 (int32_t)lock,
1289                                 (int32_t *)lw) == 0) {
1290                 /*
1291                  * A new lockword has been installed with a hash state
1292                  * of hashed.  Use the raw object address.
1293                  */
1294                 return (u4)obj >> 3;
1295             }
1296         } else {
1297             if (tryLockMonitor(self, LW_MONITOR(*lw))) {
1298                 /*
1299                  * The monitor lock has been acquired.  Change the
1300                  * hash state to hashed and use the raw object
1301                  * address.
1302                  */
1303                 *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1304                 unlockMonitor(self, LW_MONITOR(*lw));
1305                 return (u4)obj >> 3;
1306             }
1307         }
1308         /*
1309          * At this point we have failed to acquire the lock.  We must
1310          * identify the owning thread and suspend it.
1311          */
1312         dvmLockThreadList(self);
1313         /*
1314          * Cache the lock word as its value can change between
1315          * determining its shape and retrieving its owner.
1316          */
1317         lock = *lw;
1318         if (LW_SHAPE(lock) == LW_SHAPE_THIN) {
1319             /*
1320              * Find the thread with the corresponding thread id.
1321              */
1322             owner = LW_LOCK_OWNER(lock);
1323             assert(owner != self->threadId);
1324             /*
1325              * If the lock has no owner do not bother scanning the
1326              * thread list and fall through to the failure handler.
1327              */
1328             thread = owner ? gDvm.threadList : NULL;
1329             while (thread != NULL) {
1330                 if (thread->threadId == owner) {
1331                     break;
1332                 }
1333                 thread = thread->next;
1334             }
1335         } else {
1336             thread = LW_MONITOR(lock)->owner;
1337         }
1338         /*
1339          * If thread is NULL the object has been released since the
1340          * thread list lock was acquired.  Try again.
1341          */
1342         if (thread == NULL) {
1343             dvmUnlockThreadList();
1344             goto retry;
1345         }
1346         /*
1347          * Wait for the owning thread to suspend.
1348          */
1349         dvmSuspendThread(thread);
1350         if (dvmHoldsLock(thread, obj)) {
1351             /*
1352              * The owning thread has been suspended.  We can safely
1353              * change the hash state to hashed.
1354              */
1355             *lw |= (LW_HASH_STATE_HASHED << LW_HASH_STATE_SHIFT);
1356             dvmResumeThread(thread);
1357             dvmUnlockThreadList();
1358             return (u4)obj >> 3;
1359         }
1360         /*
1361          * The wrong thread has been suspended.  Try again.
1362          */
1363         dvmResumeThread(thread);
1364         dvmUnlockThreadList();
1365         goto retry;
1366     }
1367     LOGE("object %p has an unknown hash state %#x", obj, hashState);
1368     dvmDumpThread(dvmThreadSelf(), false);
1369     dvmAbort();
1370     return 0;  /* Quiet the compiler. */
1371 }
1372 #endif  /* WITH_COPYING_GC */