uint nw_cons(future *fut) { printf("cons"); /*int i, status; status = future_get(fut, &i); if (status < 1) { printf("future_get failed\n"); return -1; } printf("it produced %d (Consumer PID : %d)\n", i, currpid); if(fut->flag == FUTURE_EXCLUSIVE){ future_free(fut); }else if(fisempty(fut->get_queue)){ future_free(fut); } return OK;*/ int i, status; status = future_get(fut, &i); if (status < 1) { printf("future_get failed\n"); return -1; } printf("It produced : %d \n", i); if(fut->flag == FUTURE_EXCLUSIVE){ future_free(fut); return OK; } if(fisempty(fut->get_queue)){ future_free(fut); } return OK; }
uint future_cons(future *fut) { int i, status; /*uncomment below line to run with fsignal anf fwait status = future_get(fut, &i);*/ /*as suggested this code follows busy waiting *same can be implemented with resched() which is implemented and commented for now*/ /*if (status < 1) { printf("future_get failed\n"); return -1; }*/ while(1){ status = future_get(fut, &i); if(status == OK){ break; } } status = future_get(fut, &i); printf("it produced %d (Consumer PID : %d)\n", i, currpid); future_free(fut); return OK; }
void TorrentTrackerCommManager::requestPeers(const uint64_t amountUploaded, const uint64_t amountDownloaded, const uint64_t amountLeft) { std::vector<future_t *> futures; std::vector<TorrentTrackerComm *>::iterator it; for (it = trackers.begin(); it != trackers.end(); ++it) { //spawn thread to requestPeers in each tracker //if return null, re-establish connection CallRequestPeersParams * param = new CallRequestPeersParams(); param->amountUploaded = amountUploaded; param->amountDownloaded = amountDownloaded; param->amountLeft = amountLeft; param->tracker = *it; param->trackers = &trackers; param->peerList = &peerList; future_t * f = thread_pool_submit(threadPool, (thread_pool_callable_func_t) &callRequestPeers, param); futures.push_back(f); } //Finish and delete all running threads std::vector<future_t *>::iterator futureIt; for (futureIt = futures.begin(); futureIt != futures.end(); futureIt++) { future_get(*futureIt); future_free(*futureIt); } }
uint future_cons(future *fut) { //kprintf("tid = %d\n",gettid()); int i, status; status = future_get(fut, &i); if (status == SYSERR) { printf("future_get failed\n"); return -1; } kprintf("consumed %d\n", i); if(fut->flag == FUTURE_EXCLUSIVE){ if(future_free(fut)==SYSERR){ printf("future_free failed\n"); } } return OK; }
static int run_test(int nthreads) { struct benchmark_data * bdata = start_benchmark(); struct thread_pool * threadpool = thread_pool_new(nthreads); struct arg2 args = { .a = 20, .b = 22, }; struct future * sum = thread_pool_submit(threadpool, (fork_join_task_t) adder_task, &args); uintptr_t ssum = (uintptr_t) future_get(sum); future_free(sum); thread_pool_shutdown_and_destroy(threadpool); stop_benchmark(bdata); // consistency check if (ssum != 42) { fprintf(stderr, "Wrong result, expected 42, got %ld\n", ssum); abort(); } report_benchmark_results(bdata); printf("Test successful.\n"); free(bdata); return 0; } /**********************************************************************************/ static void usage(char *av0, int exvalue) { fprintf(stderr, "Usage: %s [-n <n>]\n" " -n number of threads in pool, default %d\n" , av0, DEFAULT_THREADS); exit(exvalue); }
shellcmd xsh_prodcons(int nargs, char *args[]) { n = 0; int count = 2000; bool use_futures = FALSE; if (nargs > 3) { printf( "ERROR: Wrong number of arguments given. Expected 2, 1 or 0, found %d.\n" "USAGE: prodcons COUNT\n", nargs - 1); return 1; } use_futures = search_dash_f(args); count = search_count_arg(args, count); printf("use_futures: %d\n", use_futures); printf("count: %d\n", count); if (use_futures == FALSE) { /* run as before */ consumed = semcreate(0); produced = semcreate(1); tid_typ producer_th = create(producer, 1024, 20, "producer", 1, count); tid_typ consumer_th = create(consumer, 1024, 20, "consumer", 1, count); resume(consumer_th); resume(producer_th); } else { /* run program that uses futures */ future *f1, *f2, *f3; f1 = future_alloc(FUTURE_EXCLUSIVE); f2 = future_alloc(FUTURE_EXCLUSIVE); f3 = future_alloc(FUTURE_EXCLUSIVE); /* semaphore for printing */ semaphore print_sem = semcreate(1); /* semaphores to wait on thrads */ semaphore running = semcreate(0); future *f_exclusive, *f_shared, *f_queue; f_exclusive = future_alloc(FUTURE_EXCLUSIVE); f_shared = future_alloc(FUTURE_SHARED); f_queue = future_alloc(FUTURE_QUEUE); int i; printf("\ntesting FUTURE_EXCLUSIVE\n"); resume(create(future_cons, 1024, 20, "fcons1", 3, f_exclusive, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod1", 3, f_exclusive, print_sem, running)); wait(running); wait(running); printf("\ntesting FUTURE_SHARED\n"); resume(create(future_cons, 1024, 20, "fcons2", 3, f_shared, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons3", 3, f_shared, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons4", 3, f_shared, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons5", 3, f_shared, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod2", 3, f_shared, print_sem, running)); for (i = 0; i < 5; ++i) wait(running); printf("\ntesting FUTURE_QUEUE\n"); resume(create(future_cons, 1024, 20, "fcons6", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod3", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod4", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod5", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod6", 3, f_queue, print_sem, running)); for (i = 0; i < 8; ++i) wait(running); printf("\ntesting FUTURE_QUEUE (in different order)\n"); resume(create(future_prod, 1024, 20, "fprod4", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons6", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod3", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod5", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod6", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); for (i = 0; i < 8; ++i) wait(running); printf("\ntesting FUTURE_QUEUE (in different order)\n"); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod3", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod5", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod6", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons6", 3, f_queue, print_sem, running)); resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running)); resume(create(future_prod, 1024, 20, "fprod4", 3, f_queue, print_sem, running)); for (i = 0; i < 8; ++i) wait(running); /* threads are done, free the semaphores and futures */ semfree(print_sem); semfree(running); future_free(f1); future_free(f2); future_free(f3); } return 0; }
/*------------------------------------------------------------------------ * xsh_prodcons - Producer Consumer Problem *------------------------------------------------------------------------ */ shellcmd xsh_prodcons(int nargs, char *args[]) { /* Output info for '--help' argument */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s <integer> \n\n", args[0]); printf("Description:\n"); printf("\tProducer Consumer Problem\n"); printf("\t--help\tdisplays this help and exit\n"); return SYSERR; } if(nargs >2){ /* throw error saying too many arguements */ fprintf(stderr, "%s: too many arguements\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } if (nargs == 2 && strncmp(args[1], "-f", 7) == 0) { /* the -f option is enabled */ main_pid = currpid; mutex = 0; printf("----- FUTURE_EXCLUSIVE -----\n"); /* Allocate futures */ f_exclusive = future_alloc(FUTURE_EXCLUSIVE); if(f_exclusive == NULL) { printf("Future Allocation Failed\n"); return SYSERR; } /* Create the producer & consumer and put them in the ready queue */ resume( create(future_cons, 1024, 20, "fcons1", 1, f_exclusive) ); resume( create(future_prod, 1024, 20, "fprod1", 1, f_exclusive) ); sleep(1); printf("----- FUTURE_SHARED -----\n"); /* Allocate futures */ f_shared = future_alloc(FUTURE_SHARED); if(f_shared == NULL) { printf("Future Allocation Failed\n"); return SYSERR; } /* Create the producer & consumer and put them in the ready queue */ resume( create(future_cons, 1024, 20, "fcons2", 1, f_shared) ); resume( create(future_cons, 1024, 20, "fcons3", 1, f_shared) ); resume( create(future_cons, 1024, 20, "fcons4", 1, f_shared) ); resume( create(future_cons, 1024, 20, "fcons5", 1, f_shared) ); resume( create(future_prod, 1024, 20, "fprod2", 1, f_shared) ); /* wait for a message */ //receive(); sleep(1); printf("----- FUTURE_QUEUE -----\n"); /* Allocate futures */ f_queue = future_alloc(FUTURE_QUEUE); if(f_queue == NULL) { printf("Future Allocation Failed\n"); return SYSERR; } resume( create(future_cons, 1024, 20, "fcons6", 1, f_queue) ); resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) ); resume( create(future_cons, 1024, 20, "fcons8", 1, f_queue) ); resume( create(future_cons, 1024, 20, "fcons9", 1, f_queue) ); resume( create(future_prod, 1024, 20, "fprod3", 1, f_queue) ); resume( create(future_prod, 1024, 20, "fprod4", 1, f_queue) ); resume( create(future_prod, 1024, 20, "fprod5", 1, f_queue) ); resume( create(future_prod, 1024, 20, "fprod6", 1, f_queue) ); /* wait for a message */ receive(); /* Delete the exclusive future */ future_free(f_exclusive); /* de-allocate the futures created */ future_free(f_shared); /* de-allocate the futures created */ future_free(f_queue); return OK; } else { /* Semapaphore calls */ /* default count value of 2000 */ int count = 2000; n = 0; /* Assign value of arg to count */ if (nargs == 2){ count = atoi(args[1]); } if(count <= 0) { fprintf(stderr, "Please enter a valid integer greater than 0\n"); return SYSERR; } /* initialize the semaphores */ produced = semcreate(0); consumed = semcreate(1); /* Create the producer & consumer and put them in the ready queue */ resume( create(producer, 1024, 20, "producer", 1, count) ); resume( create(consumer, 1024, 20, "consumer", 2, count, currpid) ); /* wait for a message to be received */ receive(); /* Delete the semaphores */ semdelete(produced); semdelete(consumed); } }