1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 ** Description: test of adding and removing concurrency options
21 #include "private/pprio.h"
25 #define DEFAULT_RANGE 10
26 #define DEFAULT_LOOPS 100
28 static PRThreadScope thread_scope = PR_LOCAL_THREAD;
30 typedef struct Context
39 ** Make the instance of 'context' static (not on the stack)
42 static Context context = {NULL, NULL, 0, 0};
44 static void PR_CALLBACK Dull(void *arg)
46 Context *context = (Context*)arg;
49 while (context->want >= context->have)
50 PR_WaitCondVar(context->cv, PR_INTERVAL_NO_TIMEOUT);
52 PR_Unlock(context->ml);
55 PRIntn PR_CALLBACK Concur(PRIntn argc, char **argv)
60 PRBool debug = PR_FALSE;
61 PRUintn range = DEFAULT_RANGE;
64 PRUintn loops = DEFAULT_LOOPS;
65 PRIntervalTime hundredMills = PR_MillisecondsToInterval(100);
66 PLOptState *opt = PL_CreateOptState(argc, argv, "Gdl:r:");
67 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
69 if (PL_OPT_BAD == os) continue;
72 case 'G': /* GLOBAL threads */
73 thread_scope = PR_GLOBAL_THREAD;
75 case 'd': /* debug mode */
78 case 'r': /* range limit */
79 range = atoi(opt->value);
81 case 'l': /* loop counter */
82 loops = atoi(opt->value);
88 PL_DestroyOptState(opt);
90 if (0 == range) range = DEFAULT_RANGE;
91 if (0 == loops) loops = DEFAULT_LOOPS;
93 context.ml = PR_NewLock();
94 context.cv = PR_NewCondVar(context.ml);
98 PR_STDERR, "Testing with %d CPUs and %d interations\n", range, loops);
100 threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * range);
103 for (cpus = 1; cpus <= range; ++cpus)
105 PR_SetConcurrency(cpus);
108 threads[cpus - 1] = PR_CreateThread(
109 PR_USER_THREAD, Dull, &context, PR_PRIORITY_NORMAL,
110 thread_scope, PR_JOINABLE_THREAD, 0);
113 PR_Sleep(hundredMills);
115 for (cpus = range; cpus > 0; cpus--)
117 PR_SetConcurrency(cpus);
118 context.want = cpus - 1;
121 PR_NotifyCondVar(context.cv);
122 PR_Unlock(context.ml);
124 for(cnt = 0; cnt < range; cnt++) {
125 rc = PR_JoinThread(threads[cnt]);
126 PR_ASSERT(rc == PR_SUCCESS);
133 PR_STDERR, "Waiting for %d thread(s) to exit\n", context.have);
135 while (context.have > 0) PR_Sleep(hundredMills);
139 PR_STDERR, "Finished [want: %d, have: %d]\n",
140 context.want, context.have);
142 PR_DestroyLock(context.ml);
143 PR_DestroyCondVar(context.cv);
146 PR_fprintf(PR_STDERR, "PASSED\n");
151 int main(int argc, char **argv)
154 return PR_Initialize(Concur, argc, argv, 0);