void Writer( void * arg ) { struct LOCKING_CONTEXT block; INDEX sequenceIndex=0; INDEX index; LockingInit( &block, LockingBlockNonBlocking, LockingWakeNonBlocking ); while(1) { ResourceLockExclusive( &BufferLock, &block); while( !LockingIsAcquired( &block ) ); //resource should be exlusive ASSERT( BufferLock.State == RESOURCE_EXCLUSIVE ); sequenceIndex++; sequenceIndex%=SEQUENCE_LENGTH; for( index = 0; index < BUFFER_SIZE; index++ ) { Buffer[index] = index + Sequence[ sequenceIndex ] ; } ResourceUnlockExclusive( &BufferLock ); SchedulerStartCritical(); TimesWritten++; SchedulerForceSwitch(); } }
void Reader( void * arg ) { INDEX index; int first,second; BOOL ready = FALSE; while( ! ready ) { SchedulerStartCritical(); if( TimesWritten > 0 ) { ready = TRUE; SchedulerEndCritical(); } else { SchedulerForceSwitch(); } } while(1) { ResourceLockShared( &BufferLock, NULL ); //the resource should be shared ASSERT( BufferLock.State == RESOURCE_SHARED ); for(index=1 ; index < BUFFER_SIZE; index++) { first = Buffer[index-1]; second = Buffer[index]; if( (first +1) != second ) { KernelPanic( ); } } ResourceUnlockShared( &BufferLock ); SchedulerStartCritical(); TimesRead++; SchedulerForceSwitch(); } }
void StallThreadMain( void * arg ) { while(TRUE) { if(StallCount != 0 ) { KernelPanic(); } StallCount++; TotalStall++; SchedulerStartCritical(); SchedulerBlockThread(); SchedulerForceSwitch(); } //We should never get here. KernelPanic(); }