int main(int argc, char **argv) { int sent, checks, i; sthread_t child[MAXTHREADS]; printf("Testing sthread_cond_*, impl: %s\n", (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user"); assert(num_threads <= MAXTHREADS); sthread_init(); mutex = sthread_mutex_init(); avail_cond = sthread_cond_init(); sthread_mutex_lock(mutex); for (i = 0; i < num_threads; i++) { child[i] = sthread_create(thread_start, NULL, 1); if (child[i] == NULL) { printf("sthread_create %d failed\n", i); exit(1); } } assert(transfered == 0); /* This should let the other thread run at some point. */ sthread_mutex_unlock(mutex); /* Send a bunch of things for the other threads to take */ sent = 0; while (sent < max_transfer) { sthread_mutex_lock(mutex); waiting++; sent++; sthread_cond_signal(avail_cond); sthread_mutex_unlock(mutex); sthread_yield(); } printf("Sent %d\n", sent); /* Now give the other threads 100 tries to get * them all across. We assume that's enough * for the sake of not running this test forever. */ checks = 10000; //arbitrary?? while (checks > 0) { sthread_mutex_lock(mutex); if (transfered != max_transfer) checks--; else { /* broadcast to let the consumers know we've * finished, so they can exit * (othrewise, they may still be holding the lock * when we try to free it below) */ sthread_cond_broadcast(avail_cond); checks = -1; } sthread_mutex_unlock(mutex); sthread_yield(); } if (checks == -1) { /* Wait for child threads to finish, otherwise we could try to * free the mutex before they've unlocked it! */ printf("joining on children\n"); for (i = 0; i < num_threads; i++) { sthread_join(child[i]); printf("joined with child %d\n", i); } printf("sthread_cond passed\n"); } else { printf("*** sthread_cond failed\n"); /* If we failed, don't bother joining on threads. */ } sthread_mutex_free(mutex); sthread_cond_free(avail_cond); return 0; }
int sthread_cond_broadcast(sthread_cond_t *cond) { return sthread_cond_signal(cond); }