Debian Codegraph @

nsprpub/pr/tests/pollable.c

raw
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 * A test for the pollable events.
8 *
9 * A number of threads are in a ring configuration, each waiting on
10 * a pollable event that is set by its upstream neighbor.
11 */
12
13 #include "prinit.h"
14 #include "prio.h"
15 #include "prthread.h"
16 #include "prerror.h"
17 #include "prmem.h"
18 #include "prlog.h"
19 #include "prprf.h"
20
21 #include "plgetopt.h"
22
23 #include <stdlib.h>
24
25 #define DEFAULT_THREADS 10
26 #define DEFAULT_LOOPS 100
27
28 PRIntn numThreads = DEFAULT_THREADS;
29 PRIntn numIterations = DEFAULT_LOOPS;
30 PRIntervalTime dally = PR_INTERVAL_NO_WAIT;
31 PRFileDesc* debug_out = NULL;
32 PRBool debug_mode = PR_FALSE;
33 PRBool verbosity = PR_FALSE;
34
35 typedef struct ThreadData {
36 PRFileDesc* event;
37 int index;
38 struct ThreadData* next;
39 } ThreadData;
40
41 void ThreadRoutine(void* arg) {
42 ThreadData* data = (ThreadData*)arg;
43 PRIntn i;
44 PRPollDesc pd;
45 PRInt32 rv;
46
47 pd.fd = data->event;
48 pd.in_flags = PR_POLL_READ;
49
50 for (i = 0; i < numIterations; i++) {
51 rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
52 if (rv == -1) {
53 PR_fprintf(PR_STDERR, "PR_Poll failed\n");
54 exit(1);
55 }
56 if (verbosity) {
57 PR_fprintf(debug_out, "thread %d awakened\n", data->index);
58 }
59 PR_ASSERT(rv != 0);
60 PR_ASSERT(pd.out_flags & PR_POLL_READ);
61 if (PR_WaitForPollableEvent(data->event) == PR_FAILURE) {
62 PR_fprintf(PR_STDERR, "consume event failed\n");
63 exit(1);
64 }
65 if (dally != PR_INTERVAL_NO_WAIT) {
66 PR_Sleep(dally);
67 }
68 if (verbosity) {
69 PR_fprintf(debug_out, "thread %d posting event\n", data->index);
70 }
71 if (PR_SetPollableEvent(data->next->event) == PR_FAILURE) {
72 PR_fprintf(PR_STDERR, "post event failed\n");
73 exit(1);
74 }
75 }
76 }
77
78 static void Help(void) {
79 debug_out = PR_STDOUT;
80
81 PR_fprintf(debug_out,
82 "Usage: pollable [-c n] [-t n] [-d] [-v] [-G] [-C n] [-D n]\n");
83 PR_fprintf(debug_out, "-c n\tloops at thread level (default: %d)\n",
84 DEFAULT_LOOPS);
85 PR_fprintf(debug_out, "-t n\tnumber of threads (default: %d)\n",
86 DEFAULT_THREADS);
87 PR_fprintf(debug_out, "-d\tturn on debugging output (default: FALSE)\n");
88 PR_fprintf(debug_out, "-v\tturn on verbose output (default: FALSE)\n");
89 PR_fprintf(debug_out, "-G\tglobal threads only (default: FALSE)\n");
90 PR_fprintf(debug_out, "-C n\tconcurrency setting (default: 1)\n");
91 PR_fprintf(debug_out, "-D n\tdally setting (msecs) (default: 0)\n");
92 } /* Help */
93
94 int main(int argc, char** argv) {
95 ThreadData selfData;
96 ThreadData* data;
97 PRThread** thread;
98 void* block;
99 PRIntn i;
100 PRIntervalTime timeStart, timeEnd;
101 PRPollDesc pd;
102 PRInt32 rv;
103 PRThreadScope thread_scope = PR_LOCAL_THREAD;
104 PRBool help = PR_FALSE;
105 PRUintn concurrency = 1;
106 PRUintn average;
107 PLOptStatus os;
108 PLOptState* opt;
109
110 PR_STDIO_INIT();
111
112 opt = PL_CreateOptState(argc, argv, "hdvc:t:C:GD:");
113 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
114 if (PL_OPT_BAD == os) {
115 continue;
116 }
117 switch (opt->option) {
118 case 'v': /* verbose mode */
119 verbosity = PR_TRUE;
120 case 'd': /* debug mode */
121 debug_mode = PR_TRUE;
122 break;
123 case 'c': /* loop counter */
124 numIterations = atoi(opt->value);
125 break;
126 case 't': /* thread limit */
127 numThreads = atoi(opt->value);
128 break;
129 case 'C': /* Concurrency limit */
130 concurrency = atoi(opt->value);
131 break;
132 case 'G': /* global threads only */
133 thread_scope = PR_GLOBAL_THREAD;
134 break;
135 case 'D': /* dally */
136 dally = PR_MillisecondsToInterval(atoi(opt->value));
137 break;
138 case 'h': /* help message */
139 Help();
140 help = PR_TRUE;
141 break;
142 default:
143 break;
144 }
145 }
146 PL_DestroyOptState(opt);
147
148 if (help) {
149 return 1;
150 }
151
152 if (concurrency > 1) {
153 PR_SetConcurrency(concurrency);
154 }
155
156 if (PR_TRUE == debug_mode) {
157 debug_out = PR_STDOUT;
158 PR_fprintf(debug_out, "Test parameters\n");
159 PR_fprintf(debug_out, "\tThreads involved: %d\n", numThreads);
160 PR_fprintf(debug_out, "\tIteration limit: %d\n", numIterations);
161 PR_fprintf(debug_out, "\tConcurrency: %d\n", concurrency);
162 PR_fprintf(debug_out, "\tThread type: %s\n",
163 (PR_GLOBAL_THREAD == thread_scope) ? "GLOBAL" : "LOCAL");
164 }
165
166 /*
167 * Malloc a block of memory and divide it into data and thread.
168 */
169 block = PR_MALLOC(numThreads * (sizeof(ThreadData) + sizeof(PRThread*)));
170 if (block == NULL) {
171 PR_fprintf(PR_STDERR, "cannot malloc, failed\n");
172 exit(1);
173 }
174 data = (ThreadData*)block;
175 thread = (PRThread**)&data[numThreads];
176
177 /* Pollable event */
178 selfData.event = PR_NewPollableEvent();
179 if (selfData.event == NULL) {
180 PR_fprintf(PR_STDERR, "cannot create event: (%ld, %ld)\n", PR_GetError(),
181 PR_GetOSError());
182 exit(1);
183 }
184 selfData.next = &data[0];
185 for (i = 0; i < numThreads; i++) {
186 data[i].event = PR_NewPollableEvent();
187 if (data[i].event == NULL) {
188 PR_fprintf(PR_STDERR, "cannot create event: (%ld, %ld)\n", PR_GetError(),
189 PR_GetOSError());
190 exit(1);
191 }
192 data[i].index = i;
193 if (i != numThreads - 1) {
194 data[i].next = &data[i + 1];
195 } else {
196 data[i].next = &selfData;
197 }
198
199 thread[i] = PR_CreateThread(PR_USER_THREAD, ThreadRoutine, &data[i],
200 PR_PRIORITY_NORMAL, thread_scope,
201 PR_JOINABLE_THREAD, 0);
202 if (thread[i] == NULL) {
203 PR_fprintf(PR_STDERR, "cannot create thread\n");
204 exit(1);
205 }
206 }
207
208 timeStart = PR_IntervalNow();
209 pd.fd = selfData.event;
210 pd.in_flags = PR_POLL_READ;
211 for (i = 0; i < numIterations; i++) {
212 if (dally != PR_INTERVAL_NO_WAIT) {
213 PR_Sleep(dally);
214 }
215 if (verbosity) {
216 PR_fprintf(debug_out, "main thread posting event\n");
217 }
218 if (PR_SetPollableEvent(selfData.next->event) == PR_FAILURE) {
219 PR_fprintf(PR_STDERR, "set event failed\n");
220 exit(1);
221 }
222 rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
223 if (rv == -1) {
224 PR_fprintf(PR_STDERR, "wait failed\n");
225 exit(1);
226 }
227 PR_ASSERT(rv != 0);
228 PR_ASSERT(pd.out_flags & PR_POLL_READ);
229 if (verbosity) {
230 PR_fprintf(debug_out, "main thread awakened\n");
231 }
232 if (PR_WaitForPollableEvent(selfData.event) == PR_FAILURE) {
233 PR_fprintf(PR_STDERR, "consume event failed\n");
234 exit(1);
235 }
236 }
237 timeEnd = PR_IntervalNow();
238
239 if (debug_mode) {
240 average = PR_IntervalToMicroseconds(timeEnd - timeStart) /
241 (numIterations * numThreads);
242 PR_fprintf(debug_out, "Average switch times %d usecs for %d threads\n",
243 average, numThreads);
244 }
245
246 for (i = 0; i < numThreads; i++) {
247 if (PR_JoinThread(thread[i]) == PR_FAILURE) {
248 PR_fprintf(PR_STDERR, "join thread failed\n");
249 exit(1);
250 }
251 PR_DestroyPollableEvent(data[i].event);
252 }
253 PR_DELETE(block);
254 PR_DestroyPollableEvent(selfData.event);
255
256 PR_fprintf(PR_STDOUT, "PASSED\n");
257 return 0;
258 }