Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / tests / foreign.c
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 ** File:        foreign.c
40 ** Description: Testing various functions w/ foreign threads
41 **
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
45 **      in.
46 **
47 **      The goal: try to survive.
48 **      
49 */
50
51 #include "prcvar.h"
52 #include "prenv.h"
53 #include "prerror.h"
54 #include "prinit.h"
55 #include "prinrval.h"
56 #include "prio.h"
57 #include "prlock.h"
58 #include "prlog.h"
59 #include "prmem.h"
60 #include "prthread.h"
61 #include "prtypes.h"
62 #include "prprf.h"
63 #include "plgetopt.h"
64
65 #include <stdio.h>
66 #include <stdlib.h>
67
68 static enum {
69     thread_nspr, thread_pthread, thread_sproc, thread_win32
70 } thread_provider;
71
72 typedef void (*StartFn)(void*);
73 typedef struct StartObject
74 {
75     StartFn start;
76     void *arg;
77 } StartObject;
78
79 static PRFileDesc *output;
80
81 static int _debug_on = 0;
82
83 #define DEFAULT_THREAD_COUNT    10
84
85 #define DPRINTF(arg)    if (_debug_on) PR_fprintf arg
86
87 #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
88 #include <pthread.h>
89 #include "md/_pth.h"
90 static void *pthread_start(void *arg)
91 {
92     StartFn start = ((StartObject*)arg)->start;
93     void *data = ((StartObject*)arg)->arg;
94     PR_Free(arg);
95     start(data);
96     return NULL;
97 }  /* pthread_start */
98 #endif /* defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) */
99
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)
104 {
105     StartObject *so = (StartObject*)arg;
106     StartFn start = so->start;
107     void *data = so->arg;
108     PR_Free(so);
109     start(data);
110 }  /* sproc_start */
111 #endif  /* defined(IRIX) && !defined(_PR_PTHREADS) */
112
113 #if defined(WIN32)
114 #if defined(WINCE)
115 #include <windows.h>
116 #endif
117 #include <process.h>  /* for _beginthreadex() */
118
119 static PRUintn __stdcall windows_start(void *arg)
120 {
121     StartObject *so = (StartObject*)arg;
122     StartFn start = so->start;
123     void *data = so->arg;
124     PR_Free(so);
125     start(data);
126     return 0;
127 }  /* windows_start */
128 #endif /* defined(WIN32) */
129
130 static PRStatus NSPRPUB_TESTS_CreateThread(StartFn start, void *arg)
131 {
132     PRStatus rv;
133
134     switch (thread_provider)
135     {
136     case thread_nspr:
137         {
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;
143         }
144         break;
145     case thread_pthread:
146 #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
147         {
148             int rv;
149             pthread_t id;
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;
156
157             rv = _PT_PTHREAD_ATTR_INIT(&tattr);
158             PR_ASSERT(0 == rv);
159
160             rv = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
161             PR_ASSERT(0 == rv);
162
163             rv = pthread_attr_setstacksize(&tattr, 64 * 1024);
164             PR_ASSERT(0 == rv);
165
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;
169         }
170 #else
171         PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
172         rv = PR_FAILURE;
173         break;
174 #endif /* defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) */
175
176     case thread_sproc:
177 #if defined(IRIX) && !defined(_PR_PTHREADS)
178         {
179             PRInt32 pid;
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;
185             pid = sprocsp(
186                 sproc_start, PR_SALL, start_object, NULL, 64 * 1024);
187             rv = (0 < pid) ? PR_SUCCESS : PR_FAILURE;
188         }
189 #else
190         PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
191         rv = PR_FAILURE;
192 #endif  /* defined(IRIX) && !defined(_PR_PTHREADS) */
193         break;
194     case thread_win32:
195 #if defined(WIN32)
196         {
197             void *th;
198             PRUintn id;       
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 */ );
211
212             rv = (NULL == th) ? PR_FAILURE : PR_SUCCESS;
213         }
214 #else
215         PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
216         rv = PR_FAILURE;
217 #endif
218         break;
219     default:
220         PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
221         rv = PR_FAILURE;
222     }
223     return rv;
224 }  /* NSPRPUB_TESTS_CreateThread */
225
226 static void PR_CALLBACK lazyEntry(void *arg)
227 {
228     PR_ASSERT(NULL == arg);
229 }  /* lazyEntry */
230
231
232 static void OneShot(void *arg)
233 {
234     PRUintn pdkey;
235     PRLock *lock;
236     PRFileDesc *fd;
237     PRDir *dir;
238     PRFileDesc *pair[2];
239     PRIntn test = (PRIntn)arg;
240
241         for (test = 0; test < 12; ++test) {
242
243     switch (test)
244     {
245         case 0:
246             lock = PR_NewLock(); 
247                         DPRINTF((output,"Thread[0x%x] called PR_NewLock\n",
248                         PR_GetCurrentThread()));
249             PR_DestroyLock(lock);
250             break;
251             
252         case 1:
253             (void)PR_SecondsToInterval(1);
254                         DPRINTF((output,"Thread[0x%x] called PR_SecondsToInterval\n",
255                         PR_GetCurrentThread()));
256             break;
257             
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()));
263             break;
264             
265         case 3:
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()));
269             PR_Close(fd);
270             break;
271             
272         case 4:
273             fd = PR_NewUDPSocket(); 
274                         DPRINTF((output,"Thread[0x%x] called PR_NewUDPSocket\n",
275                         PR_GetCurrentThread()));
276             PR_Close(fd);
277             break;
278             
279         case 5:
280             fd = PR_NewTCPSocket(); 
281                         DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocket\n",
282                         PR_GetCurrentThread()));
283             PR_Close(fd);
284             break;
285             
286         case 6:
287 #ifdef SYMBIAN
288 #define TEMP_DIR "c:\\data\\"
289 #else
290 #define TEMP_DIR "/tmp/"
291 #endif
292             dir = PR_OpenDir(TEMP_DIR);
293                         DPRINTF((output,"Thread[0x%x] called PR_OpenDir\n",
294                         PR_GetCurrentThread()));
295             PR_CloseDir(dir);
296             break;
297             
298         case 7:
299             (void)PR_NewThreadPrivateIndex(&pdkey, NULL);
300                         DPRINTF((output,"Thread[0x%x] called PR_NewThreadPrivateIndex\n",
301                         PR_GetCurrentThread()));
302             break;
303         
304         case 8:
305             (void)PR_GetEnv("PATH");
306                         DPRINTF((output,"Thread[0x%x] called PR_GetEnv\n",
307                         PR_GetCurrentThread()));
308             break;
309             
310         case 9:
311             (void)PR_NewTCPSocketPair(pair);
312                         DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocketPair\n",
313                         PR_GetCurrentThread()));
314             PR_Close(pair[0]);
315             PR_Close(pair[1]);
316             break;
317             
318         case 10:
319             PR_SetConcurrency(2);
320                         DPRINTF((output,"Thread[0x%x] called PR_SetConcurrency\n",
321                         PR_GetCurrentThread()));
322             break;
323
324         case 11:
325             PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_HIGH);
326                         DPRINTF((output,"Thread[0x%x] called PR_SetThreadPriority\n",
327                         PR_GetCurrentThread()));
328             break;
329             
330         default: 
331             break;
332     } /* switch() */
333         }
334 }  /* OneShot */
335
336 int main(int argc, char **argv)
337 {
338     PRStatus rv;
339         PRInt32 thread_cnt = DEFAULT_THREAD_COUNT;
340         PLOptStatus os;
341         PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
342
343 #if defined(WIN32)
344         thread_provider = thread_win32;
345 #elif defined(_PR_PTHREADS)
346         thread_provider = thread_pthread;
347 #elif defined(IRIX)
348         thread_provider = thread_sproc;
349 #else
350     thread_provider = thread_nspr;
351 #endif
352
353
354         while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
355     {
356                 if (PL_OPT_BAD == os) continue;
357         switch (opt->option)
358         {
359         case 'd':  /* debug mode */
360                         _debug_on = 1;
361             break;
362         case 't':  /* thread count */
363             thread_cnt = atoi(opt->value);
364             break;
365          default:
366             break;
367         }
368     }
369         PL_DestroyOptState(opt);
370
371         PR_SetConcurrency(2);
372
373         output = PR_GetSpecialFD(PR_StandardOutput);
374
375     while (thread_cnt-- > 0)
376     {
377         rv = NSPRPUB_TESTS_CreateThread(OneShot, (void*)thread_cnt);
378         PR_ASSERT(PR_SUCCESS == rv);
379         PR_Sleep(PR_MillisecondsToInterval(5));
380     }
381     PR_Sleep(PR_SecondsToInterval(3));
382     return (PR_SUCCESS == PR_Cleanup()) ? 0 : 1;
383 }  /* main */
384
385 /* foreign.c */