struct Pool* createPool() { struct Pool* pool = malloc (sizeof (struct Pool)); pool->mutex = uthread_mutex_create(); pool->notEmpty = uthread_cond_create (pool->mutex); pool->notFull = uthread_cond_create (pool->mutex); pool->items = 0; return pool; }
struct Agent* createAgent() { struct Agent* agent = malloc (sizeof (struct Agent)); agent->mutex = uthread_mutex_create(); agent->paper = uthread_cond_create (agent->mutex); agent->match = uthread_cond_create (agent->mutex); agent->tobacco = uthread_cond_create (agent->mutex); agent->smoke = uthread_cond_create (agent->mutex); return agent; }
int main (int argc, char** argv) { // setup mutex and condition variables mx = uthread_mutex_create(); smoker_waiting = uthread_cond_create(mx); for (int i = 0; i < 3; i++) { resource[i] = uthread_cond_create(mx); } done_smoking = uthread_cond_create(mx); // setup threads and start agent uthread_init(1); uthread_mutex_lock(mx); for (int i = 0; i < 3; i++) { threads[i] = uthread_create(smoker, (void*)(intptr_t) i); uthread_cond_wait(smoker_waiting); } uthread_mutex_unlock(mx); threads[3] = uthread_create(agent, NULL); // wait for threads to finish for (int i = 3; i >= 0; i--) { uthread_join(threads[i], NULL); } // verify that the histograms of actual/expected match for (int i = 0; i < 3; i++) { assert(histo_expected[i] == histo_actual[i]); } // print histograms of expected and actual smoke counts int sum; printf("Expected: [ "); sum = 0; for (int i = 0; i < 3; i++) { sum += histo_expected[i]; printf("%d ", histo_expected[i]); } printf("]\tSum: %d\n", sum); sum = 0; printf("Actual: [ "); for (int i = 0; i < 3; i++) { sum += histo_actual[i]; printf("%d ", histo_actual[i]); } printf("]\tSum: %d\n", sum); }
int main (int argc, char** argv) { uthread_init (1); struct Washroom* washroom = createWashroom(); uthread_t pt [NUM_PEOPLE]; waitingHistogrammutex = uthread_mutex_create (); // TODO printf ("Times with 1 male %d\n", occupancyHistogram [MALE] [1]); printf ("Times with 2 males %d\n", occupancyHistogram [MALE] [2]); printf ("Times with 3 males %d\n", occupancyHistogram [MALE] [3]); printf ("Times with 1 female %d\n", occupancyHistogram [FEMALE] [1]); printf ("Times with 2 females %d\n", occupancyHistogram [FEMALE] [2]); printf ("Times with 3 females %d\n", occupancyHistogram [FEMALE] [3]); printf ("Waiting Histogram\n"); for (int i=0; i<WAITING_HISTOGRAM_SIZE; i++) if (waitingHistogram [i]) printf (" Number of times people waited for %d %s to enter: %d\n", i, i==1?"person":"people", waitingHistogram [i]); if (waitingHistogramOverflow) printf (" Number of times people waited more than %d entries: %d\n", WAITING_HISTOGRAM_SIZE, waitingHistogramOverflow); }
int main (int argc, char** argv) { assert (NUM_PRODUCERS == NUM_CONSUMERS); mx = uthread_mutex_create(); not_empty = uthread_cond_create(mx); not_full = uthread_cond_create(mx); uthread_init(NUM_PRODUCERS + NUM_CONSUMERS); uthread_t producers[NUM_PRODUCERS]; uthread_t consumers[NUM_CONSUMERS]; for (int i = 0; i < NUM_PRODUCERS; i++) { consumers[i] = uthread_create(consumer, NULL); producers[i] = uthread_create(producer, NULL); } for (int i = 0; i < NUM_PRODUCERS; i++) { uthread_join(consumers[i], NULL); uthread_join(producers[i], NULL); } uthread_cond_destroy(not_full); uthread_cond_destroy(not_empty); uthread_mutex_destroy(mx); printf("Producer Wait Time: %d\tConsumer Wait Time: %d\n", producer_wait_count, consumer_wait_count); printf("Histogram: [ "); int sum = 0; for (int i = 0; i < MAX_ITEMS + 1; i++) { sum += histogram[i]; printf("%d ", histogram[i]); } printf("]\tSum: %d\n", sum); }