void create_data(){ struct semaphore* s=semaphore_create(M); set_bouncer(s); struct BB* bb=BB_create(A); set_ABB(bb); bb=BB_create(A); set_DrinkBB(bb); bb=BB_create(C); set_CBB(bb); // buy new cups int i; for(i=0; i<C; i++){ struct Cup* c=malloc(sizeof(struct Cup)); c->id=next_cupID; next_cupID++; printf(1, "buy new cup\n"); BB_put(cbb, c); } bb=BB_create(C); set_DBB(bb); }
void return_cup(struct Cup* c) { BB_put(dbb, c); dc_count++; if(get_dc_percent()>60){ printf(1, "wakeup cupboy!\n"); semaphore_up(cupboy_lk); } }
void *ping(void *s) { thread_init *p = s; /* p has the proper type instead of void */ int i; int sleep_time; char sync[1] = {'S'}; unsigned int rand_seed = p->my_seed; for (i = 0; i < p->count; i++) { sleep_time = 1 + (rand_r(&rand_seed) % p->max_sleep); printf("%s (Delay %d seconds before & after Pong thread)\n", p->my_call, sleep_time); st_sleep(sleep_time); BB_put(sync); st_sleep(sleep_time); /* give pong a chance to run */ } quit = true; printf("Ping exiting\n"); BB_put(sync); fflush(stdout); st_thread_exit(NULL); }
// This function is called by a bartender whenever a student finished to drink his drink // and wishes to return the cup used (i.e when the type of the action the bartender received from // ABB is 2 - returning a dirty cup). // If at least 60% of the cups are dirty, the cup boy will be notified. void return_cup(Cup* cup){ BB_put(DBB,cup); }
// This function is called by the cup boy when he wishes to add a clean cup he just washed. void add_clean_cup(Cup* cup){ BB_put(CBB,cup); }
// This function is called by the bartender whenever he finishes to make a drink (ordered by a student). // The cup the drink is made in is placed in the DrinkBB. void serve_drink(Cup* cup){ BB_put(DrinkBB,cup); }
// This function is called by a student whenever he wants to perform an action: // place an order for a drink from the bar or return a dirty cup. // The action is placed at the end of the buffer. void place_action(Action* action){ BB_put(ABB,action); }
void serve_drink(struct Cup* c) { BB_put(drinkBB, c); }
void place_action(struct Action* action) { BB_put(bb, action); }
void add_clean_cup(struct Cup* c) { BB_put(cbb, c); }