//Custom down void down(Semaphore *s){ pthread_t thread; thread = pthread_self(); //get the currently running thread enter_region(thread); if(thread == prod_thread){ //if producer s->val -= 1; if(s->val < 0){ //Save wake signal then sleep s->sig = PROD_SIGNAL; leave_region(thread); my_sleep(PROD_SIGNAL); } } else if (thread == cons_thread){ //if consumer s->val -= 1; if(s->val < 0){ //save wake signal then sleep s->sig = CONS_SIGNAL; leave_region(thread); my_sleep(CONS_SIGNAL); } } leave_region(thread); //catchall }
//Custom up void up(Semaphore *s){ pthread_t thread; thread = pthread_self(); //get the currently running thread enter_region(thread); if(thread == prod_thread){ //if producer s->val += 1; if(s->val <= 0){ //We know wake sig, so wake my_wakeup(cons_thread, CONS_SIGNAL); } } else if (thread == cons_thread){ //if consumer s->val += 1; if(s->val <= 0){ //we know wake sig, so wake my_wakeup(prod_thread, PROD_SIGNAL); } } leave_region(thread); }
void *test_func(void *arg) { int process = *((int *)arg); printf("thread %d run\n", process); int i=0; for(i=0;i<2000000;++i) { enter_region(process); //printf("%d enter, count = %d\n", pthread_self(),count); ++count; leave_region(process); } return NULL; }