void thread::cancel() { lock(); if (finished()) { unlock(); return; } unlock(); if (!in()) { abort(); return; } bool canCancel = false; lock(); if (started()) { iState = Cancelled; canCancel = true; } unlock(); if (canCancel) throw cancellation(); }
void thread::unblock() { assert(in()); lock(); --iBlockedCount; bool shouldCancel = aborted(); unlock(); if (shouldCancel) throw cancellation(); }
cancellation_test<TIMER_QUEUE>::cancellation_test (const char *timer_queue_type) { ACE_DEBUG ((LM_DEBUG, "\nCancellation test for %C\n\n", timer_queue_type)); int configs[][4] = { { 0, 0, 0, 0, }, { 0, 0, 0, 1, }, { 0, 0, 1, 0, }, { 0, 0, 1, 1, }, { 0, 1, 0, 0, }, { 0, 1, 0, 1, }, { 0, 1, 1, 0, }, { 0, 1, 1, 1, }, { 1, 0, 0, 0, }, { 1, 0, 0, 1, }, { 1, 0, 1, 0, }, { 1, 0, 1, 1, }, { 1, 1, 0, 0, }, { 1, 1, 0, 1, }, { 1, 1, 1, 0, }, { 1, 1, 1, 1, }, }; for (int i = 0; i < (int) (sizeof configs / (sizeof (int) * 5)); i++) { TIMER_QUEUE timer_queue; cancellation (timer_queue, configs[i][0], configs[i][1], configs[i][2], configs[i][3]); } }
/* A worker thread receives a number and a 4-letter name via targ */ void * tmain(void * targ) { /* Local variables are not shared with other threads */ int no, i; char name[5]; pthread_t tid; int isBooked[t];//Tells how many times train i has been booked by this thread. memset(isBooked, 0, sizeof(isBooked)); int trainsBooked[t];//Tells which all trains have been booked by this thread int numTrainsBooked = 0;//Count of the number of trains booked by this thread /* Retrieve my number and name from the parameter passed */ no = ((tinfo *)targ) -> tno; strcpy(name,((tinfo *)targ) -> tname); /* Retrieve my thread id */ tid = pthread_self(); while (1) { /* Check for termination condition */ pthread_mutex_lock(&donemutex); /* if the master thread is done */ if(mdone) { pthread_mutex_unlock(&donemutex); pthread_barrier_wait(&barrier); pthread_exit(NULL); } /* The master thread is still sleeping, so I continue to work */ pthread_mutex_unlock(&donemutex); //Check if number of active queries is less than MAX pthread_mutex_lock(&querymutex); if(numActiveQueries == MAX) { printf("\nThread %d : Blocked since active queries = MAX\n", no); pthread_cond_wait(&querycond, &querymutex); } numActiveQueries++; pthread_mutex_unlock(&querymutex); int a123; //Start query int queryType = 1 + rand()%3; if(queryType == INQUIRE) { inquiry(no); } else if(queryType == BOOK) { booking(no, isBooked, trainsBooked, &numTrainsBooked); } else { cancellation(no, isBooked, trainsBooked, &numTrainsBooked); } pthread_mutex_lock(&querymutex); numActiveQueries--; if(numActiveQueries == (MAX-1))//wake up a waiting query pthread_cond_signal(&querycond); pthread_mutex_unlock(&querymutex); sleep(SLEEPTIME); } }