- File:
-
- 1 edited
-
src/libcfa/concurrency/monitor.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/monitor.c
rcc7f4b1 r51f3798 6 6 // file "LICENCE" distributed with Cforall. 7 7 // 8 // __monitor_t.c --8 // monitor.c -- 9 9 // 10 10 // Author : Thierry Delisle … … 19 19 #include "kernel_private.h" 20 20 21 void enter( __monitor_t* this) {21 void enter(monitor * this) { 22 22 lock( &this->lock ); 23 23 thread * thrd = this_thread(); 24 24 25 if( !this->owner ) { 26 //No one has the monitor, just take it 27 this->owner = thrd; 28 this->recursion = 1; 29 } 30 else if( this->owner == thrd) { 31 //We already have the monitor, just not how many times we took it 32 assert( this->recursion > 0 ); 33 this->recursion += 1; 25 if( this->holder ) { 26 append( &this->entry_queue, thrd ); 27 ScheduleInternal( &this->lock ); 28 return; 34 29 } 35 30 else { 36 //Some one else has the monitor, wait in line for it 37 append( &this->entry_queue, thrd ); 38 ScheduleInternal( &this->lock ); 39 40 //ScheduleInternal will unlock spinlock, no need to unlock ourselves 41 return; 31 this->holder = thrd; 42 32 } 43 33 … … 45 35 } 46 36 47 void leave( __monitor_t* this) {37 void leave(monitor * this) { 48 38 lock( &this->lock ); 49 39 50 40 thread * thrd = this_thread(); 51 assert( thrd == this-> owner );41 assert( thrd == this->holder ); 52 42 53 //Leaving a recursion level, decrement the counter 54 this->recursion -= 1; 55 56 //If we left the last level of recursion it means we are changing who owns the monitor 57 thread * new_owner = 0; 58 if( this->recursion == 0) { 59 //Get the next thread in the list 60 new_owner = this->owner = pop_head( &this->entry_queue ); 61 62 //We are passing the monitor to someone else, which means recursion level is not 0 63 this->recursion = new_owner ? 1 : 0; 64 } 43 this->holder = pop_head( &this->entry_queue ); 65 44 66 45 unlock( &this->lock ); 67 46 68 //If we have a new owner, we need to wake-up the thread 69 if( new_owner ) { 70 ScheduleThread( new_owner ); 71 } 47 if( this->holder ) ScheduleThread( this->holder ); 72 48 }
Note:
See TracChangeset
for help on using the changeset viewer.