|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
|
5 * |
|
6 * THE BSD LICENSE |
|
7 * |
|
8 * Redistribution and use in source and binary forms, with or without |
|
9 * modification, are permitted provided that the following conditions are met: |
|
10 * |
|
11 * Redistributions of source code must retain the above copyright notice, this |
|
12 * list of conditions and the following disclaimer. |
|
13 * Redistributions in binary form must reproduce the above copyright notice, |
|
14 * this list of conditions and the following disclaimer in the documentation |
|
15 * and/or other materials provided with the distribution. |
|
16 * |
|
17 * Neither the name of the nor the names of its contributors may be |
|
18 * used to endorse or promote products derived from this software without |
|
19 * specific prior written permission. |
|
20 * |
|
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
|
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 */ |
|
33 |
|
34 /* |
|
35 * systhr.c: Abstracted threading mechanisms |
|
36 * |
|
37 * Rob McCool |
|
38 */ |
|
39 |
|
40 |
|
41 #include "systhr.h" |
|
42 #include "ereport.h" |
|
43 |
|
44 #include "prinit.h" |
|
45 #include "prthread.h" |
|
46 #include "private/pprthred.h" |
|
47 |
|
48 #include "systems.h" |
|
49 |
|
50 #ifdef XP_UNIX |
|
51 #include <poll.h> |
|
52 #endif |
|
53 |
|
54 #ifdef THREAD_WIN32 |
|
55 #include <process.h> |
|
56 |
|
57 typedef struct { |
|
58 HANDLE hand; |
|
59 DWORD id; |
|
60 } sys_thread_s; |
|
61 |
|
62 #endif |
|
63 |
|
64 #define DEFAULT_STACKSIZE (64*1024) |
|
65 |
|
66 static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE; |
|
67 |
|
68 NSAPI_PUBLIC |
|
69 void systhread_set_default_stacksize(unsigned long size) |
|
70 { |
|
71 _systhr_stacksize = size; |
|
72 } |
|
73 |
|
74 NSAPI_PUBLIC |
|
75 SYS_THREAD systhread_start(int prio, int stksz, thrstartfunc fn, void *arg) |
|
76 { |
|
77 PRThread *ret = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))fn, |
|
78 (void *)arg, (PRThreadPriority)prio, |
|
79 PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, |
|
80 stksz ? stksz : _systhr_stacksize); |
|
81 return (void *) ret; |
|
82 } |
|
83 |
|
84 |
|
85 NSAPI_PUBLIC SYS_THREAD systhread_current(void) |
|
86 { |
|
87 return PR_GetCurrentThread(); |
|
88 } |
|
89 |
|
90 NSAPI_PUBLIC void systhread_yield(void) |
|
91 { |
|
92 PR_Sleep(PR_INTERVAL_NO_WAIT); |
|
93 } |
|
94 |
|
95 |
|
96 NSAPI_PUBLIC void systhread_timerset(int usec) |
|
97 { |
|
98 /* This is an interesting problem. If you ever do turn on interrupts |
|
99 * on the server, you're in for lots of fun with NSPR Threads |
|
100 PR_StartEvents(usec); */ |
|
101 } |
|
102 |
|
103 |
|
104 NSAPI_PUBLIC |
|
105 SYS_THREAD systhread_attach(void) |
|
106 { |
|
107 PRThread *ret; |
|
108 ret = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL); |
|
109 |
|
110 return (void *) ret; |
|
111 } |
|
112 |
|
113 NSAPI_PUBLIC |
|
114 void systhread_detach(SYS_THREAD thr) |
|
115 { |
|
116 /* XXXMB - this is not correct! */ |
|
117 PR_DetachThread(); |
|
118 } |
|
119 |
|
120 NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr) |
|
121 { |
|
122 PR_Interrupt((PRThread *)thr); |
|
123 } |
|
124 |
|
125 NSAPI_PUBLIC void systhread_sleep(int milliseconds) |
|
126 { |
|
127 #ifdef XP_WIN32 |
|
128 PR_Sleep(PR_MillisecondsToInterval(milliseconds)); |
|
129 #else |
|
130 /* poll() is more efficient than PR_Sleep() */ |
|
131 if (milliseconds > 0) |
|
132 poll(NULL, NULL, milliseconds); |
|
133 #endif |
|
134 } |
|
135 |
|
136 NSAPI_PUBLIC void systhread_init(char *name) |
|
137 { |
|
138 if (!PR_Initialized()) { |
|
139 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256); |
|
140 } |
|
141 // XXX: ruslan - this bug can potentially exist on all plafroms due to |
|
142 // possible priority inversion on NSPR spin locks. This code will |
|
143 // need to be remove as we get new NSPR drop |
|
144 // <WORKAROUND> |
|
145 /* VB: This is to fix bug# 364813 coupled with NSPR not wanting to patch |
|
146 their problems. The fix is to prevent NSPR itself from |
|
147 using atomic stacks. |
|
148 */ |
|
149 // ruslan: this problem exists on DEC also. We will roll it back when |
|
150 // we have the right fix from NSPR group. It has smth. to do with |
|
151 // atomic operations on DEC, it's an assembly code which is different |
|
152 // for every platform. NSPR_FD_CACHE_SIZE_HIGH env var will cause the |
|
153 // same effect as this fix. Debug version of NSPR always works as it doesn't |
|
154 // have FD stack. |
|
155 |
|
156 int maxPRFdCache = 8192; |
|
157 PR_SetFDCacheSize(0, maxPRFdCache); |
|
158 // </WORKAROUND> |
|
159 } |
|
160 |
|
161 |
|
162 NSAPI_PUBLIC int systhread_newkey() |
|
163 { |
|
164 uintn newkey; |
|
165 |
|
166 PR_NewThreadPrivateIndex(&newkey, NULL); |
|
167 return (newkey); |
|
168 } |
|
169 |
|
170 NSAPI_PUBLIC void *systhread_getdata(int key) |
|
171 { |
|
172 return PR_GetThreadPrivate(key); |
|
173 } |
|
174 |
|
175 NSAPI_PUBLIC void systhread_setdata(int key, void *data) |
|
176 { |
|
177 PR_SetThreadPrivate(key, data); |
|
178 } |
|
179 |
|
180 NSAPI_PUBLIC void systhread_dummy(void) |
|
181 { |
|
182 } |