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; }
int main(int argc, char **argv) { int arg = 1; printf("Testing sthread_mutex_*, impl: %s\n", (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user"); sthread_init(); mon = sthread_monitor_init(); sthread_monitor_enter(mon); if (sthread_create(thread_start, (void*)&arg, 1) == NULL) { printf("sthread_create failed\n"); exit(1); } printf("thread principal vai bloquear-se\n"); sthread_monitor_wait(mon); printf("thread principal desbloqueada\n"); i = 2; printf("i=2\n"); sthread_monitor_exit(mon); if (i==2) { printf("\nSUCESSO!\n"); } else { printf("\nteste falhado...\n"); } return 1; }
void *thread1(void *arg) { int i = *(int*)arg; printf("thread %d started\n", i); sthread_monitor_enter(mon1); printf("thread %d enter mon1\n", i); sthread_monitor_signal(mon1); printf("thread %d signal mon1\n", i); sthread_monitor_exit(mon1); printf("thread %d exit mon1\n", i); sthread_monitor_enter(mon2); printf("thread %d enter mon2\n", i); sthread_monitor_wait(mon2); printf("thread %d wait mon2\n", i); sthread_monitor_exit(mon2); printf("end %i\n", *(int*)arg); return 0; }
void *thread_start(void *arg) { printf("thread filha antes do monitor\n"); sthread_monitor_enter(mon); printf("thread filha dentro do monitor, antes do signal\n"); sthread_monitor_signal(mon); printf("thread filha dentro do monitor, depois do signal\n"); sthread_monitor_exit(mon); printf("thread filha depois do monitor\n"); return NULL; }
void* thread_consumer() { req_t req_d; int ressz, req_i; snfs_msg_res_t res; while(1) { sthread_monitor_enter(mon); // get request from queue while (!available_reqs) sthread_monitor_wait(mon); req_d = get_req(); available_reqs--; sthread_monitor_signal(mon); sthread_monitor_exit(mon); // clean response memset(&res,0,sizeof(res)); // find request handler req_i = -1; for (int i = 0; i < NUM_REQ_HANDLERS; i++) { if (req_d->req.type == Service[i].type) { req_i = i; break; } } // serve the request if (req_i == -1) { res.status = RES_UNKNOWN; ressz = sizeof(res) - sizeof(res.body); printf("[snfs_srv] unknown request.\n"); } else { Service[req_i].handler(&(req_d->req),req_d->reqsz,&res,&ressz); } // send response to client srv_send_response(&res,ressz,&(req_d->cliaddr),req_d->clilen); // free stuff free(req_d); req_d = NULL; // force request processing sthread_yield(); } }
int main(int argc, char **argv) { int arg = 1; sthread_init(); mon = sthread_monitor_init(); sthread_monitor_enter(mon); if (sthread_create(thread_start, (void*)&arg, 10) == NULL) { printf("sthread_create failed\n"); exit(1); } printf("thread principal vai bloquear-se\n"); sthread_monitor_wait(mon); printf("thread principal desbloqueada\n"); sthread_monitor_exit(mon); printf("\nSUCESSO!\n"); return 1; }