void *rgb (void *arg) { arg_s *a = arg; u64 start, finish; unint i; rgb_lock(a->name, &Red); start = nsecs(); for (i = Loops; i; i--) { #if (!DEBUG && !MUTEX && !SPIN && !TSPIN) donothing(); #endif rgb_lock(a->name, &Green); rgb_unlock(a->name, &Red); rgb_lock(a->name, &Blue); rgb_unlock(a->name, &Green); rgb_lock(a->name, &Red); rgb_unlock(a->name, &Blue); } rgb_unlock(a->name, &Red); finish = nsecs(); printf("%s %g nsecs per lock-unlock pair\n", a->name, ((double)(finish - start))/(3 * Loops)); return NULL; }
void *producer (void *arg) { u64 start, finish; unint wait = 0; unint i; ++P; while (P > C) { sleep(0); } printf("Producer\n"); start = nsecs(); for (i = 0; i < Loops; i++, P++) { while (P > C) { #if SLEEP sleep(0); #elif DONOTHING donothing(); #endif ++wait; } } finish = nsecs(); printf("producer %g nsecs per increment and %g waits\n", ((double)(finish - start)) / Loops, (double)wait / Loops); return NULL; }
void *rgb (void *arg) { arg_s *a = arg; u64 start, finish; unint red = a->id; unint green = a->id + 1; unint i; rgb_lock(a->name, &Lock[red]); dec_count(); rgb_lock("start", &StartLock); rgb_unlock("start", &StartLock); start = nsecs(); for (i = Loops; i; i--) { #if (!DEBUG && !MUTEX && !SPIN && !TSPIN) donothing(); #endif rgb_lock(a->name, &Lock[green]); rgb_unlock(a->name, &Lock[red]); if (++red == Num_locks) red = 0; if (++green == Num_locks) green = 0; } rgb_unlock(a->name, &Lock[red]); finish = nsecs(); printf("%s %g nsecs per lock-unlock pair\n", a->name, ((double)(finish - start))/Loops); return NULL; }
void *consumer (void *arg) { u64 start, finish; unint wait = 0; unint i; printf("Consumer\n"); start = nsecs(); for (i = 0; i < Loops; i++, C++) { while (C >= P) { #if SLEEP sleep(0); #elif DONOTHING donothing(); #endif ++wait; } } finish = nsecs(); printf("consumer %g nsecs per increment and %g waits\n", ((double)(finish - start)) / Loops, (double)wait / Loops); return NULL; }