Back to index

lightning-sunbird  0.9+nobinonly
concur.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 ** File:            concur.c 
00040 ** Description:     test of adding and removing concurrency options
00041 */
00042 
00043 #include "prcvar.h"
00044 #include "prinit.h"
00045 #include "prinrval.h"
00046 #include "prlock.h"
00047 #include "prprf.h"
00048 #include "prmem.h"
00049 #include "prlog.h"
00050 
00051 #include "plgetopt.h"
00052 
00053 #if defined(XP_MAC)
00054 #include "pprio.h"
00055 #else
00056 #include "private/pprio.h"
00057 #endif
00058 
00059 #include <stdlib.h>
00060 
00061 #define DEFAULT_RANGE 10
00062 #define DEFAULT_LOOPS 100
00063 
00064 static PRThreadScope thread_scope = PR_LOCAL_THREAD;
00065 
00066 typedef struct Context
00067 {
00068     PRLock *ml;
00069     PRCondVar *cv;
00070     PRIntn want, have;
00071 } Context;
00072 
00073 
00074 /*
00075 ** Make the instance of 'context' static (not on the stack)
00076 ** for Win16 threads
00077 */
00078 static Context context = {NULL, NULL, 0, 0};
00079 
00080 static void PR_CALLBACK Dull(void *arg)
00081 {
00082     Context *context = (Context*)arg;
00083     PR_Lock(context->ml);
00084     context->have += 1;
00085     while (context->want >= context->have)
00086         PR_WaitCondVar(context->cv, PR_INTERVAL_NO_TIMEOUT);
00087     context->have -= 1;
00088     PR_Unlock(context->ml);
00089 }  /* Dull */
00090 
00091 PRIntn PR_CALLBACK Concur(PRIntn argc, char **argv)
00092 {
00093     PRUintn cpus;
00094        PLOptStatus os;
00095        PRThread **threads;
00096     PRBool debug = PR_FALSE;
00097     PRUintn range = DEFAULT_RANGE;
00098        PRStatus rc;
00099     PRUintn cnt;
00100     PRUintn loops = DEFAULT_LOOPS;
00101        PRIntervalTime hundredMills = PR_MillisecondsToInterval(100);
00102        PLOptState *opt = PL_CreateOptState(argc, argv, "Gdl:r:");
00103        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00104     {
00105               if (PL_OPT_BAD == os) continue;
00106         switch (opt->option)
00107         {
00108         case 'G':  /* GLOBAL threads */
00109                      thread_scope = PR_GLOBAL_THREAD;
00110             break;
00111         case 'd':  /* debug mode */
00112                      debug = PR_TRUE;
00113             break;
00114         case 'r':  /* range limit */
00115                      range = atoi(opt->value);
00116             break;
00117         case 'l':  /* loop counter */
00118                      loops = atoi(opt->value);
00119             break;
00120          default:
00121             break;
00122         }
00123     }
00124        PL_DestroyOptState(opt);
00125 
00126     if (0 == range) range = DEFAULT_RANGE;
00127     if (0 == loops) loops = DEFAULT_LOOPS;
00128 
00129     context.ml = PR_NewLock();
00130     context.cv = PR_NewCondVar(context.ml);
00131 
00132     if (debug)
00133         PR_fprintf(
00134             PR_STDERR, "Testing with %d CPUs and %d interations\n", range, loops);
00135 
00136        threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * range);
00137     while (--loops > 0)
00138     {
00139         for (cpus = 1; cpus <= range; ++cpus)
00140         {
00141             PR_SetConcurrency(cpus);
00142             context.want = cpus;
00143 
00144             threads[cpus - 1] = PR_CreateThread(
00145                 PR_USER_THREAD, Dull, &context, PR_PRIORITY_NORMAL,
00146                                   thread_scope, PR_JOINABLE_THREAD, 0);
00147         }
00148 
00149         PR_Sleep(hundredMills);
00150 
00151         for (cpus = range; cpus > 0; cpus--)
00152         {
00153             PR_SetConcurrency(cpus);
00154             context.want = cpus - 1;
00155 
00156             PR_Lock(context.ml);
00157             PR_NotifyCondVar(context.cv);
00158             PR_Unlock(context.ml);
00159         }
00160               for(cnt = 0; cnt < range; cnt++) {
00161                      rc = PR_JoinThread(threads[cnt]);
00162                      PR_ASSERT(rc == PR_SUCCESS);
00163               }
00164     }
00165 
00166     
00167     if (debug)
00168         PR_fprintf(
00169             PR_STDERR, "Waiting for %d thread(s) to exit\n", context.have);
00170 
00171     while (context.have > 0) PR_Sleep(hundredMills);
00172 
00173     if (debug)
00174         PR_fprintf(
00175             PR_STDERR, "Finished [want: %d, have: %d]\n",
00176             context.want, context.have);
00177 
00178     PR_DestroyLock(context.ml);
00179     PR_DestroyCondVar(context.cv);
00180     PR_DELETE(threads);
00181 
00182     PR_fprintf(PR_STDERR, "PASSED\n");
00183 
00184     return 0;
00185 } /* Concur */
00186 
00187 PRIntn main(PRIntn argc, char **argv)
00188 {
00189     PR_STDIO_INIT();
00190     return PR_Initialize(Concur, argc, argv, 0);
00191 }  /* main */
00192 
00193 /* concur.c */