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/. */
6 /***********************************************************************
7 ** 1996 - Netscape Communications Corporation
11 ** Description: Platform-specific code to create a native thread. The native thread will
12 ** repeatedly call PR_AttachThread and PR_DetachThread. The
13 ** primordial thread waits for this new thread to finish.
15 ** Modification History:
16 ** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
17 ** The debug mode will print all of the printfs associated with this test.
18 ** The regress mode will be the default mode. Since the regress tool limits
19 ** the output to a one line status:PASS or FAIL,all of the printf statements
20 ** have been handled with an if (debug_mode) statement.
21 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
22 ** recognize the return code from tha main program.
23 ** 12-June-97 Revert to return code 0 and 1.
24 ***********************************************************************/
26 /***********************************************************************
28 ***********************************************************************/
30 /* Used to get the command line option */
40 #elif defined(_PR_PTHREADS)
44 #include <sys/types.h>
45 #include <sys/prctl.h>
48 #elif defined(SOLARIS)
55 #elif defined(XP_BEOS)
56 #include <kernel/OS.h>
59 #define DEFAULT_COUNT 1000
60 PRIntn failed_already=0;
73 for (index=0;index<count; index++) {
74 me = PR_AttachThread(PR_USER_THREAD,
79 fprintf(stderr, "Error attaching thread %d: PR_AttachThread failed\n",
88 /************************************************************************/
90 static void Measure(void (*func)(void), const char *msg)
92 PRIntervalTime start, stop;
95 start = PR_IntervalNow();
97 stop = PR_IntervalNow();
99 d = (double)PR_IntervalToMicroseconds(stop - start);
101 printf("%40s: %6.2f usec\n", msg, d / count);
105 static unsigned __stdcall threadStartFunc(void *arg)
106 #elif defined(IRIX) && !defined(_PR_PTHREADS)
107 static void threadStartFunc(void *arg)
108 #elif defined(XP_BEOS)
109 static int32 threadStartFunc(void *arg)
111 static void * threadStartFunc(void *arg)
114 #ifdef _PR_DCETHREADS
117 pthread_t self = pthread_self();
118 rv = pthread_detach(&self);
119 if (debug_mode) PR_ASSERT(0 == rv);
120 else if (0 != rv) failed_already=1;
124 Measure(AttachDetach, "Attach/Detach");
131 int main(int argc, char **argv)
137 #elif defined(SOLARIS)
150 #elif defined(XP_BEOS)
156 /* The command line argument: -d is used to determine if the test is being run
157 in debug mode. The regress tool requires only one line output:PASS or FAIL.
158 All of the printfs associated with this test has been handled with a if (debug_mode)
160 Usage: test_name [-d] [-c n]
163 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:");
164 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
166 if (PL_OPT_BAD == os) continue;
169 case 'd': /* debug mode */
172 case 'c': /* loop count */
173 count = atoi(opt->value);
179 PL_DestroyOptState(opt);
182 printf("attach: This test is not valid for Win16\n");
186 if(0 == count) count = DEFAULT_COUNT;
189 * To force the implicit initialization of nspr20
195 * Platform-specific code to create a native thread. The native
196 * thread will repeatedly call PR_AttachThread and PR_DetachThread.
197 * The primordial thread waits for this new thread to finish.
202 rv = _PT_PTHREAD_ATTR_INIT(&attr);
203 if (debug_mode) PR_ASSERT(0 == rv);
209 #ifndef _PR_DCETHREADS
210 rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
211 if (debug_mode) PR_ASSERT(0 == rv);
216 #endif /* !_PR_DCETHREADS */
217 rv = _PT_PTHREAD_CREATE(&threadID, attr, threadStartFunc, NULL);
219 fprintf(stderr, "thread creation failed: error code %d\n", rv);
225 printf ("thread creation succeeded \n");
228 rv = _PT_PTHREAD_ATTR_DESTROY(&attr);
229 if (debug_mode) PR_ASSERT(0 == rv);
234 rv = pthread_join(threadID, NULL);
235 if (debug_mode) PR_ASSERT(0 == rv);
241 #elif defined(SOLARIS)
243 rv = thr_create(NULL, 0, threadStartFunc, NULL, 0, &threadID);
249 fprintf(stderr, "thread creation failed: error code %d\n", rv);
251 rv = thr_join(threadID, NULL, NULL);
252 if (debug_mode) PR_ASSERT(0 == rv);
262 hThread = (HANDLE) _beginthreadex(NULL, 0, threadStartFunc, NULL,
265 fprintf(stderr, "thread creation failed: error code %d\n",
270 rv = WaitForSingleObject(hThread, INFINITE);
271 if (debug_mode)PR_ASSERT(rv != WAIT_FAILED);
272 else if (rv == WAIT_FAILED) {
279 threadID = sproc(threadStartFunc, PR_SALL, NULL);
280 if (threadID == -1) {
282 fprintf(stderr, "thread creation failed: error code %d\n",
290 printf ("thread creation succeeded \n");
294 rv = waitpid(threadID, NULL, 0);
295 if (debug_mode) PR_ASSERT(rv != -1);
303 threadID = (TID) _beginthread((void *)threadStartFunc, NULL,
305 if (threadID == -1) {
306 fprintf(stderr, "thread creation failed: error code %d\n", errno);
310 rv = DosWaitThread(&threadID, DCWW_WAIT);
312 PR_ASSERT(rv == NO_ERROR);
313 } else if (rv != NO_ERROR) {
318 #elif defined(XP_BEOS)
320 threadID = spawn_thread(threadStartFunc, NULL, B_NORMAL_PRIORITY, NULL);
321 if (threadID <= B_ERROR) {
322 fprintf(stderr, "thread creation failed: error code %08lx\n", threadID);
326 if (resume_thread(threadID) != B_OK) {
327 fprintf(stderr, "failed starting thread: error code %08lx\n", threadID);
332 waitRV = wait_for_thread(threadID, &threadRV);
334 PR_ASSERT(waitRV == B_OK);
335 else if (waitRV != B_OK) {
344 printf("The attach test does not apply to this platform because\n"
345 "either this platform does not have native threads or the\n"
346 "test needs to be written for this platform.\n");