void doCashierStuff(int mySSN, int* cash){ int socialSecurityNum = mySSN; //int sizeOfInt = sizeof(int); //First get in line with a generic method //cout<<"Customer #"<<socialSecurityNum<<" getting in cashier line\n"; int myLine = getInLine(&cashier,socialSecurityNum,cash); //Enter interaction monitor with passport clerk Lock* workLock = &cashier.clerkLock[myLine]; //int workLock = *(cashier.clerkLock + (myLine * sizeOfInt)); Condition* workCV = &cashier.clerkCV[myLine]; //int workCV = *(cashier.clerkCV + (myLine * sizeOfInt)); workLock->Acquire(); //Acquire(workLock); //Tell Clerk CV, then wait cout<<"Customer #"<<socialSecurityNum<<" has given SSN "<<socialSecurityNum<<" to Cashier #"<<myLine<<"\n"; tellCashierSSN(mySSN,myLine); workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Decide weather to self-punish bool readyToPay = cashierChecked[mySSN]; //int readyToPay = *(cashierChecked + (mySSN * sizeOfInt)); if(!readyToPay/*readyToPay == 0*/) { //Release, punish, and leave cout<<"Customer #"<<socialSecurityNum<<" has gone to Cashier #"<<myLine<<" too soon. "<< "They are going to the back of the line.\n"; workCV->Signal(workLock); //Signal(workCV, workLock); workLock->Release(); //Release(workLock); punish(punishTime); return; } //Now you can pay cout<<"Customer #"<<socialSecurityNum<<" has given Cashier #"<<myLine<<" $100\n"; payCashier(mySSN,cash); workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now you've been woken up because you have the passport, so leave //cout<<"Customer #"<<socialSecurityNum<<" got passport and is leaving cashier #"<<myLine<<"\n"; workCV->Signal(workLock); //Signal(workCV, workLock); workLock->Release(); //Release(workLock); return; }
void passportClerk(int id) { int myLineID = id; //set ID bool firstTime = true; //int firstTime = 1; bool ifBribed; //int ifBribed; //int sizeOfInt = sizeof(int); while(true) { //Wait fot the next cust to signal ifBribed = waitForLine(&passPClerk, id, firstTime); //Set up some convenient variables Lock *workLock = &passPClerk.clerkLock[myLineID]; //int workLock = *(passPClerk.clerkLock + (myLineID * sizeOfInt)); Condition *workCV = &passPClerk.clerkCV[myLineID]; //int workCV = *(passPClerk.clerkCV + (myLineID * sizeOfInt)); //Now the clerk has been woken up and has been told the customer ID //Check int customerSSN = passportClerkCurrentCustomer[myLineID]; //int customerSSN = *(passportClerkCurrentCustomer + (myLineID * sizeOfInt)); cout<<"PassportClerk #" << id << " has received SSN " << customerSSN << " from Customer #" << customerSSN << ".\n"; passportClerkChecked[customerSSN] = customersWithCompletedApps[customerSSN] && customersWithCompletedPics[customerSSN]; //*(passportClerkChecked + (customerSSN * sizeOfInt)) = *(customersWithCompletedApps + (customerSSN * sizeOfInt)) && *(customersWithCompletedPics + (customerSSN * sizeOfInt)); if(!passportClerkChecked[customerSSN]/* *(passportClerkChecked (customerSSN + sizeOfInt)) == 0 */){ cout<<"PassportClerk #"<<id<<" has determined that Customer #"<<customerSSN<< " does not have both their application and picture completed\n"; } else{ cout<<"PassportClerk #"<<id<<" has determined that Customer #"<<customerSSN<< " has both their application and picture completed\n"; } //And Signal workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now customer is gone if(passportClerkChecked[customerSSN]/* *(passportClerkChecked + (customerSSN * sizeOfInt)) == 1 */) { cout << "PassportClerk #" << id << " has recorded Customer #" << customerSSN << " passport documentation\n"; } if(ifBribed /*ifBribed == 1*/) { cout<<"PassportClerk #" << id << " has received $500 from Customer #" << customerSSN << ".\n"; passPClerk.cashReceived+=500; } firstTime = false; //firstTime = 0; workLock->Release(); //Release(workLock); } }
HRESULT Filter::Stop() { //Stop is a synchronous operation: when it completes, //the filter is stopped. //odbgstream os; Lock lock; HRESULT hr = lock.Seize(this); if (FAILED(hr)) return hr; #ifdef _DEBUG odbgstream os; os << "webmvorbisencoder::Filter::Stop(begin)" << endl; #endif switch (m_state) { case State_Paused: case State_Running: m_state = State_Stopped; //Stop inpin first, to signal thread to terminate. m_inpin.Stop(); hr = lock.Release(); assert(SUCCEEDED(hr)); //Now stop outpin, to terminate its thread too. m_outpin.Stop(); break; case State_Stopped: default: break; } #ifdef _DEBUG os << "webmvorbisencoder::Filter::Stop(end)" << endl; #endif return S_OK; }
void doPassportClerkStuff(int socialSecurityNum,int*cash){ int mySSN = socialSecurityNum; //int sizeOfInt = sizeof(int); //First get in line with a generic method //cout<<"Customer #"<<socialSecurityNum<<" getting in passport Line\n"; int myLine = getInLine(&passPClerk,socialSecurityNum,cash); //Enter interaction monitor with passport clerk Lock* workLock = &passPClerk.clerkLock[myLine]; //int workLock = *(passPClerk.clerkLock + (myLine * sizeOfInt)); Condition* workCV = &passPClerk.clerkCV[myLine]; //int workCV = *(passPClerk.clerkCV + (myLine * sizeOfInt)); workLock->Acquire(); //Acquire(workLock); //Tell Clerk CV, then wait cout<<"Customer #"<<socialSecurityNum<<" has given SSN "<<socialSecurityNum<<" to PassportClerk #"<<myLine<<"\n"; tellPassportClerkSSN(mySSN,myLine); workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now leave //cout<<"Customer #"<<socialSecurityNum<<" leaving passportClerk #"<<myLine<<"\n"; workCV->Signal(workLock); //Signal(workCV, workLock); workLock->Release(); //Release(workLock); //Decide weather to self-punish bool myPassportChecked = passportClerkChecked[mySSN]; //int myPassportChecked = *(passportClerkChecked + (mySSN * sizeOfInt)); if(!myPassportChecked /*myPassportChecked == 0 */) { cout<<"Customer #"<<socialSecurityNum<<" has gone to PassportClerk #"<<myLine<<" too soon. "<< "They are going to the back of the line.\n"; punish(punishTime); } return; }
static void postfork() { sLock.Release(); }
HRESULT Filter::GetState( DWORD timeout, FILTER_STATE* p) { if (p == 0) return E_POINTER; //What the GetState.timeout parameter refers to is not to locking //the filter, but rather to waiting to determine the current state. //A request to Stop is always synchronous (hence no timeout parameter), //but a request to Pause can be asynchronous, so the caller can say //how long he's willing to wait for the transition (to paused) to //complete. //TODO: implement a waiting scheme here. We'll probably have to //use SignalObjectAndWait atomically release the mutex and then //wait for the condition variable to change. //if (hr == VFW_E_TIMEOUT) // return VFW_S_STATE_INTERMEDIATE; Lock lock; HRESULT hrLock = lock.Seize(this); //The lock is only used for synchronization. If Seize fails, //it means there's a serious problem with the filter. if (FAILED(hrLock)) return E_FAIL; FILTER_STATE& state = *p; if (m_cStarvation < 0) //not starving { state = m_state; return S_OK; } assert(m_pSegment); long count = m_pSegment->GetCount(); if (count > m_cStarvation) { m_cStarvation = -1; state = m_state; //TODO: should be State_Paused? return S_OK; } for (;;) { lock.Release(); DWORD index; //TODO: this timeout isn't quite correct. The parameter refers //to the total wait time. As used here in the call to WaitForHandles, //it refers to the wait time for this pass through the loop. const HRESULT hrWait = CoWaitForMultipleHandles( 0, //wait flags timeout, 1, &m_hNewCluster, &index); if (SUCCEEDED(hrWait)) assert(index == 0); else if (hrWait != RPC_S_CALLPENDING) //error, despite "S" in name return hrWait; hrLock = lock.Seize(this); if (FAILED(hrLock)) return E_FAIL; count = m_pSegment->GetCount(); if (count > m_cStarvation) { m_cStarvation = -1; state = m_state; //TODO: should be State_Paused? return S_OK; } if (FAILED(hrWait)) //there was a timeout before receiving signal return VFW_S_STATE_INTERMEDIATE; } }
HRESULT Filter::Stop() { //Stop is a synchronous operation: when it completes, //the filter is stopped. Lock lock; HRESULT hr = lock.Seize(this); if (FAILED(hr)) return hr; switch (m_state) { case State_Paused: case State_Running: //Stop is synchronous. When stop completes, all threads //should be stopped. What does "stopped" mean" In our //case it probably means "terminated". //It's a bit tricky here because we hold the filter //lock. If threads need to acquire filter lock //then we'll have to release it. Only the FGM can call //Stop, etc, so there's no problem to release lock //while Stop is executing, to allow threads to acquire //filter lock temporarily. //The streaming thread will receiving an indication //automatically (assuming it's connected), either via //GetBuffer or Receive, so there's nothing this filter //needs to do to tell the streaming thread to stop. //One implementation strategy is to have build a //vector of thread handles, and then wait for a signal //on one of them. When the handle is signalled //(meaning that the thread has terminated), then //we remove that handle from the vector, close the //handle, and the wait again. Repeat until the //all threads have been terminated. //We also need to clean up any unused samples, //and decommit the allocator. (In fact, we could //decommit the allocator immediately, and then wait //for the threads to terminated.) m_state = State_Stopped; hr = m_inpin.m_reader.BeginFlush(); assert(SUCCEEDED(hr)); lock.Release(); OnStop(); hr = lock.Seize(this); assert(SUCCEEDED(hr)); //TODO hr = m_inpin.m_reader.EndFlush(); assert(SUCCEEDED(hr)); break; case State_Stopped: default: break; } return S_OK; }
void cashierDo(int id) { int myLineID = id; //set ID bool firstTime = true; //int firstTime = 1; bool ifBribed; //int ifBribed; //int sizeOfInt = sizeof(int); while (true) { //Wait fot the next customer to signal //cout << "Cashier #" << id << " about to wait for customer\n"; waitForLine(&cashier, id, firstTime); //Set up some convenient variables Lock *workLock = &cashier.clerkLock[myLineID]; //int workLock = *(cashier.clerkLock + (myLineID * sizeOfInt)); Condition *workCV = &cashier.clerkCV[myLineID]; //int workCV = *(cashier.clerkCV + (myLineID * sizeOfInt)); //Now the clerk has been woken up and has been told the customer ID //Check int customerSSN = cashierCurrentCustomer[myLineID]; //int customerSSN = *(cashierCurrentCustomer + (myLineID * sizeOfInt)); cout<<"Cashier #" << id << " has received SSN " << customerSSN << " from Customer #" << customerSSN << ".\n"; cashierChecked[customerSSN] = passportClerkChecked[customerSSN]; //*(cashierChecked + (customerSSN * sizeOfInt)) = *(passportClerkChecked + (customerSSN * sizeOfInt)); //And Signal workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); if (!cashierChecked[customerSSN]/* *(cashierChecked + (customerSSN * sizeOfInt)) == 0 */) { //Now customer is gone cout<<"Cashier #"<<id<<" has recieved $100 from Customer #"<<customerSSN<< " before certification. They are to go to the back of the line\n"; workLock->Release(); //Release(workLock); return; } //Now customer has paid, so give passport and mark cout<<"Cashier #"<<id<<" has verified that Customer #"<<customerSSN<<" has been certified by a PassportClerk\n"; cout<<"Cashier #"<<id<<" has recieved $100 from Customer #"<<customerSSN<<" after certification\n"; cout << "Cashier #" << id << " provided Customer #" << customerSSN << " with their completed passport\n"; cashier.cashReceived+=100; gottenPassport[customerSSN] = true; //*(gottenPassport + (customerSSN * sizeOfInt)) = 1; workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now customer has left cout << "Cashier #" << id << " has recorded that Customer #" << customerSSN << " has been given their completed passport\n"; firstTime = false; //firstTime = 0; workLock->Release(); //Release(workLock); } //Exit(0); }