Debian Codegraph @

nsprpub/pr/tests/attach.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 ** 1996 - Netscape Communications Corporation
8 **
9 ** Name: attach.c
10 **
11 ** Description: Platform-specific code to create a native thread. The native
12 *thread will
13 ** repeatedly call PR_AttachThread and
14 *PR_DetachThread. The
15 ** primordial thread waits for this new thread to
16 *finish.
17 **
18 ** Modification History:
19 ** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
20 ** The debug mode will print all of the printfs associated with this
21 *test.
22 ** The regress mode will be the default mode. Since the regress tool
23 *limits
24 ** the output to a one line status:PASS or FAIL,all of the printf
25 *statements
26 ** have been handled with an if (debug_mode) statement.
27 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
28 *updated to
29 ** recognize the return code from tha main program.
30 ** 12-June-97 Revert to return code 0 and 1.
31 ***********************************************************************/
32
33 /***********************************************************************
34 ** Includes
35 ***********************************************************************/
36
37 /* Used to get the command line option */
38 #include "nspr.h"
39 #include "pprthred.h"
40 #include "plgetopt.h"
41
42 #include <stdio.h>
43
44 #ifdef WIN32
45 # include <windows.h>
46 # include <process.h>
47 #elif defined(_PR_PTHREADS)
48 # include <pthread.h>
49 # include "md/_pth.h"
50 #elif defined(SOLARIS)
51 # include <thread.h>
52 #endif
53
54 #define DEFAULT_COUNT 1000
55 PRIntn failed_already = 0;
56 PRIntn debug_mode;
57
58 int count;
59
60 static void AttachDetach(void) {
61 PRThread* me;
62 PRInt32 index;
63
64 for (index = 0; index < count; index++) {
65 me = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL);
66
67 if (!me) {
68 fprintf(stderr, "Error attaching thread %d: PR_AttachThread failed\n",
69 count);
70 failed_already = 1;
71 return;
72 }
73 PR_DetachThread();
74 }
75 }
76
77 /************************************************************************/
78
79 static void Measure(void (*func)(void), const char* msg) {
80 PRIntervalTime start, stop;
81 double d;
82
83 start = PR_IntervalNow();
84 (*func)();
85 stop = PR_IntervalNow();
86
87 d = (double)PR_IntervalToMicroseconds(stop - start);
88 if (debug_mode) {
89 printf("%40s: %6.2f usec\n", msg, d / count);
90 }
91 }
92
93 #ifdef WIN32
94 static unsigned __stdcall threadStartFunc(void* arg)
95 #else
96 static void* threadStartFunc(void* arg)
97 #endif
98 {
99 Measure(AttachDetach, "Attach/Detach");
100
101 return 0;
102 }
103
104 int main(int argc, char** argv) {
105 #ifdef _PR_PTHREADS
106 int rv;
107 pthread_t threadID;
108 pthread_attr_t attr;
109 #elif defined(SOLARIS)
110 int rv;
111 thread_t threadID;
112 #elif defined(WIN32)
113 DWORD rv;
114 unsigned threadID;
115 HANDLE hThread;
116 #endif
117
118 /* The command line argument: -d is used to determine if the test is being run
119 in debug mode. The regress tool requires only one line output:PASS or FAIL.
120 All of the printfs associated with this test has been handled with a if
121 (debug_mode) test. Usage: test_name [-d] [-c n]
122 */
123 PLOptStatus os;
124 PLOptState* opt = PL_CreateOptState(argc, argv, "dc:");
125 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
126 if (PL_OPT_BAD == os) {
127 continue;
128 }
129 switch (opt->option) {
130 case 'd': /* debug mode */
131 debug_mode = 1;
132 break;
133 case 'c': /* loop count */
134 count = atoi(opt->value);
135 break;
136 default:
137 break;
138 }
139 }
140 PL_DestroyOptState(opt);
141
142 if (0 == count) {
143 count = DEFAULT_COUNT;
144 }
145
146 /*
147 * To force the implicit initialization of nspr20
148 */
149 PR_SetError(0, 0);
150 PR_STDIO_INIT();
151
152 /*
153 * Platform-specific code to create a native thread. The native
154 * thread will repeatedly call PR_AttachThread and PR_DetachThread.
155 * The primordial thread waits for this new thread to finish.
156 */
157
158 #ifdef _PR_PTHREADS
159
160 rv = _PT_PTHREAD_ATTR_INIT(&attr);
161 if (debug_mode) {
162 PR_ASSERT(0 == rv);
163 } else if (0 != rv) {
164 failed_already = 1;
165 goto exit_now;
166 }
167
168 rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
169 if (debug_mode) {
170 PR_ASSERT(0 == rv);
171 } else if (0 != rv) {
172 failed_already = 1;
173 goto exit_now;
174 }
175 rv = _PT_PTHREAD_CREATE(&threadID, attr, threadStartFunc, NULL);
176 if (rv != 0) {
177 fprintf(stderr, "thread creation failed: error code %d\n", rv);
178 failed_already = 1;
179 goto exit_now;
180 } else {
181 if (debug_mode) {
182 printf("thread creation succeeded \n");
183 }
184 }
185 rv = _PT_PTHREAD_ATTR_DESTROY(&attr);
186 if (debug_mode) {
187 PR_ASSERT(0 == rv);
188 } else if (0 != rv) {
189 failed_already = 1;
190 goto exit_now;
191 }
192 rv = pthread_join(threadID, NULL);
193 if (debug_mode) {
194 PR_ASSERT(0 == rv);
195 } else if (0 != rv) {
196 failed_already = 1;
197 goto exit_now;
198 }
199
200 #elif defined(SOLARIS)
201
202 rv = thr_create(NULL, 0, threadStartFunc, NULL, 0, &threadID);
203 if (rv != 0) {
204 if (!debug_mode) {
205 failed_already = 1;
206 goto exit_now;
207 } else {
208 fprintf(stderr, "thread creation failed: error code %d\n", rv);
209 }
210 }
211 rv = thr_join(threadID, NULL, NULL);
212 if (debug_mode) {
213 PR_ASSERT(0 == rv);
214 } else if (0 != rv) {
215 failed_already = 1;
216 goto exit_now;
217 }
218
219 #elif defined(WIN32)
220
221 hThread =
222 (HANDLE)_beginthreadex(NULL, 0, threadStartFunc, NULL,
223 STACK_SIZE_PARAM_IS_A_RESERVATION, &threadID);
224 if (hThread == 0) {
225 fprintf(stderr, "thread creation failed: error code %d\n", GetLastError());
226 failed_already = 1;
227 goto exit_now;
228 }
229 rv = WaitForSingleObject(hThread, INFINITE);
230 if (debug_mode) {
231 PR_ASSERT(rv != WAIT_FAILED);
232 } else if (rv == WAIT_FAILED) {
233 failed_already = 1;
234 goto exit_now;
235 }
236
237 #else
238 if (!debug_mode) {
239 failed_already = 1;
240 } else
241 printf(
242 "The attach test does not apply to this platform because\n"
243 "either this platform does not have native threads or the\n"
244 "test needs to be written for this platform.\n");
245 goto exit_now;
246 #endif
247
248 exit_now:
249 if (failed_already) {
250 return 1;
251 } else {
252 return 0;
253 }
254 }