Back to index

lightning-sunbird  0.9+nobinonly
pollable.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * A test for the pollable events.
00040  *
00041  * A number of threads are in a ring configuration, each waiting on
00042  * a pollable event that is set by its upstream neighbor.
00043  */
00044 
00045 #include "prinit.h"
00046 #include "prio.h"
00047 #include "prthread.h"
00048 #include "prerror.h"
00049 #include "prmem.h"
00050 #include "prlog.h"
00051 #include "prprf.h"
00052 
00053 #include "plgetopt.h"
00054 
00055 #include <stdlib.h>
00056 
00057 #define DEFAULT_THREADS 10
00058 #define DEFAULT_LOOPS 100
00059 
00060 PRIntn numThreads = DEFAULT_THREADS;
00061 PRIntn numIterations = DEFAULT_LOOPS;
00062 PRIntervalTime dally = PR_INTERVAL_NO_WAIT;
00063 PRFileDesc *debug_out = NULL;
00064 PRBool debug_mode = PR_FALSE;
00065 PRBool verbosity = PR_FALSE;
00066 
00067 typedef struct ThreadData {
00068     PRFileDesc *event;
00069     int index;
00070     struct ThreadData *next;
00071 } ThreadData;
00072 
00073 void ThreadRoutine(void *arg)
00074 {
00075     ThreadData *data = (ThreadData *) arg;
00076     PRIntn i;
00077     PRPollDesc pd;
00078     PRInt32 rv;
00079 
00080     pd.fd = data->event;
00081     pd.in_flags = PR_POLL_READ;
00082 
00083     for (i = 0; i < numIterations; i++) {
00084         rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
00085         if (rv == -1) {
00086             PR_fprintf(PR_STDERR, "PR_Poll failed\n");
00087             exit(1);
00088         }
00089         if (verbosity) {
00090             PR_fprintf(debug_out, "thread %d awakened\n", data->index);
00091         }
00092         PR_ASSERT(rv != 0);
00093         PR_ASSERT(pd.out_flags & PR_POLL_READ);
00094         if (PR_WaitForPollableEvent(data->event) == PR_FAILURE) {
00095             PR_fprintf(PR_STDERR, "consume event failed\n");
00096             exit(1);
00097         }
00098         if (dally != PR_INTERVAL_NO_WAIT) {
00099             PR_Sleep(dally);
00100         }
00101         if (verbosity) {
00102             PR_fprintf(debug_out, "thread %d posting event\n", data->index);
00103         }
00104         if (PR_SetPollableEvent(data->next->event) == PR_FAILURE) {
00105             PR_fprintf(PR_STDERR, "post event failed\n");
00106             exit(1);
00107         }
00108     }
00109 }
00110 
00111 static void Help(void)
00112 {
00113     debug_out = PR_STDOUT;
00114 
00115     PR_fprintf(
00116             debug_out, "Usage: pollable [-c n] [-t n] [-d] [-v] [-G] [-C n] [-D n]\n");
00117     PR_fprintf(
00118             debug_out, "-c n\tloops at thread level (default: %d)\n", DEFAULT_LOOPS);
00119     PR_fprintf(
00120             debug_out, "-t n\tnumber of threads (default: %d)\n", DEFAULT_THREADS);
00121     PR_fprintf(debug_out, "-d\tturn on debugging output (default: FALSE)\n");
00122     PR_fprintf(debug_out, "-v\tturn on verbose output (default: FALSE)\n");
00123     PR_fprintf(debug_out, "-G\tglobal threads only (default: FALSE)\n");
00124     PR_fprintf(debug_out, "-C n\tconcurrency setting (default: 1)\n");
00125     PR_fprintf(debug_out, "-D n\tdally setting (msecs) (default: 0)\n");
00126 }  /* Help */
00127 
00128 int main(int argc, char **argv)
00129 {
00130     ThreadData selfData;
00131     ThreadData *data;
00132     PRThread **thread;
00133     void *block;
00134     PRIntn i;
00135     PRIntervalTime timeStart, timeEnd;
00136     PRPollDesc pd;
00137     PRInt32 rv;
00138     PRThreadScope thread_scope = PR_LOCAL_THREAD;
00139     PRBool help = PR_FALSE;
00140     PRUintn concurrency = 1;
00141     PRUintn average;
00142     PLOptStatus os;
00143     PLOptState *opt;
00144 
00145     PR_STDIO_INIT();
00146 
00147     opt = PL_CreateOptState(argc, argv, "hdvc:t:C:GD:");
00148     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
00149         if (PL_OPT_BAD == os) {
00150             continue;
00151         }
00152         switch (opt->option) {
00153             case 'v':  /* verbose mode */
00154                 verbosity = PR_TRUE;
00155             case 'd':  /* debug mode */
00156                 debug_mode = PR_TRUE;
00157                 break;
00158             case 'c':  /* loop counter */
00159                 numIterations = atoi(opt->value);
00160                 break;
00161             case 't':  /* thread limit */
00162                 numThreads = atoi(opt->value);
00163                 break;
00164             case 'C':  /* Concurrency limit */
00165                 concurrency = atoi(opt->value);
00166                 break;
00167             case 'G':  /* global threads only */
00168                 thread_scope = PR_GLOBAL_THREAD;
00169                 break;
00170             case 'D':  /* dally */
00171                 dally = PR_MillisecondsToInterval(atoi(opt->value));
00172                 break;
00173             case 'h':  /* help message */
00174                 Help();
00175                 help = PR_TRUE;
00176                 break;
00177             default:
00178                 break;
00179         }
00180     }
00181     PL_DestroyOptState(opt);
00182 
00183     if (help) {
00184         return 1;
00185     }
00186 
00187     if (concurrency > 1) {
00188         PR_SetConcurrency(concurrency);
00189     }
00190 
00191     if (PR_TRUE == debug_mode) {
00192         debug_out = PR_STDOUT;
00193        PR_fprintf(debug_out, "Test parameters\n");
00194         PR_fprintf(debug_out, "\tThreads involved: %d\n", numThreads);
00195         PR_fprintf(debug_out, "\tIteration limit: %d\n", numIterations);
00196         PR_fprintf(debug_out, "\tConcurrency: %d\n", concurrency);
00197         PR_fprintf(debug_out, "\tThread type: %s\n",
00198                 (PR_GLOBAL_THREAD == thread_scope) ? "GLOBAL" : "LOCAL");
00199     }
00200 
00201     /*
00202      * Malloc a block of memory and divide it into data and thread.
00203      */
00204     block = PR_MALLOC(numThreads * (sizeof(ThreadData) + sizeof(PRThread *)));
00205     if (block == NULL) {
00206         PR_fprintf(PR_STDERR, "cannot malloc, failed\n");
00207         exit(1);
00208     }
00209     data = (ThreadData *) block;
00210     thread = (PRThread **) &data[numThreads];
00211 
00212     /* Pollable event */
00213     selfData.event = PR_NewPollableEvent();
00214     if (selfData.event == NULL) {
00215         PR_fprintf(PR_STDERR, "cannot create event: (%ld, %ld)\n",
00216                 PR_GetError(), PR_GetOSError());
00217         exit(1);
00218     }
00219     selfData.next = &data[0];
00220     for (i = 0; i < numThreads; i++) {
00221         data[i].event = PR_NewPollableEvent();
00222         if (data[i].event == NULL) {
00223             PR_fprintf(PR_STDERR, "cannot create event: (%ld, %ld)\n",
00224                     PR_GetError(), PR_GetOSError());
00225             exit(1);
00226         }
00227         data[i].index = i;
00228         if (i != numThreads - 1) {
00229             data[i].next = &data[i + 1];
00230         } else {
00231             data[i].next = &selfData;
00232         }
00233 
00234         thread[i] = PR_CreateThread(PR_USER_THREAD,
00235                 ThreadRoutine, &data[i], PR_PRIORITY_NORMAL,
00236                 thread_scope, PR_JOINABLE_THREAD, 0);
00237         if (thread[i] == NULL) {
00238             PR_fprintf(PR_STDERR, "cannot create thread\n");
00239             exit(1);
00240         }
00241     }
00242 
00243     timeStart = PR_IntervalNow();
00244     pd.fd = selfData.event;
00245     pd.in_flags = PR_POLL_READ;
00246     for (i = 0; i < numIterations; i++) {
00247         if (dally != PR_INTERVAL_NO_WAIT) {
00248             PR_Sleep(dally);
00249         }
00250         if (verbosity) {
00251             PR_fprintf(debug_out, "main thread posting event\n");
00252         }
00253         if (PR_SetPollableEvent(selfData.next->event) == PR_FAILURE) {
00254             PR_fprintf(PR_STDERR, "set event failed\n");
00255             exit(1);
00256         }
00257         rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
00258         if (rv == -1) {
00259             PR_fprintf(PR_STDERR, "wait failed\n");
00260             exit(1);
00261         }
00262         PR_ASSERT(rv != 0);
00263         PR_ASSERT(pd.out_flags & PR_POLL_READ);
00264         if (verbosity) {
00265             PR_fprintf(debug_out, "main thread awakened\n");
00266         }
00267        if (PR_WaitForPollableEvent(selfData.event) == PR_FAILURE) {
00268             PR_fprintf(PR_STDERR, "consume event failed\n");
00269             exit(1);
00270         }
00271     }
00272     timeEnd = PR_IntervalNow();
00273 
00274     if (debug_mode) {
00275         average = PR_IntervalToMicroseconds(timeEnd - timeStart)
00276                 / (numIterations * numThreads);
00277         PR_fprintf(debug_out, "Average switch times %d usecs for %d threads\n",
00278                 average, numThreads);
00279     }
00280 
00281     for (i = 0; i < numThreads; i++) {
00282         if (PR_JoinThread(thread[i]) == PR_FAILURE) {
00283             PR_fprintf(PR_STDERR, "join thread failed\n");
00284             exit(1);
00285         }
00286         PR_DestroyPollableEvent(data[i].event);
00287     }
00288     PR_DELETE(block);
00289        PR_DestroyPollableEvent(selfData.event);
00290 
00291     PR_fprintf(PR_STDOUT, "PASSED\n");
00292     return 0;
00293 }