108f90668da74e3ad8ba87a8be4ca9bd9f6143cf
[platform/upstream/nspr.git] / nspr / pr / tests / concur.c
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/. */
5
6 /*
7 ** File:            concur.c 
8 ** Description:     test of adding and removing concurrency options
9 */
10
11 #include "prcvar.h"
12 #include "prinit.h"
13 #include "prinrval.h"
14 #include "prlock.h"
15 #include "prprf.h"
16 #include "prmem.h"
17 #include "prlog.h"
18
19 #include "plgetopt.h"
20
21 #include "private/pprio.h"
22
23 #include <stdlib.h>
24
25 #define DEFAULT_RANGE 10
26 #define DEFAULT_LOOPS 100
27
28 static PRThreadScope thread_scope = PR_LOCAL_THREAD;
29
30 typedef struct Context
31 {
32     PRLock *ml;
33     PRCondVar *cv;
34     PRIntn want, have;
35 } Context;
36
37
38 /*
39 ** Make the instance of 'context' static (not on the stack)
40 ** for Win16 threads
41 */
42 static Context context = {NULL, NULL, 0, 0};
43
44 static void PR_CALLBACK Dull(void *arg)
45 {
46     Context *context = (Context*)arg;
47     PR_Lock(context->ml);
48     context->have += 1;
49     while (context->want >= context->have)
50         PR_WaitCondVar(context->cv, PR_INTERVAL_NO_TIMEOUT);
51     context->have -= 1;
52     PR_Unlock(context->ml);
53 }  /* Dull */
54
55 PRIntn PR_CALLBACK Concur(PRIntn argc, char **argv)
56 {
57     PRUintn cpus;
58         PLOptStatus os;
59         PRThread **threads;
60     PRBool debug = PR_FALSE;
61     PRUintn range = DEFAULT_RANGE;
62         PRStatus rc;
63     PRUintn cnt;
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)))
68     {
69                 if (PL_OPT_BAD == os) continue;
70         switch (opt->option)
71         {
72         case 'G':  /* GLOBAL threads */
73                         thread_scope = PR_GLOBAL_THREAD;
74             break;
75         case 'd':  /* debug mode */
76                         debug = PR_TRUE;
77             break;
78         case 'r':  /* range limit */
79                         range = atoi(opt->value);
80             break;
81         case 'l':  /* loop counter */
82                         loops = atoi(opt->value);
83             break;
84          default:
85             break;
86         }
87     }
88         PL_DestroyOptState(opt);
89
90     if (0 == range) range = DEFAULT_RANGE;
91     if (0 == loops) loops = DEFAULT_LOOPS;
92
93     context.ml = PR_NewLock();
94     context.cv = PR_NewCondVar(context.ml);
95
96     if (debug)
97         PR_fprintf(
98             PR_STDERR, "Testing with %d CPUs and %d interations\n", range, loops);
99
100         threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * range);
101     while (--loops > 0)
102     {
103         for (cpus = 1; cpus <= range; ++cpus)
104         {
105             PR_SetConcurrency(cpus);
106             context.want = cpus;
107
108             threads[cpus - 1] = PR_CreateThread(
109                 PR_USER_THREAD, Dull, &context, PR_PRIORITY_NORMAL,
110                                       thread_scope, PR_JOINABLE_THREAD, 0);
111         }
112
113         PR_Sleep(hundredMills);
114
115         for (cpus = range; cpus > 0; cpus--)
116         {
117             PR_SetConcurrency(cpus);
118             context.want = cpus - 1;
119
120             PR_Lock(context.ml);
121             PR_NotifyCondVar(context.cv);
122             PR_Unlock(context.ml);
123         }
124                 for(cnt = 0; cnt < range; cnt++) {
125                         rc = PR_JoinThread(threads[cnt]);
126                         PR_ASSERT(rc == PR_SUCCESS);
127                 }
128     }
129
130     
131     if (debug)
132         PR_fprintf(
133             PR_STDERR, "Waiting for %d thread(s) to exit\n", context.have);
134
135     while (context.have > 0) PR_Sleep(hundredMills);
136
137     if (debug)
138         PR_fprintf(
139             PR_STDERR, "Finished [want: %d, have: %d]\n",
140             context.want, context.have);
141
142     PR_DestroyLock(context.ml);
143     PR_DestroyCondVar(context.cv);
144     PR_DELETE(threads);
145
146     PR_fprintf(PR_STDERR, "PASSED\n");
147
148     return 0;
149 } /* Concur */
150
151 int main(int argc, char **argv)
152 {
153     PR_STDIO_INIT();
154     return PR_Initialize(Concur, argc, argv, 0);
155 }  /* main */
156
157 /* concur.c */