Re: [Sablevm-developer] locks
Brought to you by:
egagnon
From: John L. <je...@pi...> - 2001-01-12 16:09:57
|
> John Leuner wrote: > > Have you implemented object locks in SableVM yet? I have had a quick look > > at the source and didn't find anything there. > > Not yet. I have spent too much time writing about SableVM (tech report > & Ph.D. proposal & presentation slides), and participating to seminars. > Before I go forward with it, I want to get single threading in a much > better state. I think that getting exceptions to work, and cleaning the > memory management code has higher priority. > > Do you want to help with implementing locks? I can give you a few > pointers (in addition to my tech report). I'ld like to make a lock implementation for my JVM (kissme), but of course it should be easy to use in SableVM too. So far I have written code to do the atomic test and set: (pstObject is the handle to the object we want to lock) { //we must do an atomic test and set int before; int temp_word; //set high bit to 1 //set bit 23 to 1 temp_word = 0x80800001; //set the bits 8-21 to the thread identifier temp_word |= ( (pthread_self() << 8) & 0x007fff00); eprintf("Before lock it is %x and temp_word is %x\n", ODEREF(pstObject)->lock_word, temp_word); before = ODEREF(pstObject)->lock_word; if(before == 0) //object is unlocked { //set eax to 0 //compare ecx to eax, if they are the same, then copy temp_word to ecx __asm__ __volatile__ ( "movl $0x0, %%eax ; \n" "movl %1, %%ecx ; \n" " cmpxchg %%ecx,%0 ; \n" : "=r" (ODEREF(pstObject)->lock_word) : "r" (temp_word) , "0" (ODEREF(pstObject)->lock_word) : "ax","cx" ); } else { if( (ODEREF(pstObject)->lock_word & 0x007fff00) == ( (pthread_self() << 8) & 0x007fff00)) { //we own the lock //so we just increment the count temp_word = before + 1; __asm__ __volatile__ ( "movl %3, %%eax ; \n" "movl %1, %%ecx ; \n" " cmpxchg %%ecx,%0 ; \n" : "=r" (ODEREF(pstObject)->lock_word) : "r" (temp_word) , "0" (ODEREF(pstObject)->lock_word) , "r" (before) : "ax","cx" ); //again we must check if we succeeded if(temp_word == ODEREF(pstObject)->lock_word) { eprintf("Succeeded in doing inc atomic lock\n"); } else { eprintf("Tried to do incremental lock but failed %x\n", ODEREF(pstObject)->lock_word); assert( 4 == 0); } } else //we aren't the owner { eprintf("we don't own this lock!!!!\n"); } } //check if we succeeded if(temp_word == ODEREF(pstObject)->lock_word) { eprintf("Succeeded in doing atomic lock %x\n", ODEREF(pstObject)->lock_word); } else { eprintf("Failed in doing atomic lock it is %x and temp_word is %x\n", ODEREF(pstObject)->lock_word, temp_word)\; //this means either we, or someone else, owns the lock assert( 2 == 3); } I'm reading the report and I'm not sure how a thin lock is inflated to a fat lock. This is what I have in mind for the contention case: if lock is owned by another thread get owning thread identifier get thread structure for owning thread acquire lock for owning thread set contention bit in thread structure if lock is still thin and owned by that thread add tuple to owning threads wait list sleep (on what?) else restore contention bit release lock for owning thread repeat this end So does a lock get inflated only during an unlock operation? When it is inflated, do we use the high bit to distinguish between a thin and fat lock? Is the fat lock just a pthread_mutex_t on linux? Do we use the lock word for this? John Leuner |