michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A test for the pollable events. michael@0: * michael@0: * A number of threads are in a ring configuration, each waiting on michael@0: * a pollable event that is set by its upstream neighbor. michael@0: */ michael@0: michael@0: #include "prinit.h" michael@0: #include "prio.h" michael@0: #include "prthread.h" michael@0: #include "prerror.h" michael@0: #include "prmem.h" michael@0: #include "prlog.h" michael@0: #include "prprf.h" michael@0: michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: michael@0: #define DEFAULT_THREADS 10 michael@0: #define DEFAULT_LOOPS 100 michael@0: michael@0: PRIntn numThreads = DEFAULT_THREADS; michael@0: PRIntn numIterations = DEFAULT_LOOPS; michael@0: PRIntervalTime dally = PR_INTERVAL_NO_WAIT; michael@0: PRFileDesc *debug_out = NULL; michael@0: PRBool debug_mode = PR_FALSE; michael@0: PRBool verbosity = PR_FALSE; michael@0: michael@0: typedef struct ThreadData { michael@0: PRFileDesc *event; michael@0: int index; michael@0: struct ThreadData *next; michael@0: } ThreadData; michael@0: michael@0: void ThreadRoutine(void *arg) michael@0: { michael@0: ThreadData *data = (ThreadData *) arg; michael@0: PRIntn i; michael@0: PRPollDesc pd; michael@0: PRInt32 rv; michael@0: michael@0: pd.fd = data->event; michael@0: pd.in_flags = PR_POLL_READ; michael@0: michael@0: for (i = 0; i < numIterations; i++) { michael@0: rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (rv == -1) { michael@0: PR_fprintf(PR_STDERR, "PR_Poll failed\n"); michael@0: exit(1); michael@0: } michael@0: if (verbosity) { michael@0: PR_fprintf(debug_out, "thread %d awakened\n", data->index); michael@0: } michael@0: PR_ASSERT(rv != 0); michael@0: PR_ASSERT(pd.out_flags & PR_POLL_READ); michael@0: if (PR_WaitForPollableEvent(data->event) == PR_FAILURE) { michael@0: PR_fprintf(PR_STDERR, "consume event failed\n"); michael@0: exit(1); michael@0: } michael@0: if (dally != PR_INTERVAL_NO_WAIT) { michael@0: PR_Sleep(dally); michael@0: } michael@0: if (verbosity) { michael@0: PR_fprintf(debug_out, "thread %d posting event\n", data->index); michael@0: } michael@0: if (PR_SetPollableEvent(data->next->event) == PR_FAILURE) { michael@0: PR_fprintf(PR_STDERR, "post event failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void Help(void) michael@0: { michael@0: debug_out = PR_STDOUT; michael@0: michael@0: PR_fprintf( michael@0: debug_out, "Usage: pollable [-c n] [-t n] [-d] [-v] [-G] [-C n] [-D n]\n"); michael@0: PR_fprintf( michael@0: debug_out, "-c n\tloops at thread level (default: %d)\n", DEFAULT_LOOPS); michael@0: PR_fprintf( michael@0: debug_out, "-t n\tnumber of threads (default: %d)\n", DEFAULT_THREADS); michael@0: PR_fprintf(debug_out, "-d\tturn on debugging output (default: FALSE)\n"); michael@0: PR_fprintf(debug_out, "-v\tturn on verbose output (default: FALSE)\n"); michael@0: PR_fprintf(debug_out, "-G\tglobal threads only (default: FALSE)\n"); michael@0: PR_fprintf(debug_out, "-C n\tconcurrency setting (default: 1)\n"); michael@0: PR_fprintf(debug_out, "-D n\tdally setting (msecs) (default: 0)\n"); michael@0: } /* Help */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: ThreadData selfData; michael@0: ThreadData *data; michael@0: PRThread **thread; michael@0: void *block; michael@0: PRIntn i; michael@0: PRIntervalTime timeStart, timeEnd; michael@0: PRPollDesc pd; michael@0: PRInt32 rv; michael@0: PRThreadScope thread_scope = PR_LOCAL_THREAD; michael@0: PRBool help = PR_FALSE; michael@0: PRUintn concurrency = 1; michael@0: PRUintn average; michael@0: PLOptStatus os; michael@0: PLOptState *opt; michael@0: michael@0: PR_STDIO_INIT(); michael@0: michael@0: opt = PL_CreateOptState(argc, argv, "hdvc:t:C:GD:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { michael@0: if (PL_OPT_BAD == os) { michael@0: continue; michael@0: } michael@0: switch (opt->option) { michael@0: case 'v': /* verbose mode */ michael@0: verbosity = PR_TRUE; michael@0: case 'd': /* debug mode */ michael@0: debug_mode = PR_TRUE; michael@0: break; michael@0: case 'c': /* loop counter */ michael@0: numIterations = atoi(opt->value); michael@0: break; michael@0: case 't': /* thread limit */ michael@0: numThreads = atoi(opt->value); michael@0: break; michael@0: case 'C': /* Concurrency limit */ michael@0: concurrency = atoi(opt->value); michael@0: break; michael@0: case 'G': /* global threads only */ michael@0: thread_scope = PR_GLOBAL_THREAD; michael@0: break; michael@0: case 'D': /* dally */ michael@0: dally = PR_MillisecondsToInterval(atoi(opt->value)); michael@0: break; michael@0: case 'h': /* help message */ michael@0: Help(); michael@0: help = PR_TRUE; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: if (help) { michael@0: return 1; michael@0: } michael@0: michael@0: if (concurrency > 1) { michael@0: PR_SetConcurrency(concurrency); michael@0: } michael@0: michael@0: if (PR_TRUE == debug_mode) { michael@0: debug_out = PR_STDOUT; michael@0: PR_fprintf(debug_out, "Test parameters\n"); michael@0: PR_fprintf(debug_out, "\tThreads involved: %d\n", numThreads); michael@0: PR_fprintf(debug_out, "\tIteration limit: %d\n", numIterations); michael@0: PR_fprintf(debug_out, "\tConcurrency: %d\n", concurrency); michael@0: PR_fprintf(debug_out, "\tThread type: %s\n", michael@0: (PR_GLOBAL_THREAD == thread_scope) ? "GLOBAL" : "LOCAL"); michael@0: } michael@0: michael@0: /* michael@0: * Malloc a block of memory and divide it into data and thread. michael@0: */ michael@0: block = PR_MALLOC(numThreads * (sizeof(ThreadData) + sizeof(PRThread *))); michael@0: if (block == NULL) { michael@0: PR_fprintf(PR_STDERR, "cannot malloc, failed\n"); michael@0: exit(1); michael@0: } michael@0: data = (ThreadData *) block; michael@0: thread = (PRThread **) &data[numThreads]; michael@0: michael@0: /* Pollable event */ michael@0: selfData.event = PR_NewPollableEvent(); michael@0: if (selfData.event == NULL) { michael@0: PR_fprintf(PR_STDERR, "cannot create event: (%ld, %ld)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: selfData.next = &data[0]; michael@0: for (i = 0; i < numThreads; i++) { michael@0: data[i].event = PR_NewPollableEvent(); michael@0: if (data[i].event == NULL) { michael@0: PR_fprintf(PR_STDERR, "cannot create event: (%ld, %ld)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: data[i].index = i; michael@0: if (i != numThreads - 1) { michael@0: data[i].next = &data[i + 1]; michael@0: } else { michael@0: data[i].next = &selfData; michael@0: } michael@0: michael@0: thread[i] = PR_CreateThread(PR_USER_THREAD, michael@0: ThreadRoutine, &data[i], PR_PRIORITY_NORMAL, michael@0: thread_scope, PR_JOINABLE_THREAD, 0); michael@0: if (thread[i] == NULL) { michael@0: PR_fprintf(PR_STDERR, "cannot create thread\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: timeStart = PR_IntervalNow(); michael@0: pd.fd = selfData.event; michael@0: pd.in_flags = PR_POLL_READ; michael@0: for (i = 0; i < numIterations; i++) { michael@0: if (dally != PR_INTERVAL_NO_WAIT) { michael@0: PR_Sleep(dally); michael@0: } michael@0: if (verbosity) { michael@0: PR_fprintf(debug_out, "main thread posting event\n"); michael@0: } michael@0: if (PR_SetPollableEvent(selfData.next->event) == PR_FAILURE) { michael@0: PR_fprintf(PR_STDERR, "set event failed\n"); michael@0: exit(1); michael@0: } michael@0: rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (rv == -1) { michael@0: PR_fprintf(PR_STDERR, "wait failed\n"); michael@0: exit(1); michael@0: } michael@0: PR_ASSERT(rv != 0); michael@0: PR_ASSERT(pd.out_flags & PR_POLL_READ); michael@0: if (verbosity) { michael@0: PR_fprintf(debug_out, "main thread awakened\n"); michael@0: } michael@0: if (PR_WaitForPollableEvent(selfData.event) == PR_FAILURE) { michael@0: PR_fprintf(PR_STDERR, "consume event failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: timeEnd = PR_IntervalNow(); michael@0: michael@0: if (debug_mode) { michael@0: average = PR_IntervalToMicroseconds(timeEnd - timeStart) michael@0: / (numIterations * numThreads); michael@0: PR_fprintf(debug_out, "Average switch times %d usecs for %d threads\n", michael@0: average, numThreads); michael@0: } michael@0: michael@0: for (i = 0; i < numThreads; i++) { michael@0: if (PR_JoinThread(thread[i]) == PR_FAILURE) { michael@0: PR_fprintf(PR_STDERR, "join thread failed\n"); michael@0: exit(1); michael@0: } michael@0: PR_DestroyPollableEvent(data[i].event); michael@0: } michael@0: PR_DELETE(block); michael@0: PR_DestroyPollableEvent(selfData.event); michael@0: michael@0: PR_fprintf(PR_STDOUT, "PASSED\n"); michael@0: return 0; michael@0: }