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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
40 ** Description: Testing various functions w/ foreign threads
42 ** We create a thread and get it to call exactly one runtime function.
43 ** The thread is allowed to be created by some other environment that
44 ** NSPR, but it does not announce itself to the runtime prior to calling
47 ** The goal: try to survive.
69 thread_nspr, thread_pthread, thread_sproc, thread_win32
72 typedef void (*StartFn)(void*);
73 typedef struct StartObject
79 static PRFileDesc *output;
81 static int _debug_on = 0;
83 #define DEFAULT_THREAD_COUNT 10
85 #define DPRINTF(arg) if (_debug_on) PR_fprintf arg
87 #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
90 static void *pthread_start(void *arg)
92 StartFn start = ((StartObject*)arg)->start;
93 void *data = ((StartObject*)arg)->arg;
98 #endif /* defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) */
100 #if defined(IRIX) && !defined(_PR_PTHREADS)
101 #include <sys/types.h>
102 #include <sys/prctl.h>
103 static void sproc_start(void *arg, PRSize size)
105 StartObject *so = (StartObject*)arg;
106 StartFn start = so->start;
107 void *data = so->arg;
111 #endif /* defined(IRIX) && !defined(_PR_PTHREADS) */
117 #include <process.h> /* for _beginthreadex() */
119 static PRUintn __stdcall windows_start(void *arg)
121 StartObject *so = (StartObject*)arg;
122 StartFn start = so->start;
123 void *data = so->arg;
127 } /* windows_start */
128 #endif /* defined(WIN32) */
130 static PRStatus NSPRPUB_TESTS_CreateThread(StartFn start, void *arg)
134 switch (thread_provider)
138 PRThread *thread = PR_CreateThread(
139 PR_USER_THREAD, start, arg,
140 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
141 PR_UNJOINABLE_THREAD, 0);
142 rv = (NULL == thread) ? PR_FAILURE : PR_SUCCESS;
146 #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
150 pthread_attr_t tattr;
151 StartObject *start_object;
152 start_object = PR_NEW(StartObject);
153 PR_ASSERT(NULL != start_object);
154 start_object->start = start;
155 start_object->arg = arg;
157 rv = _PT_PTHREAD_ATTR_INIT(&tattr);
160 rv = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
163 rv = pthread_attr_setstacksize(&tattr, 64 * 1024);
166 rv = _PT_PTHREAD_CREATE(&id, tattr, pthread_start, start_object);
167 (void)_PT_PTHREAD_ATTR_DESTROY(&tattr);
168 return (0 == rv) ? PR_SUCCESS : PR_FAILURE;
171 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
174 #endif /* defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) */
177 #if defined(IRIX) && !defined(_PR_PTHREADS)
180 StartObject *start_object;
181 start_object = PR_NEW(StartObject);
182 PR_ASSERT(NULL != start_object);
183 start_object->start = start;
184 start_object->arg = arg;
186 sproc_start, PR_SALL, start_object, NULL, 64 * 1024);
187 rv = (0 < pid) ? PR_SUCCESS : PR_FAILURE;
190 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
192 #endif /* defined(IRIX) && !defined(_PR_PTHREADS) */
199 StartObject *start_object;
200 start_object = PR_NEW(StartObject);
201 PR_ASSERT(NULL != start_object);
202 start_object->start = start;
203 start_object->arg = arg;
204 th = (void*)_beginthreadex(
205 NULL, /* LPSECURITY_ATTRIBUTES - pointer to thread security attributes */
206 0U, /* DWORD - initial thread stack size, in bytes */
207 windows_start, /* LPTHREAD_START_ROUTINE - pointer to thread function */
208 start_object, /* LPVOID - argument for new thread */
209 0U, /*DWORD dwCreationFlags - creation flags */
210 &id /* LPDWORD - pointer to returned thread identifier */ );
212 rv = (NULL == th) ? PR_FAILURE : PR_SUCCESS;
215 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
220 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
224 } /* NSPRPUB_TESTS_CreateThread */
226 static void PR_CALLBACK lazyEntry(void *arg)
228 PR_ASSERT(NULL == arg);
232 static void OneShot(void *arg)
239 PRIntn test = (PRIntn)arg;
241 for (test = 0; test < 12; ++test) {
247 DPRINTF((output,"Thread[0x%x] called PR_NewLock\n",
248 PR_GetCurrentThread()));
249 PR_DestroyLock(lock);
253 (void)PR_SecondsToInterval(1);
254 DPRINTF((output,"Thread[0x%x] called PR_SecondsToInterval\n",
255 PR_GetCurrentThread()));
258 case 2: (void)PR_CreateThread(
259 PR_USER_THREAD, lazyEntry, NULL, PR_PRIORITY_NORMAL,
260 PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0);
261 DPRINTF((output,"Thread[0x%x] called PR_CreateThread\n",
262 PR_GetCurrentThread()));
266 fd = PR_Open("foreign.tmp", PR_CREATE_FILE | PR_RDWR, 0666);
267 DPRINTF((output,"Thread[0x%x] called PR_Open\n",
268 PR_GetCurrentThread()));
273 fd = PR_NewUDPSocket();
274 DPRINTF((output,"Thread[0x%x] called PR_NewUDPSocket\n",
275 PR_GetCurrentThread()));
280 fd = PR_NewTCPSocket();
281 DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocket\n",
282 PR_GetCurrentThread()));
288 #define TEMP_DIR "c:\\data\\"
290 #define TEMP_DIR "/tmp/"
292 dir = PR_OpenDir(TEMP_DIR);
293 DPRINTF((output,"Thread[0x%x] called PR_OpenDir\n",
294 PR_GetCurrentThread()));
299 (void)PR_NewThreadPrivateIndex(&pdkey, NULL);
300 DPRINTF((output,"Thread[0x%x] called PR_NewThreadPrivateIndex\n",
301 PR_GetCurrentThread()));
305 (void)PR_GetEnv("PATH");
306 DPRINTF((output,"Thread[0x%x] called PR_GetEnv\n",
307 PR_GetCurrentThread()));
311 (void)PR_NewTCPSocketPair(pair);
312 DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocketPair\n",
313 PR_GetCurrentThread()));
319 PR_SetConcurrency(2);
320 DPRINTF((output,"Thread[0x%x] called PR_SetConcurrency\n",
321 PR_GetCurrentThread()));
325 PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_HIGH);
326 DPRINTF((output,"Thread[0x%x] called PR_SetThreadPriority\n",
327 PR_GetCurrentThread()));
336 int main(int argc, char **argv)
339 PRInt32 thread_cnt = DEFAULT_THREAD_COUNT;
341 PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
344 thread_provider = thread_win32;
345 #elif defined(_PR_PTHREADS)
346 thread_provider = thread_pthread;
348 thread_provider = thread_sproc;
350 thread_provider = thread_nspr;
354 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
356 if (PL_OPT_BAD == os) continue;
359 case 'd': /* debug mode */
362 case 't': /* thread count */
363 thread_cnt = atoi(opt->value);
369 PL_DestroyOptState(opt);
371 PR_SetConcurrency(2);
373 output = PR_GetSpecialFD(PR_StandardOutput);
375 while (thread_cnt-- > 0)
377 rv = NSPRPUB_TESTS_CreateThread(OneShot, (void*)thread_cnt);
378 PR_ASSERT(PR_SUCCESS == rv);
379 PR_Sleep(PR_MillisecondsToInterval(5));
381 PR_Sleep(PR_SecondsToInterval(3));
382 return (PR_SUCCESS == PR_Cleanup()) ? 0 : 1;