summaryrefslogtreecommitdiffstats
path: root/lib/base/systhr.cpp
blob: 169405d62921073240ef7204b3d83d678c2f4f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
/*
 * systhr.c: Abstracted threading mechanisms
 * 
 * Rob McCool
 */

#include "systhr.h"

#define USE_NSPR
#ifdef USE_NSPR
#include "nspr.h"
#include "private/prpriv.h"
extern "C" {
int32 PR_GetSysfdTableMax(void);
int32 PR_SetSysfdTableSize(int table_size);
}
#endif
#include "systems.h"

#ifdef THREAD_WIN32
#include <process.h>

typedef struct {
    HANDLE hand;
    DWORD id;
} sys_thread_s;

#endif



#if defined (USE_NSPR)


#define DEFAULT_STACKSIZE (64*1024)

static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE;

NSAPI_PUBLIC 
void systhread_set_default_stacksize(unsigned long size)
{
	_systhr_stacksize = size;
}

NSPR_BEGIN_EXTERN_C

NSAPI_PUBLIC SYS_THREAD
#ifdef UnixWare /* for ANSI C++ standard, see base/systrh.h */
systhread_start(int prio, int stksz, ArgFn_systhread_start fn, void *arg)
#else
systhread_start(int prio, int stksz, void (*fn)(void *), void *arg)
#endif
{
#if (defined(Linux) || defined(SNI) || defined(UnixWare)) && !defined(USE_PTHREADS)
    prio /= 8; /* quick and dirty fix for user thread priority scale problem */
    if (prio > 3) prio = 3;
#endif

    PRThread *ret = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))fn,
				    (void *)arg, (PRThreadPriority)prio, 
				    PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
                                    stksz ? stksz : _systhr_stacksize);
    return (void *) ret;
}

NSPR_END_EXTERN_C


NSAPI_PUBLIC SYS_THREAD systhread_current(void)
{
    return PR_GetCurrentThread();
}

NSAPI_PUBLIC void systhread_yield(void)
{
  /* PR_Yield(); */
  PR_Sleep(PR_INTERVAL_NO_WAIT);
}


NSAPI_PUBLIC void systhread_timerset(int usec)
{
   /* This is an interesting problem.  If you ever do turn on interrupts
    * on the server, you're in for lots of fun with NSPR Threads
   PR_StartEvents(usec); */
}


NSAPI_PUBLIC 
SYS_THREAD systhread_attach(void)
{
    PRThread *ret;
    ret = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL);

    return (void *) ret;
}

NSAPI_PUBLIC
void systhread_detach(SYS_THREAD thr)
{
    /* XXXMB - this is not correct! */
    PR_DetachThread();
}

NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
{

    /* Should never be here. PR_DestroyThread is no 
     * longer used. */
    PR_ASSERT(0);
  
    /* PR_DestroyThread((PRThread *) thr); */
}

NSAPI_PUBLIC void systhread_sleep(int milliseconds)
{
    PR_Sleep(milliseconds);
}

NSAPI_PUBLIC void systhread_init(char *name)
{
    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
#ifdef XP_UNIX
    /* XXXrobm allocate all the fd's we can... */
    PR_SetSysfdTableSize(PR_GetSysfdTableMax());
#endif
}


NSAPI_PUBLIC int systhread_newkey()
{
    uintn newkey;

    PR_NewThreadPrivateIndex(&newkey, NULL);
    return (newkey);
}

NSAPI_PUBLIC void *systhread_getdata(int key)
{
    return PR_GetThreadPrivate(key);
}

NSAPI_PUBLIC void systhread_setdata(int key, void *data)
{
    PR_SetThreadPrivate(key, data);
}

/* 
 * Drag in the Java code, so our dynamic library full of it works 
 * i.e. force these symbols to load.
 */
NSAPI_PUBLIC void systhread_dummy(void)
{

#ifndef NSPR20
    /* nspr/gc.c */
    PR_InitGC(0,0);
    /* nspr/prsystem.c */
    PR_GetSystemInfo(PR_SI_SYSNAME, 0, 0);
    /* nspr/linker.c */
    PR_GetLibName(0, 0);
    /* nspr/file.c */
    PR_Mkdir(0, 0);
    /* nspr/prnetdb.c */
    PR_gethostbyname(0, 0, 0, 0, 0);
    /* nspr/longlong.c */
    LL_TO_S(LL_ZERO, 0, NULL, 0);
#endif /* NSPR20 */
}

#elif defined(THREAD_WIN32)

#include <nspr/prthread.h>
#define DEFAULT_STACKSIZE 262144

NSPR_BEGIN_EXTERN_C

NSAPI_PUBLIC 
SYS_THREAD systhread_start(int prio, int stksz, void (*fn)(void *), void *arg)
{
    sys_thread_s *ret = (sys_thread_s *) MALLOC(sizeof(sys_thread_s));

    if ((ret->hand = (HANDLE)_beginthreadex(NULL, stksz, (unsigned (__stdcall *)(void *))fn, 
	                                        arg, 0, &ret->id)) == 0) {
        FREE(ret);
        return NULL;
    }
    return (void *)ret;
}

NSPR_END_EXTERN_C

NSAPI_PUBLIC SYS_THREAD systhread_current(void)
{
    /* XXXrobm this is busted.... */
    return GetCurrentThread();
}

NSAPI_PUBLIC void systhread_timerset(int usec)
{
}

NSAPI_PUBLIC SYS_THREAD systhread_attach(void)
{
    return NULL;
}

NSAPI_PUBLIC void systhread_yield(void)
{
    systhread_sleep(0);
}

NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
{
    TerminateThread(((sys_thread_s *)thr)->hand, 0);
}


NSAPI_PUBLIC void systhread_sleep(int milliseconds)
{
    /* XXXrobm there must be a better way to do this */
    HANDLE sem = CreateSemaphore(NULL, 1, 4, "sleeper");
    WaitForSingleObject(sem, INFINITE);
    WaitForSingleObject(sem, milliseconds);
    CloseHandle(sem);
}

NSAPI_PUBLIC void systhread_init(char *name)
{
    PR_Init(PR_USER_THREAD, 1, 0);
}


NSAPI_PUBLIC int systhread_newkey()
{
    return TlsAlloc();
}

NSAPI_PUBLIC void *systhread_getdata(int key)
{
    return (void *)TlsGetValue(key);
}

NSAPI_PUBLIC void systhread_setdata(int key, void *data)
{
    TlsSetValue(key, data);
}

#endif