/* * Run with one array */ void run10( PyArrayObject *pyobj1, unsigned int shadersize, unsigned int *shader ) { Operation_t op1; op1.shaderSize = shadersize; op1.EA_shader = shader; // Determining the number of SPEs op1.num_SPES = _GetNumberOfSPES( pyobj1->numberOfBlocks ); Printf1( "Using %u SPEs\n", op1.num_SPES ); op1.obj[0] = pyobj1; unsigned int checked = 0; unsigned int state = 0, r; unsigned int i; for ( i = 0 ; i < op1.num_SPES ; i++ ) { spe_pointer_addr[i][0] = (unsigned int)&op1; spe_in_mbox_write ( speData[i].spe_ctx, &state, 1, SPE_MBOX_ALL_BLOCKING ); } // Waiting for SPEs! checked = 0; while( checked < op1.num_SPES ) { if ( spe_out_mbox_status( speData[checked].spe_ctx ) ) { spe_out_mbox_read( speData[checked].spe_ctx, &r, 1 ); checked++; } } }
void startCargoHandler() { #define my CargoHandlers[_myIndex] /* Claim my Liaison */ int _myIndex; /* ID for currentThread */ int bIndex; Acquire(GlobalDataLock); _myIndex = NumActiveCargoHandlers++; Release(GlobalDataLock); while (true) { #define bag Baggages[bIndex] Acquire(ConveyorLock); if (queue_empty(&ConveyorBelt)) { my._state = ONBREAK; Printf1("Cargo Handler %d is going for a break\n", sizeof("Cargo Handler %d is going for a break\n"), _myIndex); Wait(ConveyorLock, my._commCV); /* Done? */ if (Manager._allCargoDone) { Release(ConveyorLock); break; } Printf1("Cargo Handler %d returned from break\n", sizeof("Cargo Handler %d returned from break\n"), _myIndex); } else { bIndex = queue_pop(&ConveyorBelt); Printf1("Cargo Handler %d picked bag of airline %d with weighing %d lbs\n", sizeof("Cargo Handler %d picked bag of airline %d with weighing %d lbs\n"), concat3Num(_myIndex, bag._airline, bag._weight)); Airlines[bag._airline]._numLoadedBaggages++; my._bagCount[bag._airline]++; my._weightCount[bag._airline] += bag._weight; } Release(ConveyorLock); Yield(); #undef bag } Exit(0); #undef my }
void GetSPEAddr( unsigned int ea, unsigned int *PPE_addr ) { //#ifdef _DEBUG //#if _DBGLVL > 1 // printf( "Getting SPE address @ %#x to %#x\n", ea, (unsigned int)PPE_addr ); //#endif //#endif Printf1( "SPE[%u]: Getting SPE address @ %#x to %#x\n", SPE_id, ea, (unsigned int)PPE_addr ); // Get STRUCTURE spu_mfcdma32( PPE_addr, ea, 16, 30, MFC_GET_CMD ); spu_writech( MFC_WrTagMask, 1 << 30 ); spu_mfcstat( MFC_TAG_UPDATE_ALL ); }
void GetShader( unsigned int EA, unsigned int size, unsigned int *shader ) { //#ifdef _DEBUG //#if _DBGLVL > 1 // printf( "Getting shader @ %#x to %#x(%u)\n", EA, (unsigned int)shader, size ); // printf( "Shader size is %u\n", size ); //#endif //#endif Printf1( "SPE[%u]: Getting shader @ %#x to %#x(%u)\n", SPE_id, EA, (unsigned int)shader, size ); // Get shader size = ( size + 15 ) &~ 15; spu_mfcdma32( shader, EA, size, 29, MFC_GET_CMD ); spu_writech( MFC_WrTagMask, 1 << 29 ); spu_mfcstat( MFC_TAG_UPDATE_ALL ); }
void startLiaison() { #define l Liaisons[_myIndex] /* Claim my Liaison */ int _myIndex; /* ID for currentThread */ Acquire(GlobalDataLock); _myIndex = NumActiveLiaisons++; Release(GlobalDataLock); while(true) { /* Passenger Interaction */ Acquire(LiaisonLineLock); Acquire(l._lock); if (l._lineSize == 0) { Release(LiaisonLineLock); l._state = AVAIL; if (Manager._allLiaisonsDone) { /* Done? */ Release(l._lock); break; } Wait(l._lock, l._commCV); /* Wait for new Passenger */ if (Manager._allLiaisonsDone) { /* Done? */ Release(l._lock); break; } } else { Signal(LiaisonLineLock, l._lineCV); /* Signal Passenger */ Release(LiaisonLineLock); Wait(l._lock, l._commCV); /* Wait on Passenger */ } l._state = BUSY; Printf1("Airport Liaison %d directed passenger %d of airline %d\n", sizeof("Airport Liaison %d directed passenger %d of airline %d\n"), concat3Num(_myIndex, l._currentPassenger, Passengers[l._currentPassenger]._ticket._airline)); Signal(l._lock, l._commCV); /* Signal Passenger */ Wait(l._lock, l._commCV); /* Wait for Passenger to say goodbye */ Release(l._lock); /* end Passenger Interaction */ } Exit(0); #undef l }
void GetOperation( unsigned int ea, Operation_t *data ) { //#ifdef _DEBUG //#if _DBGLVL > 1 // printf( "Getting operation @ %#x to %#x(%u)\n", ea, (unsigned int)data, sizeof( Operation_t ) ); //#endif //#endif Printf1( "SPE[%u]: Getting operation @ %#x to %#x(%u)\n", SPE_id, ea, (unsigned int)data, sizeof( Operation_t ) ); // Get STRUCTURE spu_mfcdma32( data, ea, 32, 30, MFC_GET_CMD ); // Waiting spu_writech( MFC_WrTagMask, 1 << 30 ); spu_mfcstat( MFC_TAG_UPDATE_ALL ); // printf( "---->%#x\n", (unsigned int)data->EA_shader ); // printf( "---->%#x\n", (unsigned int)data->shaderSize ); // printf( "---->%#x\n", (unsigned int)data->obj[0] ); // printf( "---->%#x\n", (unsigned int)data->obj[1] ); // printf( "---->%#x\n", (unsigned int)data->obj[2] ); // printf( "---->%#x\n", (unsigned int)data->scalars[0] ); // printf( "---->%#x\n", (unsigned int)data->scalars[1] ); // printf( "---->%#x\n", (unsigned int)data->scalars[2] ); }
void patient() { int myIndex,k; int patientWaitingCount; Acquire(patientIndexLock); myIndex=GetMV(globalPatientIndexMV,0); SetMV(globalPatientIndexMV,0,myIndex+1); Release(patientIndexLock); Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" has entered the Doctor's Office Waiting Room.\n"); Release(printLock); Acquire(wrnLineLock); if(GetMV(wrnStatusMV,0)==BUSY) { /*If wrn is busy, enter into line*/ /*Printf((unsigned int)" wrnStatus=>BUSY\n");*/ Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" gets in line of the Waiting Room Nurse to get registration form.\n"); Release(printLock); patientWaitingCount=GetMV(patientWaitingCountMV,0); SetMV(patientWaitingCountMV,0,patientWaitingCount+1); Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" waits in line to get form\n"); Release(printLock); Wait(patientWaitingCV,wrnLineLock); } else if(GetMV(wrnStatusMV,0)==FREE) { /* If wrn is FREE, make wrnStatus = BUSY*/ Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" finds no one is in line\n"); SetMV(wrnStatusMV,0,BUSY); } Release(wrnLineLock); Acquire(wrnLock); SetMV(patientTaskMV,0,GETFORM); SetMV(patientGettingFormMV,0,myIndex); Signal(wrnWaitingCV,wrnLock); Release(wrnLock); /* Allocate random values of age & name to patient*/ Acquire(infoLock); /*SetMV(patientES_age,myIndex,random()%100 + 1); SetMV(patientES_name,myIndex,random()%26 + 1); SetMV(patientES_pid,myIndex,myIndex);*/ Release(infoLock); for(k=0;k<10;k=k+1) { Yield(); } Acquire(wrnLineLock); if(GetMV(wrnStatusMV,0) == BUSY) { /*Printf((unsigned int)"wrnStatus => Busy\n");*/ Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" gets in line of the Waiting Room Nurse to submit registration form.\n"); Release(printLock); patientWaitingCount=GetMV(patientWaitingCountMV,0); SetMV(patientWaitingCountMV,0,patientWaitingCount+1); Wait(patientWaitingCV,wrnLineLock); } else if(GetMV(wrnStatusMV,0) == FREE) { /*Printf((unsigned int)"wrnStatus=>Free\n");*/ SetMV(wrnStatusMV,0,BUSY); } Release(wrnLineLock); Acquire(wrnLock); SetMV(patientTaskMV,0, GIVEFORM); SetMV(patientGivingFormMV,0, myIndex); Acquire(printLock); Printf1((unsigned int)"Adult patient:",myIndex); Printf((unsigned int)" submits the filled form to the Waiting Room Nurse.\n"); Release(printLock); Signal(wrnWaitingCV,wrnLock); Release(wrnLock); }
int main( unsigned long long id __attribute__ ((unused)), unsigned long long argv ) { unsigned int i, EA = argv; /* * Allocate memory * */ // Info LS_ShaderInfo shaderinfo; // Memory for the shader unsigned int *shader[__NUMBER_OF_SHADERS]; for( i = 0 ; i < __NUMBER_OF_SHADERS ; i++ ) { shader[i] = (unsigned int *)_malloc_align( __SHADER_SIZE, 7 ); } // Memory for the blocks char *blocks[(__NUMBER_OF_BLOCKS_IN_MEM) * 2]; for( i = 0 ; i < __NUMBER_OF_BLOCKS_IN_MEM * 2 ; i++ ) { blocks[i] = (char *)_malloc_align( __BLOCK_DATA_SIZE, 7 ); shaderinfo.LS_blockDataArea[i] = blocks[i]; } // Memory for metadata char *meta = (char *)_malloc_align( __META_DATA_SIZE, 7 ); shaderinfo.LS_shaderMemory = meta; Operation_t myop[__NUMBER_OF_SHADERS]; unsigned int PPE_addr[__NUMBER_OF_SHADERS] __attribute__((aligned(16))); unsigned int EA_result[__NUMBER_OF_SHADERS]; // Getting SPE id SPE_id = mb_getmbox( ); // SELF CHECK //if ( SPE_id == 0 ) SelfCheck(); //printf( "SPE[%u]: SPE_* is at %#x\n", SPE_id, EA ); //printf( "[SPE(%u)]Check!!!\n", SPE_id ); // SPU program fragment prototype void (*run)( unsigned int SPE_id, LS_ShaderInfo *info, Operation_t *myop, Functions_t *funcs ) = NULL; void (*runr)( unsigned int SPE_id, unsigned int EA_result, LS_ShaderInfo *info, Operation_t *myop, Functions_t *funcs ) = NULL; // Getting SPE id unsigned int seed = mb_getmbox( ); // Init random mc_rand_ks_init( seed ); // Common Functions Functions_t funcs; funcs.printuint = PrintUInt; funcs.printint = PrintInt; funcs.printe = PrintE; funcs.printchar = PrintChar; funcs.printfloat = PrintFloat; funcs.printaddr = PrintAddr; funcs.printstr = PrintString; funcs.printfloat3 = PrintFloat3; funcs.printfloat3v = PrintFloat3v; funcs.printfloatv = PrintFloatv; funcs.printfloatrow = PrintFloatRow; //funcs.printf = printf2; funcs.rand_0_to_1_f = mc_rand_ks_0_to_1_f4; //funcs.rand_0_to_1_fm = mc_rand_ks_0_to_1_array_f4; unsigned int running = 1, task = 0; // prof_clear(); // prof_start(); unsigned int idt; // THA LOOP while( running ) { task = mb_getmbox( ); Printf1( "[SPE(%u)]Got state %u\n", SPE_id, task ); // Transfer operation and run shader with no return value // 0 -> 99 if( task < 100 ) { idt = task; CHECKBOUNDS( idt ); Printf1( "SPE[%u]: Get shader for slot %u\n", SPE_id, idt ); GetSPEAddr( EA, PPE_addr ); GetOperation( PPE_addr[idt], &myop[idt] ); GetShader( (unsigned int)myop[idt].EA_shader, myop[idt].shaderSize, shader[idt] ); Printf1( "SPE[%u]: Shader recieved for slot\n", SPE_id ); run = (void *)shader[0]; Printf1( "SPE[%u]: --- Running shader! ----------\n", SPE_id ); run( SPE_id, &shaderinfo, &myop[idt], &funcs ); Printf1( "SPE[%u]: --- End of shader! -----------\n", SPE_id ); spu_writech( SPU_WrOutMbox, 1 ); } // Transfer operation and run shader WITH return value // 100 -> 199 else if( task < 200 ) { idt = task - 100; CHECKBOUNDS( idt ); Printf1( "SPE[%u]: Get shader with return value for slot %u\n", SPE_id, idt ); EA_result[idt] = mb_getmbox( ); GetSPEAddr( EA, PPE_addr ); GetOperation( PPE_addr[idt], &myop[idt] ); GetShader( (unsigned int)myop[idt].EA_shader, myop[idt].shaderSize, shader[idt] ); runr = (void *)shader[idt]; Printf1( "SPE[%u]: --- Running shader! ----------\n", SPE_id ); runr( SPE_id, EA_result[idt], &shaderinfo, &myop[idt], &funcs ); Printf1( "SPE[%u]: --- End of shader! -----------\n", SPE_id ); spu_writech( SPU_WrOutMbox, 1 ); } // Transfer operation and shader WITHOUT return value // 200 -> 299 else if( task < 300 ) { idt = task - 200; CHECKBOUNDS( idt ); Printf1( "SPE[%u]: Get shader for slot %u\n", SPE_id, idt ); GetSPEAddr( EA, PPE_addr ); GetOperation( PPE_addr[idt], &myop[idt] ); GetShader( (unsigned int)myop[idt].EA_shader, myop[idt].shaderSize, shader[idt] ); spu_writech( SPU_WrOutMbox, 1 ); } // Run shader with no return value // 300 -> 399 else if( task < 400 ) { idt = task - 300; CHECKBOUNDS( idt ); Printf1( "SPE[%u]: Run shader from slot %u\n", SPE_id, idt ); run = (void *)shader[idt]; run( SPE_id, &shaderinfo, &myop[idt], &funcs ); spu_writech( SPU_WrOutMbox, 1 ); } // Transfer operation and shader WITH return value // 400 -> 499 else if( task < 500 ) { idt = task - 400; CHECKBOUNDS( idt ); Printf1( "SPE[%u]: Get shader with return value for slot %u\n", SPE_id, idt ); EA_result[idt] = mb_getmbox( ); GetSPEAddr( EA, PPE_addr ); GetOperation( PPE_addr[idt], &myop[idt] ); GetShader( (unsigned int)myop[idt].EA_shader, myop[idt].shaderSize, shader[idt] ); Printf1( "SPE[%u]: Done getting operation %u\n", SPE_id, idt ); spu_writech( SPU_WrOutMbox, 1 ); } // Run shader WITH return value // 500 -> 599 else if( task < 600 ) { idt = task - 500; CHECKBOUNDS( idt ); Printf1( "SPE[%u]: Run shader with return value from slot %u\n", SPE_id, idt ); runr = (void *)shader[0]; runr( SPE_id, EA_result[idt], &shaderinfo, &myop[0], &funcs ); spu_writech( SPU_WrOutMbox, 1 ); } // Sanity check!!! else if( task == 1000 ) { spu_writech( SPU_WrOutMbox, 123 ); } // DEFAULT else { Printf1( "[SPE(%u)]No such instruction, quitting\n", SPE_id ); running = 0; } // switch( task ) // { // case 0: // QUIT // Printf1( "[SPE(%u)]Quitting\n", SPE_id ); // // running = 0; // break; // // /* // * Get an operation with no return value // * // */ // case 1: // Get operation and run // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[0], &myop[0] ); // GetShader( (unsigned int)myop[0].EA_shader, myop[0].shaderSize, shader[0] ); // run = (void *)shader[0]; // // Printf1( "SPE[%u]: --- Running shader! ----------\n", SPE_id ); // run( SPE_id, &shaderinfo, &myop[0], &funcs ); // Printf1( "SPE[%u]: --- End of shader! -----------\n", SPE_id ); // // spu_writech( SPU_WrOutMbox, 1 ); // break; // // /* // * Get an operation with a return value // * // */ // case 2: // Get operation with return value and run // EA_result = mb_getmbox( ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[0], &myop[0] ); // GetShader( (unsigned int)myop[0].EA_shader, myop[0].shaderSize, shader[0] ); // runr = (void *)shader[0]; // // Printf1( "SPE[%u]: --- Running shader! ----------\n", SPE_id ); // runr( SPE_id, EA_result, &shaderinfo, &myop[0], &funcs ); // // Printf1( "SPE[%u]: --- End of shader! -----------\n", SPE_id ); // // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 3: // Get operation without return value NO RUN! // // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[0], &myop[0] ); // GetShader( (unsigned int)myop[0].EA_shader, myop[0].shaderSize, shader[0] ); // run = (void *)shader[0]; // // spu_writech( SPU_WrOutMbox, 1 ); // break; // // // case 4: // Get operation with return value NO RUN! // // EA_result = mb_getmbox( ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[0], &myop[0] ); // GetShader( (unsigned int)myop[0].EA_shader, myop[0].shaderSize, shader[0] ); // runr = (void *)shader[0]; // // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 5: // RUN!!!! with _NO_ return value // Printf1( "SPE[%u]: --- Running shader! ----------\n", SPE_id ); // // run( SPE_id, &shaderinfo, &myop[0], &funcs ); // Printf1( "SPE[%u]: --- End of shader! -----------\n", SPE_id ); // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 6: // RUN!!!! with return value // // Printf1( "SPE[%u]: --- Running shader! ----------\n", SPE_id ); // // runr( SPE_id, EA_result, &shaderinfo, &myop[0], &funcs ); // // Printf1( "SPE[%u]: --- End of shader! -----------\n", SPE_id ); // // spu_writech( SPU_WrOutMbox, 1 ); // break; // // /* // * Operations with no return values // */ // case 100: // Get a shader for slot 0 // Printf1( "SPE[%u]: Get shader for slot 0\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[0], &myop[0] ); // GetShader( (unsigned int)myop[0].EA_shader, myop[0].shaderSize, shader[0] ); // break; // // case 101: // Get a shader for slot 1 // Printf1( "SPE[%u]: Get shader for slot 1\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[1], &myop[1] ); // GetShader( (unsigned int)myop[1].EA_shader, myop[1].shaderSize, shader[1] ); // break; // // case 102: // Get a shader for slot 2 // Printf1( "SPE[%u]: Get shader for slot 2\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[2], &myop[2] ); // GetShader( (unsigned int)myop[2].EA_shader, myop[2].shaderSize, shader[2] ); // break; // // case 103: // Get a shader for slot 3 // Printf1( "SPE[%u]: Get shader for slot 3\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[3], &myop[3] ); // GetShader( (unsigned int)myop[3].EA_shader, myop[3].shaderSize, shader[3] ); // break; // // case 110: // Run slot 0 // Printf1( "SPE[%u]: Run shader from slot 0\n", SPE_id ); // run = (void *)shader[0]; // run( SPE_id, &shaderinfo, &myop[0], &funcs ); // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 111: // Run slot 1 // Printf1( "SPE[%u]: Run shader from slot 1\n", SPE_id ); // run = (void *)shader[1]; // run( SPE_id, &shaderinfo, &myop[1], &funcs ); // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 112: // Run slot 2 // Printf1( "SPE[%u]: Run shader from slot 2\n", SPE_id ); // run = (void *)shader[2]; // run( SPE_id, &shaderinfo, &myop[2], &funcs ); // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 113: // Run slot 3 // Printf1( "SPE[%u]: Run shader from slot 3\n", SPE_id ); // run = (void *)shader[3]; // Printf1( "SPE[%u]: Run shader from slot 3\n", SPE_id ); // run( SPE_id, &shaderinfo, &myop[3], &funcs ); // spu_writech( SPU_WrOutMbox, 1 ); // break; // // /* // * Update operations // */ // // case 120: // Update operation for slot 0 // Printf1( "SPE[%u]: Update operation for slot 0\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[0], &myop[0] ); // break; // // case 121: // Update operation for slot 1 // Printf1( "SPE[%u]: Update operation for slot 1\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[1], &myop[1] ); // break; // // case 122: // Update operation for slot 2 // Printf1( "SPE[%u]: Update operation for slot 2\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[2], &myop[2] ); // break; // // case 123: // Update operation for slot 3 // Printf1( "SPE[%u]: Update operation for slot 3\n", SPE_id ); // GetSPEAddr( EA, PPE_addr ); // GetOperation( PPE_addr[3], &myop[3] ); // break; // // /* // * Operations with return values // */ // case 200: // Get a shader for slot 0 // Printf1( "SPE[%u]: Run shader with return value from slot 0\n", SPE_id ); // EA_result = mb_getmbox( ); // GetOperation( PPE_addr[0], &myop[0] ); // GetShader( (unsigned int)myop[0].EA_shader, myop[0].shaderSize, shader[0] ); // break; // // case 201: // Get a shader for slot 1 // Printf1( "SPE[%u]: Run shader with return value from slot 1\n", SPE_id ); // EA_result = mb_getmbox( ); // GetOperation( PPE_addr[0], &myop[1] ); // GetShader( (unsigned int)myop[1].EA_shader, myop[1].shaderSize, shader[1] ); // break; // // case 210: // Run slot 0 // runr = (void *)shader[0]; // runr( SPE_id, EA_result, &shaderinfo, &myop[0], &funcs ); // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 211: // Run slot 1 // runr = (void *)shader[1]; // runr( SPE_id, EA_result, &shaderinfo, &myop[1], &funcs ); // spu_writech( SPU_WrOutMbox, 1 ); // break; // // case 1000: // Sanity check // spu_writech( SPU_WrOutMbox, 123 ); // break; // // // default: // Printf1( "[SPE(%u)]No such instruction, quitting\n", SPE_id ); // running = 0; // } } //prof_stop(); return 1; }
void startManager() { #define cis Airlines[i]._cis[j] int i, j; /* for-loop iterator */ while (true) { int numReadyAirlines = 0; /* Check-in Staff */ if (!Manager._allCISDone) { int numDoneAirline = 0; for (i = 0; i < NUM_AIRLINES; ++i) { if (!Airlines[i]._CISclosed) { Acquire(Airlines[i]._lock); if (Airlines[i]._numCheckedinPassengers == Airlines[i]._numExpectedPassengers){ if (Airlines[i]._numOnBreakCIS == NUM_CIS_PER_AIRLINE) { /* All Passenger have JUST went through, send CIS home */ Release(Airlines[i]._lock); /* TODO is this necessary? */ numDoneAirline++; for (j = 0; j < NUM_CIS_PER_AIRLINE; ++j) { /*Acquire(cis._lock);*/ cis._done = true; Signal(Airlines[i]._lock, cis._commCV); /*Release(cis._lock);*/ } Airlines[i]._CISclosed = true; } else { Release(Airlines[i]._lock); /* TODO is this necessary? */ } } else { /* There are still passengers to serve */ Release(Airlines[i]._lock);/* TODO is this necessary? */ for (j = 0; j < NUM_CIS_PER_AIRLINE; ++j) { Acquire(Airlines[i]._cisLineLock); Acquire(Airlines[i]._execLineLock); Acquire(cis._lock); if ((!queue_empty(&Airlines[i]._execQueue) || cis._lineSize) && cis._state == ONBREAK) { /* Signal(cis._lock, cis._commCV);*/ Signal(Airlines[i]._lock, cis._commCV); } Release(cis._lock); Release(Airlines[i]._execLineLock); Release(Airlines[i]._cisLineLock); } } } else { numDoneAirline++; } } if (numDoneAirline == NUM_AIRLINES) { Manager._allCISDone = true; /* Exit all Liaisons too! */ /* If all CIS are done, so are all Liaisons */ Manager._allLiaisonsDone = true; for(i = 0; i < NUM_LIASONS; ++i) { Signal(Liaisons[i]._lock, Liaisons[i]._commCV); } } else { Manager._allCISDone = false; } } /* end if(!_allCISDone) */ #undef cis /* end CIS */ /* Check Conveyor Belt - Cargo Handlers */ if (!Manager._allCargoDone) { int numDone = 0; bool msgToCargo = true; Acquire(ConveyorLock); for (i = 0; i < NUM_AIRLINES; ++i) { /*Printf1("Number loaded: %d, number expected: %d\n", sizeof("Number loaded: %d, number expected: %d\n"), concat2Num(Airlines[i]._numExpectedBaggages, Airlines[i]._numLoadedBaggages));*/ if (Airlines[i]._numExpectedBaggages == Airlines[i]._numLoadedBaggages) { numDone++; } } if (numDone == NUM_AIRLINES) { /*Printf0("All Cargo Handlers done!\n", sizeof("All Cargo Handlers done!\n"));*/ Manager._allCargoDone = true; for (i = 0; i < NUM_CARGO_HANDLERS; ++i) { Signal(ConveyorLock, CargoHandlers[i]._commCV); } } for (i = 0; i < NUM_CARGO_HANDLERS; ++i) { if (!queue_empty(&ConveyorBelt) && CargoHandlers[i]._state == ONBREAK) { Signal(ConveyorLock, CargoHandlers[i]._commCV); if (msgToCargo) { Printf0("Airport manager calls back all the cargo handlers from break\n", sizeof("Airport manager calls back all the cargo handlers from break\n")); msgToCargo = false; } } } Release(ConveyorLock); } /* end Conveyor Belt / Cargo Handlers */ /* Security Officers */ Acquire(OfficersLineLock); for (i = 0; i < NUM_SCREENING_OFFICERS; ++i) { if (!queue_empty(&OfficersLine) && ScreeningOfficers[i]._state == ONBREAK) { Signal(OfficersLineLock, ScreeningOfficers[i]._commCV); } } Release(OfficersLineLock); /* end Security Officers */ /* Check Boarding Lounge */ for (i = 0; i < NUM_AIRLINES; ++i) { if (Airlines[i]._boarded) { numReadyAirlines++; } else if(Airlines[i]._numExpectedBaggages == Airlines[i]._numLoadedBaggages && Airlines[i]._numExpectedPassengers == Airlines[i]._numReadyPassengers) { Printf1("Airport manager gives a boarding call to airline %d\n", sizeof("Airport manager gives a boarding call to airline %d\n"), i); for (j = 0; j < Airlines[i]._numReadyPassengers; ++j) { Acquire(Airlines[i]._lock); Signal(Airlines[i]._lock, Airlines[i]._boardLoungeCV); Release(Airlines[i]._lock); } numReadyAirlines++; Airlines[i]._boarded = true; } } if (numReadyAirlines == NUM_AIRLINES) { int pass_cnt_liaisons = 0; int pass_cnt_SI = 0; int pass_cnt_CIS = 0; int bag_cnt_liaison = 0; int bag_cnt_cargo = 0; int weight_cnt_CIS = 0; int weight_cnt_cargo = 0; Printf0("=====================STATS======================\n", sizeof("=====================STATS======================\n")); for (i = 0; i < NUM_LIASONS; ++i) { for (j = 0; j < NUM_AIRLINES; ++j) { pass_cnt_liaisons += Liaisons[i]._passCount[j]; } } Printf1("Passenger count reported by airport liaison = %d\n", sizeof("Passenger count reported by airport liaison = %d\n"), pass_cnt_liaisons); for (i = 0; i < NUM_AIRLINES; ++i) { for (j = 0; j < NUM_CIS_PER_AIRLINE; ++j) { pass_cnt_CIS += Airlines[i]._cis[j]._passCount; } } Printf1("Passenger count reported by airline check-in staff = %d\n", sizeof("Passenger count reported by airline check-in staff = %d\n"), pass_cnt_CIS); for (i = 0; i < NUM_SECURITY_INSPECTORS; ++i) { pass_cnt_SI += SecurityInspectors[i]._passCount; } Printf1("Passenger count reported by security inspector = %d\n", sizeof("Passenger count reported by security inspector = %d\n"), pass_cnt_SI); for (i = 0; i < NUM_AIRLINES; ++i) { Printf1("From setup: Baggage count of airline %d = %d\n", sizeof("From setup: Baggage count of airline %d = %d\n"), concat2Num(i, Airlines[i]._numExpectedBaggages)); } for (i = 0; i < NUM_AIRLINES; ++i) { bag_cnt_liaison = 0; for (j = 0; j < NUM_LIASONS; ++j) { bag_cnt_liaison += Liaisons[j]._bagCount[i]; } Printf1("From airport liaison: Baggage count of airline %d = %d\n", sizeof("From airport liaison: Baggage count of airline %d = %d\n"), concat2Num(i, bag_cnt_liaison)); } for (i = 0; i < NUM_AIRLINES; ++i) { bag_cnt_cargo = 0; for (j = 0; j < NUM_CARGO_HANDLERS; ++j) { bag_cnt_cargo += CargoHandlers[j]._bagCount[i]; } Printf1("From cargo handlers: Baggage count of airline %d = %d\n", sizeof("From cargo handlers: Baggage count of airline %d = %d\n"), concat2Num(i, bag_cnt_cargo)); } for (i = 0; i < NUM_AIRLINES; ++i) { Printf1("From setup: Baggage weight of airline %d = %d\n", sizeof("From setup: Baggage weight of airline %d = %d\n"), concat2Num(i, Airlines[i]._weightCount)); } for (i = 0; i < NUM_AIRLINES; ++i) { weight_cnt_CIS = 0; for (j = 0; j < NUM_CIS_PER_AIRLINE; ++j) { weight_cnt_CIS += Airlines[i]._cis[j]._weightCount; } Printf1("From airline check-in staff: Baggage weight of airline %d = %d\n", sizeof("From airline check-in staff: Baggage weight of airline %d = %d\n"), concat2Num(i, weight_cnt_CIS)); } for (i = 0; i < NUM_AIRLINES; ++i) { weight_cnt_cargo = 0; for (j = 0; j < NUM_CARGO_HANDLERS; ++j) { weight_cnt_cargo += CargoHandlers[j]._weightCount[i]; } Printf1("From cargo handlers: Baggage weight of airline %d = %d\n", sizeof("From cargo handlers: Baggage weight of airline %d = %d\n"), concat2Num(i, weight_cnt_cargo)); } Printf0("================================================\n", sizeof("================================================\n")); /* tell all Screening Officers to go home */ Manager._allSODone = true; for (i = 0; i < NUM_SCREENING_OFFICERS; ++i) { Signal(OfficersLineLock, ScreeningOfficers[i]._commCV); } /* tell all Security Inspectors to go home */ Manager._allSIDone = true; for (i = 0; i < NUM_SECURITY_INSPECTORS; ++i) { Signal(SecurityInspectors[i]._lock, SecurityInspectors[i]._commCV); } break; } /* Make sure Manager doesn't hog CPU */ for (i = 0; i < 20; ++i) { Yield(); } } Exit(0); }
void startSecurityInspector() { #define mySI SecurityInspectors[_myIndex] int _myIndex; /* Claim my security insepctor */ Acquire(GlobalDataLock); /* _myIndex = NumActiveScreeningOfficers++;*/ _myIndex = NumActiveSecurityInspectors++; Release(GlobalDataLock); /* Start work */ while (true) { bool suspicious, guilty; Acquire(mySI._lock); /*Printf1("SI %d has _newPassenger", sizeof("SI %d has _newPassneger"), _myIndex); Printf1("%d\n", sizeof("%d\n"), mySI._newPassenger);*/ if (mySI._rtnPassSize == 0 && mySI._newPassenger == -1) { mySI._state = AVAIL; /* Done? */ if (Manager._allSIDone) { Release(mySI._lock); break; } Wait(mySI._lock, mySI._commCV); /* Wait for Passenger to come */ /* Done? */ if (Manager._allSIDone) { Release(mySI._lock); break; } mySI._state = BUSY; } if (mySI._rtnPassSize > 0) { /* priority to returning passengers */ while (mySI._rtnPassSize > 0) { Signal(mySI._lock, mySI._rtnPassCV); /* Wake up passener */ Wait(mySI._lock, mySI._rtnPassCV); /* Wait on passenger */ Printf1("Security inspector %d permits returning passenger %d to board\n", sizeof("Security inspector %d permits returning passenger %d to board\n"), concat2Num(_myIndex, mySI._rtnPassenger)); mySI._passCount++; /* mySI._rtnPassenger--;*/ mySI._rtnPassSize--; Signal(mySI._lock, mySI._rtnPassCV); } mySI._rtnPassenger = -1; } if (mySI._newPassenger != -1) { /* I have a new passenger to help */ suspicious = (mySI._newPassenger * 23) % 10 > 7; /* Psuedo Random */ guilty = suspicious || SecurityFailResults[mySI._newPassenger]; if (suspicious) { Printf1("Security Inspector %d is suspicious of the hand luggage of passenger %d\n", sizeof("Security Inspector %d is suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, mySI._newPassenger)); } else { Printf1("Security Inspector %d is not suspicious of the hand luggage of passenger %d\n", sizeof("Security Inspector %d is not suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, mySI._newPassenger)); } if (guilty) { Passengers[mySI._newPassenger]._furtherQuestioning = true; Printf1("Security inspector %d asks passenger %d to go for further examination\n", sizeof("Security inspector %d asks passenger %d to go for further examination\n"), concat2Num(_myIndex, mySI._newPassenger)); } else { Printf1("Security inspector %d allows passenger %d to board \n", sizeof("Security inspector %d allows passenger %d to board \n"), concat2Num(_myIndex, mySI._newPassenger)); mySI._passCount++; } mySI._newPassenger = -1; Signal(mySI._lock, mySI._newPassCV); } Release(mySI._lock); } #undef mySI Exit(0); }
void startScreeningOfficer() { #define mySO ScreeningOfficers[_myIndex] int _myIndex; /* Claim my screening officer */ Acquire(GlobalDataLock); _myIndex = NumActiveScreeningOfficers++; Release(GlobalDataLock); /* Get to work! */ while (true) { int suspicionLevel = false; int shortestLineIndex = -1; /* impossible value */ int i; /* iterator */ Acquire(OfficersLineLock); if (queue_empty(&OfficersLine)) { mySO._state = ONBREAK; /*Printf1("SO %d is going to sleep\n", sizeof("SO %d is going to sleep\n"), _myIndex);*/ /* Done? */ if (Manager._allSODone) { Release(OfficersLineLock); break; } Wait(OfficersLineLock, mySO._commCV); /* Wait for a passenger */ /* Done? */ if (Manager._allSODone) { Release(OfficersLineLock); break; } /*Printf1("SO %d is waking up\n", sizeof("SO %d is waking up\n"), _myIndex);*/ } if (!queue_empty(&OfficersLine)) { Acquire(mySO._lock); mySO._state = BUSY; mySO._currentPassenger = queue_pop(&OfficersLine); Passengers[mySO._currentPassenger]._officerID = _myIndex; Signal(OfficersLineLock, OfficersLineCV); /* Wake up passenger */ Release(OfficersLineLock); Wait(mySO._lock, mySO._commCV); /* Wait for passenger to approach */ /* Generate PASS/FAIL Results */ suspicionLevel = (17 * mySO._currentPassenger) % 10; /* PSUEDO rand() */ SecurityFailResults[mySO._currentPassenger] = suspicionLevel > 8; if (SecurityFailResults[mySO._currentPassenger]) { /* FAIL */ Printf1("Screening officer %d is suspicious of the hand luggage of passenger %d\n", sizeof("Screening officer %d is suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, mySO._currentPassenger)); } else { /* PASS */ Printf1("Screening officer %d is not suspicious of the hand luggage of passenger %d\n", sizeof("Screening officer %d is not suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, mySO._currentPassenger)); } /* Find an Available Security Inspector */ while (shortestLineIndex == -1) { /*Printf1("SO %d is trying to find SI\n", sizeof("SO %d is trying to find SI\n"), _myIndex);*/ #define inspector SecurityInspectors[i] Acquire(InspectorLineLock); /*Printf1("SO %d acquired InspectorLineLock\n", sizeof("SO %d acquired InspectorLineLock\n"), _myIndex);*/ for (i = 0; i < NUM_SECURITY_INSPECTORS; ++i) { Acquire(inspector._lock); /*Printf1("SO %d is checking SI %d\n", sizeof("SO %d is checking SI %d\n"), concat2Num(_myIndex, i));*/ if (inspector._state == AVAIL && inspector._newPassenger == -1) { /* Found an inspector! */ shortestLineIndex = inspector._id; inspector._newPassenger = mySO._currentPassenger; Release(inspector._lock); break; } Release(inspector._lock); } Release(InspectorLineLock); if (shortestLineIndex == -1) { Printf1("SO %d is about to Yield\n", sizeof("SO %d is about to Yield\n"), _myIndex); Yield(); } #undef inspector /* Wait(mySO._lock, mySO._commCV);*/ } /* Found an inspetor for the passenger */ Passengers[mySO._currentPassenger]._inspectorID = shortestLineIndex; Printf1("Screening officer %d directs passenger %d to security inspector %d\n", sizeof("Screening officer %d directs passenger %d to security inspector %d\n"), concat3Num(_myIndex, mySO._currentPassenger, shortestLineIndex)); Signal(mySO._lock, mySO._commCV); Release(mySO._lock); } else { Release(OfficersLineLock); } } Exit(0); }
void startCheckInStaff() { #define myAirline Airlines[_myAirline] #define my myAirline._cis[_myIndex] #define passenger Passengers[my._currentPassenger] /* Claim my CIS */ int _myAirline; int _myIndex; /* ID for currentThread */ Acquire(GlobalDataLock); _myIndex = NumActiveCIS % NUM_CIS_PER_AIRLINE; _myAirline = NumActiveCIS / NUM_CIS_PER_AIRLINE; NumActiveCIS++; Release(GlobalDataLock); while (true) { /* Check lines */ Acquire(myAirline._lock); if (my._lineSize == 0 && queue_empty(&myAirline._execQueue)) { /*Printf0("1\n", sizeof("1\n"));*/ my._state = ONBREAK; /* 'Clock Out' for Break */ myAirline._numOnBreakCIS++; /*Printf1("Cis %d going to sleep\n", sizeof("Cis %d going to sleep\n"), _myIndex);*/ Wait(myAirline._lock, my._commCV); /* Wait on Manager */ /* TODO - make sure okay to wait on aiport lock... maybe better? */ /*Printf1("Cis %d woke up by manager\n", sizeof("Cis %d woke up by manager\n"), _myIndex);*/ /* Time to go home! TGIF! */ if (my._done) { Printf1("Airline check-in staff %d of airline %d is closing the counter\n", sizeof("Airline check-in staff %d of airline %d is closing the counter\n"), concat2Num(_myIndex, _myAirline)); /* Acquire(my._lock);*/ Release(myAirline._lock); /* Wait(my._lock, my._commCV); *//* Wait forever, basically */ /* Release(my._lock); *//* Never reaches here, but whatever... */ break; } myAirline._numOnBreakCIS--; } /* Start helping a passenger */ my._state = BUSY; Acquire(myAirline._cisLineLock); Acquire(myAirline._execLineLock); if (queue_size(&myAirline._execQueue) > 0) { my._currentPassenger = queue_pop(&myAirline._execQueue); passenger._cisID = _myIndex; Printf1("Airline check-in staff %d of airline %d serves an executive class passenger and economy line length = %d\n", sizeof("Airline check-in staff %d of airline %d serves an executive class passenger and economy line length = %d\n"), concat3Num(_myIndex, _myAirline, my._lineSize)); Signal(myAirline._execLineLock, myAirline._execLineCV); /* Signal Passenger */ } else if (my._lineSize > 0) { Printf1("Airline check-in staff %d of airline %d serves an economy class passenger and executive class line length = %d\n", sizeof("Airline check-in staff %d of airline %d serves an economy class passenger and executive class line length = %d\n"), concat3Num(_myIndex, _myAirline, queue_size(&myAirline._execQueue))); Signal(myAirline._cisLineLock, my._lineCV); /*Printf1("Cis %d of airline %d wakes up passenger\n", sizeof("Cis %d of airline %d wakes up passenger\n"), _myIndex*1000+_myAirline);*/ } /* Interact with Passenger */ Acquire(my._lock); Release(myAirline._cisLineLock); Release(myAirline._execLineLock); Release(myAirline._lock); if (my._lineSize > 0 || my._currentPassenger != -1) { /*Printf1("Cis %d of airline %d goes to sleep\n", sizeof("Cis %d of airline %d goes to sleep\n"), _myIndex*1000+_myAirline);*/ Wait(my._lock, my._commCV); } /* Otherwise, Manager woke you up for no reason */ if (my._currentPassenger != -1) { int i; /* Assign seat number */ Acquire(myAirline._lock); passenger._ticket._seat = myAirline._numCheckedinPassengers; myAirline._numCheckedinPassengers++; Release(myAirline._lock); /* Deal with baggage */ Acquire(ConveyorLock); for (i = 0; i < passenger._numBaggages; ++i) { /*Printf0("3\n", sizeof("1\n"));*/ /* #define bIndex (passenger._id * 3) + i #define bag Baggages[bIndex]*/ #define bag Baggages[(passenger._id*3)+i] bag._airline = _myAirline; /* Tag the bag */ /* queue_insert(&ConveyorBelt, bIndex);*/ queue_insert(&ConveyorBelt, passenger._id*3+i); Printf1("Airline check-in staff %d of airline %d dropped bags to the conveyor system \n", sizeof("Airline check-in staff %d of airline %d dropped bags to the conveyor system \n"), concat2Num(_myIndex, _myAirline)); /* myAirline._numExpectedBaggages++;*/ my._weightCount += bag._weight; #undef bag /* #undef bIndex*/ } Release(ConveyorLock); /* Direct Passenger to Airline */ if (passenger._ticket._executive) { Printf2("Airline check-in staff %d of airline %d informs executive class passenger %d to board at gate %d\n", sizeof("Airline check-in staff %d of airline %d informs executive class passenger %d to board at gate %d\n"), /* concat3Num(_myAirline, my._currentPassenger, _myAirline), _myIndex);*/ concat3Num(_myIndex, _myAirline, my._currentPassenger), _myAirline); } else { Printf2("Airline check-in staff %d of airline %d informs economy class passenger %d to board at gate %d\n", sizeof("Airline check-in staff %d of airline %d informs economy class passenger %d to board at gate %d\n"), /* concat3Num(_myAirline, my._currentPassenger, _myAirline), _myIndex);*/ concat3Num(_myIndex, _myAirline, my._currentPassenger), _myAirline); } Signal(my._lock, my._commCV); Wait(my._lock, my._commCV); } my._currentPassenger = -1; Release(my._lock); } /* end while */ Exit(0); #undef passenger #undef my #undef myAirline }
/* Start Functions - functions called by Fork() syscall. */ void startPassenger() { #define my Passengers[_myIndex] #define liaison Liaisons[my._liaisonID] /* Claim my Passenger */ int i, j; /* for-loop iterators */ int _myIndex; /* ID for currentThread */ int _minLineSize; Acquire(GlobalDataLock); _myIndex = NumActivePassengers++; Release(GlobalDataLock); /* Liaison Interaction */ Acquire(LiaisonLineLock); _minLineSize = Liaisons[0]._lineSize; /* Find shortest line */ for (i = 1; i < NUM_LIASONS; i++) { if (Liaisons[i]._lineSize < _minLineSize) { _minLineSize = Liaisons[i]._lineSize; my._liaisonID = i; } } Printf1("Passenger %d chose Liaison %d with a line length %d\n", sizeof("Passenger %d chose Liaison %d with a line length %d\n"), concat3Num(_myIndex, my._liaisonID, liaison._lineSize)); /* Get in line? */ if (liaison._state == BUSY) { /* Printf0("Passenger says liaison is BUSY\n", sizeof("Passenger says liaison is BUSY\n")); Printf1("Passenger's liaison id: %d\n", sizeof("Passenger's liaison id: %d\n"), _myIndex);*/ liaison._lineSize++; Wait(LiaisonLineLock, liaison._lineCV); liaison._lineSize--; } Printf1("Passenger %d is moving along...\n", sizeof("Passenger %d is moving along...\n"), _myIndex); /* Go to Liaison */ Acquire(liaison._lock); Release(LiaisonLineLock); /* Give Liaison my Passenger info */ liaison._passCount[my._ticket._airline]++; liaison._bagCount[my._ticket._airline] += my._numBaggages; liaison._currentPassenger = _myIndex; Signal(liaison._lock, liaison._commCV); /* Signal Liaison */ Wait(liaison._lock, liaison._commCV); /* Wait for Liaison */ Printf1("Passenger %d of Airline %d is directed to the check-in counter\n", sizeof("Passenger %d of Airline %d is directed to the check-in counter\n"), concat2Num(_myIndex, my._ticket._airline)); Signal(liaison._lock, liaison._commCV); Release(liaison._lock); #undef liaison /* end Liaison Interaction */ /* Check-in Staff Interaction */ #define myAirline Airlines[my._ticket._airline] #define myCIS myAirline._cis[my._cisID] if (my._ticket._executive) { Acquire(myAirline._execLineLock); queue_insert(&myAirline._execQueue, _myIndex); Printf1("Passenger %d of Airline %d is waiting in the executive class line\n", sizeof("Passenger %d of Airline %d is waiting in the executive class line\n"), concat2Num(_myIndex, my._ticket._airline)); /* Wait(myAirline._execLineLock, myAirline._execLineLock); *//* Wait on CIS */ Wait(myAirline._execLineLock, myAirline._execLineCV); /* Wait on CIS */ } else { /* Economy */ Acquire(myAirline._cisLineLock); /* Find shortest line */ _minLineSize = myAirline._cis[0]._lineSize; /* declare at top of startPassenger */ my._cisID = 0; for (i = 0; i < NUM_CIS_PER_AIRLINE; ++i) { if (myAirline._cis[i]._lineSize < _minLineSize) { _minLineSize = myAirline._cis[i]._lineSize; my._cisID = i; } } Printf2("Passenger %d of Airline %d chose Airline Check-In staff %d with a line length %d\n", sizeof("Passenger %d of Airline %d chose Airline Check-In staff %d with a line length %d\n"), concat3Num(_myIndex, my._ticket._airline, my._cisID), _minLineSize); myCIS._lineSize++; /*Printf1("Passenger %d of Airline %d is going to sleep and should be woken up by cis %d\n", sizeof("Passenger %d of Airline %d is going to sleep and should be woken up by cis %d\n"), concat3Num(_myIndex, my._ticket._airline, my._cisID));*/ Wait(myAirline._cisLineLock, myCIS._lineCV); /*Printf1("Passenger %d of Airline %d is woken up by cis %d\n", sizeof("Passenger %d of Airline %d is woken up by cis %d\n"), concat3Num(_myIndex, my._ticket._airline, my._cisID));*/ } Acquire(myCIS._lock); if (my._ticket._executive) { Release(myAirline._execLineLock); } else { myCIS._lineSize--; Release(myAirline._cisLineLock); } /* Give baggage to CIS */ myCIS._passCount++; myCIS._bagCount += my._numBaggages; myCIS._currentPassenger = _myIndex; Signal(myCIS._lock, myCIS._commCV); /* Signal CIS */ Wait(myCIS._lock, myCIS._commCV); /* Wait on CIS */ Printf1("Passenger %d of Airline %d was informed to board at gate %d\n", sizeof("Passenger %d of Airline %d was informed to board at gate %d\n"), concat3Num(_myIndex, my._ticket._airline, my._ticket._airline)); Signal(myCIS._lock, myCIS._commCV); /* Signal CIS */ Release(myCIS._lock); #undef myCIS #undef myAirline /* end Check-in Staff Interaction */ /* Screening Officer Interaction */ Acquire(OfficersLineLock); queue_insert(&OfficersLine, _myIndex); Wait(OfficersLineLock, OfficersLineCV); Printf1("Passenger %d gives the hand-luggage to screening officer %d\n", sizeof("Passenger %d gives the hand-luggage to screening officer %d\n"), concat2Num(_myIndex, my._officerID)); Acquire(ScreeningOfficers[my._officerID]._lock); Release(OfficersLineLock); Signal(ScreeningOfficers[my._officerID]._lock, ScreeningOfficers[my._officerID]._commCV); Wait(ScreeningOfficers[my._officerID]._lock, ScreeningOfficers[my._officerID]._commCV); /* officer lock is released below! */ /* end Screening Officer Interaction */ /* Security Inspector Interaction */ #define inspector SecurityInspectors[my._inspectorID] Printf1("Passenger %d moves to security inspector %d\n", sizeof("Passenger %d moves to security inspector %d\n"), concat2Num(_myIndex, my._inspectorID)); Acquire(inspector._lock); Release(ScreeningOfficers[my._officerID]._lock); inspector._newPassenger = _myIndex; if (inspector._state == AVAIL) { /* Wake up inspector */ Signal(inspector._lock, inspector._commCV); } Wait(inspector._lock, inspector._newPassCV); /* Wait for security results */ if (my._furtherQuestioning) { Printf1("Passenger %d goes for futher questioning\n", sizeof("Passenger %d goes for futher questioning\n"), _myIndex); Release(inspector._lock); for (i = 0; i < 10; ++i) { Yield(); /* Simulate Further questioning */ } Printf1("Passenger %d comes back to security inspector %d after further examination\n", sizeof("Passenger %d comes back to security inspector %d after further examination\n"), concat2Num(_myIndex, my._inspectorID)); Acquire(inspector._lock); /* inspector._rtnPassenger++;*/ inspector._rtnPassSize++; if (inspector._state == AVAIL) { Signal(inspector._lock, inspector._commCV); } /*Printf0("About to wait\n", sizeof("About to wait\n"));*/ Wait(inspector._lock, inspector._rtnPassCV); /*Printf0("Woken up\n", sizeof("Woken up\n"));*/ inspector._rtnPassenger = _myIndex; Signal(inspector._lock, inspector._rtnPassCV); Wait(inspector._lock, inspector._rtnPassCV); } Release(inspector._lock); #undef inspector /* end Security Inspector Interaction */ /* Reached the Boarding Lounge */ #define myAirline Airlines[my._ticket._airline] Acquire(myAirline._lock); myAirline._numReadyPassengers++; Printf1("Passenger %d of Airline %d reached the gate %d\n", sizeof("Passenger %d of Airline %d reached the gate %d\n"), concat3Num(_myIndex, my._ticket._airline, my._ticket._airline)); Wait(myAirline._lock, myAirline._boardLoungeCV); /* Wait for boarding call by manager */ Printf1("Passenger %d of Airline %d boarded airline %d\n", sizeof("Passenger %d of Airline %d boarded airline %d\n"), concat3Num(_myIndex, my._ticket._airline, my._ticket._airline)); Release(myAirline._lock); #undef myAirline /* End Boarding Lounge */ Exit(0); #undef my }
void startScreeningOfficer() { #define mySOMACRO GetMV(screeningOfficers, _myIndex) int _myIndex; int _myMV; /* Claim my screening officer */ Acquire(GlobalDataLock); _myIndex = GetMV(NumActiveScreeningOfficers, 0); incrementMV(NumActiveScreeningOfficers, 0); _myMV = GetMV(screeningOfficers, _myIndex); Release(GlobalDataLock); /* Get to work! */ while (true) { int suspicionLevel = false; int shortestLineIndex = -1; /* impossible value */ int i; /* iterator */ Acquire(OfficersLineLock); if (queue_empty(officersLine)) { SetMV(mySOMACRO, SOState, ONBREAK); /*Printf1("SO %d is going to sleep\n", sizeof("SO %d is going to sleep\n"), _myIndex);*/ /* Done? */ if (GetMV(manager, ManAllSODone)) { Release(OfficersLineLock); break; } Wait(OfficersLineLock, GetMV(mySOMACRO, SOCommCV)); /* Wait for a passenger */ /* Done? */ if (GetMV(manager, ManAllSODone)) { Release(OfficersLineLock); break; } /*Printf1("SO %d is waking up\n", sizeof("SO %d is waking up\n"), _myIndex);*/ } if (!queue_empty(officersLine)) { Acquire(GetMV(mySOMACRO, SOLock)); SetMV(mySOMACRO, SOState, BUSY); SetMV(mySOMACRO, SOCurrentPassenger, queue_pop(officersLine)); /* SetMV(GetMV(passengers, GetMV(mySOMACRO, SOCurrentPassenger)), PassOfficerID, _myIndex);*/ SetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassOfficerID, _myMV); Signal(OfficersLineLock, OfficersLineCV); /* Wake up passenger */ Release(OfficersLineLock); Wait(GetMV(mySOMACRO, SOLock), GetMV(mySOMACRO, SOCommCV)); /* Wait for passenger to approach */ /* Generate PASS/FAIL Results */ /* suspicionLevel = (17 * GetMV(mySOMACRO, SOCurrentPassenger)) % 10; *//* PSUEDO rand() */ suspicionLevel = 0; SetMV(SecurityFailResults, GetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassIndex), suspicionLevel > 8); if (GetMV(SecurityFailResults, GetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassIndex))) { /* FAIL */ Printf1("Screening officer %d is suspicious of the hand luggage of passenger %d\n", sizeof("Screening officer %d is suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, GetMV(mySOMACRO, SOCurrentPassenger))); } else { /* PASS */ Printf1("Screening officer %d is not suspicious of the hand luggage of passenger %d\n", sizeof("Screening officer %d is not suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, GetMV(mySOMACRO, SOCurrentPassenger))); } /* Find an Available Security Inspector */ while (shortestLineIndex == -1) { /*Printf1("SO %d is trying to find SI\n", sizeof("SO %d is trying to find SI\n"), _myIndex);*/ #define inspector GetMV(securityInspectors, i) Acquire(InspectorLineLock); /*Printf1("SO %d acquired InspectorLineLock\n", sizeof("SO %d acquired InspectorLineLock\n"), _myIndex);*/ for (i = 0; i < NUM_SECURITY_INSPECTORS; ++i) { Acquire(GetMV(inspector, SILock)); /*Printf1("SO %d is checking SI %d\n", sizeof("SO %d is checking SI %d\n"), concat2Num(_myIndex, i));*/ if (GetMV(inspector, SIState) == AVAIL && GetMV(inspector, SINewPassenger) == -1) { /* Found an inspector! */ shortestLineIndex = GetMV(inspector, SIID); SetMV(inspector, SINewPassenger, GetMV(mySOMACRO, SOCurrentPassenger)); Release(GetMV(inspector, SILock)); break; } /*else { Printf1("GetMV(inspector, SIState) = %d\n", sizeof("GetMV(inspector, SIState) = %d\n"), GetMV(inspector, SIState)); Printf1("GetMV(inspector, SINewPassenger) = %d\n", sizeof("GetMV(inspector, SINewPassenger) = %d\n"), GetMV(inspector, SINewPassenger)); }*/ Release(GetMV(inspector, SILock)); } Release(InspectorLineLock); if (shortestLineIndex == -1) { /* Printf1("SO %d is about to Yield\n", sizeof("SO %d is about to Yield\n"), _myIndex);*/ Yield(); } #undef inspector /* Wait(GetMV(mySOMACRO, SOLock), GetMV(mySOMACRO, SOCommCV));*/ } /* Found an inspetor for the passenger */ SetMV(GetMV(passengers, GetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassIndex)), PassInspectorID, shortestLineIndex); Printf1("Screening officer %d directs passenger %d to security inspector %d\n", sizeof("Screening officer %d directs passenger %d to security inspector %d\n"), concat3Num(_myIndex, GetMV(mySOMACRO, SOCurrentPassenger), shortestLineIndex)); Signal(GetMV(mySOMACRO, SOLock), GetMV(mySOMACRO, SOCommCV)); Release(GetMV(mySOMACRO, SOLock)); } else { Release(OfficersLineLock); } } }