VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/attach.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/***********************************************************************
39** 1996 - Netscape Communications Corporation
40**
41** Name: attach.c
42**
43** Description: Platform-specific code to create a native thread. The native thread will
44** repeatedly call PR_AttachThread and PR_DetachThread. The
45** primordial thread waits for this new thread to finish.
46**
47** Modification History:
48** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
49** The debug mode will print all of the printfs associated with this test.
50** The regress mode will be the default mode. Since the regress tool limits
51** the output to a one line status:PASS or FAIL,all of the printf statements
52** have been handled with an if (debug_mode) statement.
53** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
54** recognize the return code from tha main program.
55** 12-June-97 Revert to return code 0 and 1.
56***********************************************************************/
57
58/***********************************************************************
59** Includes
60***********************************************************************/
61
62/* Used to get the command line option */
63#include "nspr.h"
64#include "pprthred.h"
65#include "plgetopt.h"
66
67#include <stdio.h>
68
69#ifdef WIN32
70#include <windows.h>
71#include <process.h>
72#elif defined(_PR_PTHREADS)
73#include <pthread.h>
74#include "md/_pth.h"
75#elif defined(IRIX)
76#include <sys/types.h>
77#include <sys/prctl.h>
78#include <sys/wait.h>
79#include <errno.h>
80#elif defined(SOLARIS)
81#include <thread.h>
82#elif defined(OS2)
83#define INCL_DOS
84#define INCL_ERRORS
85#include <os2.h>
86#include <process.h>
87#elif defined(XP_BEOS)
88#include <kernel/OS.h>
89#endif
90
91#define DEFAULT_COUNT 1000
92PRIntn failed_already=0;
93PRIntn debug_mode;
94
95
96int count;
97
98
99static void
100AttachDetach(void)
101{
102 PRThread *me;
103 PRInt32 index;
104
105 for (index=0;index<count; index++) {
106 me = PR_AttachThread(PR_USER_THREAD,
107 PR_PRIORITY_NORMAL,
108 NULL);
109
110 if (!me) {
111 fprintf(stderr, "Error attaching thread %d: PR_AttachThread failed\n",
112 count);
113 failed_already = 1;
114 return;
115 }
116 PR_DetachThread();
117 }
118}
119
120/************************************************************************/
121
122static void Measure(void (*func)(void), const char *msg)
123{
124 PRIntervalTime start, stop;
125 double d;
126
127 start = PR_IntervalNow();
128 (*func)();
129 stop = PR_IntervalNow();
130
131 d = (double)PR_IntervalToMicroseconds(stop - start);
132 if (debug_mode)
133 printf("%40s: %6.2f usec\n", msg, d / count);
134}
135
136#ifdef WIN32
137static unsigned __stdcall threadStartFunc(void *arg)
138#elif defined(IRIX) && !defined(_PR_PTHREADS)
139static void threadStartFunc(void *arg)
140#elif defined(XP_BEOS)
141static int32 threadStartFunc(void *arg)
142#else
143static void * threadStartFunc(void *arg)
144#endif
145{
146#ifdef _PR_DCETHREADS
147 {
148 int rv;
149 pthread_t self = pthread_self();
150 rv = pthread_detach(&self);
151 if (debug_mode) PR_ASSERT(0 == rv);
152 else if (0 != rv) failed_already=1;
153 }
154#endif
155
156 Measure(AttachDetach, "Attach/Detach");
157
158#ifndef IRIX
159 return 0;
160#endif
161}
162
163int main(int argc, char **argv)
164{
165#ifdef _PR_PTHREADS
166 int rv;
167 pthread_t threadID;
168 pthread_attr_t attr;
169#elif defined(SOLARIS)
170 int rv;
171 thread_t threadID;
172#elif defined(WIN32)
173 DWORD rv;
174 unsigned threadID;
175 HANDLE hThread;
176#elif defined(IRIX)
177 int rv;
178 int threadID;
179#elif defined(OS2)
180 int rv;
181 TID threadID;
182#elif defined(XP_BEOS)
183 thread_id threadID;
184 int32 threadRV;
185 status_t waitRV;
186#endif
187
188 /* The command line argument: -d is used to determine if the test is being run
189 in debug mode. The regress tool requires only one line output:PASS or FAIL.
190 All of the printfs associated with this test has been handled with a if (debug_mode)
191 test.
192 Usage: test_name [-d] [-c n]
193 */
194 PLOptStatus os;
195 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:");
196 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
197 {
198 if (PL_OPT_BAD == os) continue;
199 switch (opt->option)
200 {
201 case 'd': /* debug mode */
202 debug_mode = 1;
203 break;
204 case 'c': /* loop count */
205 count = atoi(opt->value);
206 break;
207 default:
208 break;
209 }
210 }
211 PL_DestroyOptState(opt);
212
213#if defined(WIN16)
214 printf("attach: This test is not valid for Win16\n");
215 goto exit_now;
216#endif
217
218 if(0 == count) count = DEFAULT_COUNT;
219
220 /*
221 * To force the implicit initialization of nspr20
222 */
223 PR_SetError(0, 0);
224 PR_STDIO_INIT();
225
226 /*
227 * Platform-specific code to create a native thread. The native
228 * thread will repeatedly call PR_AttachThread and PR_DetachThread.
229 * The primordial thread waits for this new thread to finish.
230 */
231
232#ifdef _PR_PTHREADS
233
234 rv = _PT_PTHREAD_ATTR_INIT(&attr);
235 if (debug_mode) PR_ASSERT(0 == rv);
236 else if (0 != rv) {
237 failed_already=1;
238 goto exit_now;
239 }
240
241#ifndef _PR_DCETHREADS
242 rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
243 if (debug_mode) PR_ASSERT(0 == rv);
244 else if (0 != rv) {
245 failed_already=1;
246 goto exit_now;
247 }
248#endif /* !_PR_DCETHREADS */
249 rv = _PT_PTHREAD_CREATE(&threadID, attr, threadStartFunc, NULL);
250 if (rv != 0) {
251 fprintf(stderr, "thread creation failed: error code %d\n", rv);
252 failed_already=1;
253 goto exit_now;
254 }
255 else {
256 if (debug_mode)
257 printf ("thread creation succeeded \n");
258
259 }
260 rv = _PT_PTHREAD_ATTR_DESTROY(&attr);
261 if (debug_mode) PR_ASSERT(0 == rv);
262 else if (0 != rv) {
263 failed_already=1;
264 goto exit_now;
265 }
266 rv = pthread_join(threadID, NULL);
267 if (debug_mode) PR_ASSERT(0 == rv);
268 else if (0 != rv) {
269 failed_already=1;
270 goto exit_now;
271 }
272
273#elif defined(SOLARIS)
274
275 rv = thr_create(NULL, 0, threadStartFunc, NULL, 0, &threadID);
276 if (rv != 0) {
277 if(!debug_mode) {
278 failed_already=1;
279 goto exit_now;
280 } else
281 fprintf(stderr, "thread creation failed: error code %d\n", rv);
282 }
283 rv = thr_join(threadID, NULL, NULL);
284 if (debug_mode) PR_ASSERT(0 == rv);
285 else if (0 != rv)
286 {
287 failed_already=1;
288 goto exit_now;
289 }
290
291
292#elif defined(WIN32)
293
294 hThread = (HANDLE) _beginthreadex(NULL, 0, threadStartFunc, NULL,
295 0, &threadID);
296 if (hThread == 0) {
297 fprintf(stderr, "thread creation failed: error code %d\n",
298 GetLastError());
299 failed_already=1;
300 goto exit_now;
301 }
302 rv = WaitForSingleObject(hThread, INFINITE);
303 if (debug_mode)PR_ASSERT(rv != WAIT_FAILED);
304 else if (rv == WAIT_FAILED) {
305 failed_already=1;
306 goto exit_now;
307 }
308
309#elif defined(IRIX)
310
311 threadID = sproc(threadStartFunc, PR_SALL, NULL);
312 if (threadID == -1) {
313
314 fprintf(stderr, "thread creation failed: error code %d\n",
315 errno);
316 failed_already=1;
317 goto exit_now;
318
319 }
320 else {
321 if (debug_mode)
322 printf ("thread creation succeeded \n");
323 sleep(3);
324 goto exit_now;
325 }
326 rv = waitpid(threadID, NULL, 0);
327 if (debug_mode) PR_ASSERT(rv != -1);
328 else if (rv != -1) {
329 failed_already=1;
330 goto exit_now;
331 }
332
333#elif defined(OS2)
334
335# ifdef __EMX__
336 threadID = (TID) _beginthread((void *)threadStartFunc, NULL,
337 32768, NULL);
338# else
339 threadID = (TID) _beginthread((void(* _Optlink)(void*))threadStartFunc, NULL,
340 32768, NULL);
341# endif
342 if (threadID == -1) {
343 fprintf(stderr, "thread creation failed: error code %d\n", errno);
344 failed_already=1;
345 goto exit_now;
346 }
347 rv = DosWaitThread(&threadID, DCWW_WAIT);
348 if (debug_mode) {
349 PR_ASSERT(rv == NO_ERROR);
350 } else if (rv != NO_ERROR) {
351 failed_already=1;
352 goto exit_now;
353 }
354
355#elif defined(XP_BEOS)
356
357 threadID = spawn_thread(threadStartFunc, NULL, B_NORMAL_PRIORITY, NULL);
358 if (threadID <= B_ERROR) {
359 fprintf(stderr, "thread creation failed: error code %08lx\n", threadID);
360 failed_already = 1;
361 goto exit_now;
362 }
363 if (resume_thread(threadID) != B_OK) {
364 fprintf(stderr, "failed starting thread: error code %08lx\n", threadID);
365 failed_already = 1;
366 goto exit_now;
367 }
368
369 waitRV = wait_for_thread(threadID, &threadRV);
370 if (debug_mode)
371 PR_ASSERT(waitRV == B_OK);
372 else if (waitRV != B_OK) {
373 failed_already = 1;
374 goto exit_now;
375 }
376
377#else
378 if (!debug_mode)
379 failed_already=1;
380 else
381 printf("The attach test does not apply to this platform because\n"
382 "either this platform does not have native threads or the\n"
383 "test needs to be written for this platform.\n");
384 goto exit_now;
385#endif
386
387exit_now:
388 if(failed_already)
389 return 1;
390 else
391 return 0;
392}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette