src/server/util/systhr.c

changeset 29
e8619defde14
parent 14
b8bf95b39952
child 40
56cda23f48d4
equal deleted inserted replaced
28:f387669912e8 29:e8619defde14
37 * Rob McCool 37 * Rob McCool
38 */ 38 */
39 39
40 40
41 #include "systhr.h" 41 #include "systhr.h"
42 #include "ereport.h" 42 //include "ereport.h"
43 43
44 #include "prinit.h" 44 #include "prinit.h"
45 #include "prthread.h" 45 #include "prthread.h"
46 #include "private/pprthred.h" 46 #include "private/pprthred.h"
47 47
48 #include "systems.h" 48 #include "systems.h"
49 49
50 #ifdef XP_UNIX 50 #ifdef XP_UNIX
51 #include <poll.h> 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 52 #endif
63 53
64 #define DEFAULT_STACKSIZE (64*1024) 54 #define DEFAULT_STACKSIZE (64*1024)
65 55
66 static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE; 56 static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE;
69 void systhread_set_default_stacksize(unsigned long size) 59 void systhread_set_default_stacksize(unsigned long size)
70 { 60 {
71 _systhr_stacksize = size; 61 _systhr_stacksize = size;
72 } 62 }
73 63
74 NSAPI_PUBLIC 64
75 SYS_THREAD systhread_start(int prio, int stksz, thrstartfunc fn, void *arg) 65
66 SYS_THREAD systhread_start(int prio, int stksz, thrstartfunc fn, void *arg) {
67 pthread_t thr = 0;
68 pthread_attr_t attr;
69
70 pthread_attr_init(&attr);
71 if(stksz) {
72 pthread_attr_setstacksize(&attr, stksz);
73 }
74
75 if(pthread_create(&thr, &attr, (posix_thrstartfunc)fn, arg) != 0) {
76 /* error */
77 }
78
79 return thr;
80 }
81
82 SYS_THREAD systhread_current(void) {
83 return pthread_self();
84 }
85
86 void systhread_yield(void)
76 { 87 {
77 PRThread *ret = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))fn, 88 sched_yield();
78 (void *)arg, (PRThreadPriority)prio,
79 PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
80 stksz ? stksz : _systhr_stacksize);
81 return (void *) ret;
82 } 89 }
83 90
84 91
85 NSAPI_PUBLIC SYS_THREAD systhread_current(void) 92 void systhread_timerset(int usec)
86 { 93 {
87 return PR_GetCurrentThread(); 94
88 } 95 }
89 96
90 NSAPI_PUBLIC void systhread_yield(void) 97 SYS_THREAD systhread_attach(void)
91 { 98 {
92 PR_Sleep(PR_INTERVAL_NO_WAIT); 99 /* TODO: what to do? */
100 return 0;
93 } 101 }
94 102
95 103 void systhread_detach(SYS_THREAD thr)
96 NSAPI_PUBLIC void systhread_timerset(int usec)
97 { 104 {
98 /* This is an interesting problem. If you ever do turn on interrupts 105 pthread_detach(thr);
99 * on the server, you're in for lots of fun with NSPR Threads
100 PR_StartEvents(usec); */
101 } 106 }
102 107
103 108 void systhread_terminate(SYS_THREAD thr)
104 NSAPI_PUBLIC
105 SYS_THREAD systhread_attach(void)
106 { 109 {
107 PRThread *ret; 110 //PR_Interrupt((PRThread *)thr);
108 ret = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL);
109
110 return (void *) ret;
111 } 111 }
112 112
113 NSAPI_PUBLIC 113 void systhread_sleep(int msec)
114 void systhread_detach(SYS_THREAD thr)
115 { 114 {
116 /* XXXMB - this is not correct! */ 115 if(msec > 0) {
117 PR_DetachThread(); 116 poll(NULL, NULL, msec);
118 } 117 } else {
119 118 sched_yield();
120 NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr) 119 }
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 } 120 }
135 121
136 NSAPI_PUBLIC void systhread_init(char *name) 122 NSAPI_PUBLIC void systhread_init(char *name)
137 { 123 {
138 if (!PR_Initialized()) { 124 if (!PR_Initialized()) {
156 int maxPRFdCache = 8192; 142 int maxPRFdCache = 8192;
157 PR_SetFDCacheSize(0, maxPRFdCache); 143 PR_SetFDCacheSize(0, maxPRFdCache);
158 // </WORKAROUND> 144 // </WORKAROUND>
159 } 145 }
160 146
147 /*
148 * TODO: reimplement with pthread api
149 */
161 150
162 NSAPI_PUBLIC int systhread_newkey() 151 NSAPI_PUBLIC int systhread_newkey()
163 { 152 {
164 uintn newkey; 153 uintn newkey;
165 154

mercurial