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 *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 start_threads (unsigned threads) { pthread_t *thread; unsigned i; int rc; arg_s *arg; arg_s *a; rgb_lock("main", &Red); thread = ezalloc(threads * sizeof(pthread_t)); arg = ezalloc(threads * sizeof(arg_s)); for (i = 0, a = arg; i < threads; i++, a++) { sprintf(a->name, "rgb%d", i); rc = pthread_create( &thread[i], NULL, rgb, a); if (rc) { eprintf("pthread_create %d\n", rc); break; } } sleep(1); rgb_unlock("main", &Red); while (i--) { pthread_join(thread[i], NULL); } }
void start_threads (unsigned threads) { pthread_t *thread; unsigned i; int rc; arg_s *arg; arg_s *a; Count = threads; rgb_lock("start", &StartLock); thread = ezalloc(threads * sizeof(pthread_t)); arg = ezalloc(threads * sizeof(arg_s)); for (i = 0, a = arg; i < threads; i++, a++) { sprintf(a->name, "rgb%d", i); a->id = i; rc = pthread_create( &thread[i], NULL, rgb, a); if (rc) { eprintf("pthread_create %d\n", rc); break; } } for (i = 0; Count; i++) { sleep(1); } rgb_unlock("start", &StartLock); for (i = 0; i < threads; i++) { pthread_join(thread[i], NULL); } }
static void dec_count(void) { rgb_lock("count", &CountLock); --Count; rgb_unlock("count", &CountLock); }