void* thread_producer() { req_t req_d; while(1) { // wait for a free buffer slot sthread_monitor_enter(mon); while (available_reqs == RING_SIZE) sthread_monitor_wait(mon); sthread_monitor_exit(mon); // create and clean request req_d = (req_t) malloc(sizeof(struct _req)); memset(req_d,0,sizeof(struct _req)); if ((req_d->reqsz = srv_recv_request(&(req_d->req),&(req_d->cliaddr),&(req_d->clilen))) == 0) continue; sthread_monitor_enter(mon); // send to buffer put_req(req_d); available_reqs++; sthread_monitor_signalall(mon); sthread_monitor_exit(mon); sthread_yield(); } }
int main(int argc, char **argv) { void *ret; int i; printf("Testing sthreads, impl: %s\n", (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user"); sthread_init(); mon1 = sthread_monitor_init(); mon2 = sthread_monitor_init(); if (sthread_create(thread0, (void*)1, 10) == NULL) { printf("sthread_create failed\n"); exit(-1); } sthread_monitor_enter(mon1); for (i = 0; i < NUMBER; i++){ if ((thr[i] = sthread_create(thread1, (void*)i, 10)) == NULL) { printf("sthread_create failed\n"); exit(-1); } sthread_yield(); } for (i = 0; i < NUMBER; i++){ sthread_monitor_wait(mon1); } printf("in main\n"); sthread_monitor_exit(mon1); sthread_sleep(10000); sthread_monitor_enter(mon2); sthread_monitor_signalall(mon2); sthread_monitor_exit(mon2); for (i = 0; i < NUMBER; i++){ sthread_join(thr[i], &ret); } printf("\nSUCCESS in creating %i threads\n", NUMBER); printf("out main\n"); return 0; }