src/server/util/systhr.c

changeset 29
e8619defde14
parent 14
b8bf95b39952
child 40
56cda23f48d4
--- a/src/server/util/systhr.c	Sun May 06 10:09:27 2012 +0200
+++ b/src/server/util/systhr.c	Wed May 16 12:47:28 2012 +0200
@@ -39,7 +39,7 @@
 
 
 #include "systhr.h"
-#include "ereport.h"
+//include "ereport.h"
 
 #include "prinit.h"
 #include "prthread.h"
@@ -51,16 +51,6 @@
 #include <poll.h>
 #endif
 
-#ifdef THREAD_WIN32
-#include <process.h>
-
-typedef struct {
-    HANDLE hand;
-    DWORD id;
-} sys_thread_s;
-
-#endif
-
 #define DEFAULT_STACKSIZE (64*1024)
 
 static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE;
@@ -71,66 +61,62 @@
 	_systhr_stacksize = size;
 }
 
-NSAPI_PUBLIC 
-SYS_THREAD systhread_start(int prio, int stksz, thrstartfunc fn, void *arg)
-{
-    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;
-}
 
 
-NSAPI_PUBLIC SYS_THREAD systhread_current(void)
-{
-    return PR_GetCurrentThread();
+SYS_THREAD systhread_start(int prio, int stksz, thrstartfunc fn, void *arg) {
+    pthread_t      thr = 0;
+    pthread_attr_t attr;
+    
+    pthread_attr_init(&attr);
+    if(stksz) {
+        pthread_attr_setstacksize(&attr, stksz);
+    }
+    
+    if(pthread_create(&thr, &attr, (posix_thrstartfunc)fn, arg) != 0) {
+        /* error */
+    }
+    
+    return thr;
 }
 
-NSAPI_PUBLIC void systhread_yield(void)
-{
-    PR_Sleep(PR_INTERVAL_NO_WAIT);
+SYS_THREAD systhread_current(void) {
+    return pthread_self();
 }
 
-
-NSAPI_PUBLIC void systhread_timerset(int usec)
+void systhread_yield(void)
 {
-   /* 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); */
+    sched_yield();
 }
 
 
-NSAPI_PUBLIC 
+void systhread_timerset(int usec)
+{
+   
+}
+
 SYS_THREAD systhread_attach(void)
 {
-    PRThread *ret;
-    ret = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL);
-
-    return (void *) ret;
+    /* TODO: what to do? */
+    return 0;
 }
 
-NSAPI_PUBLIC
 void systhread_detach(SYS_THREAD thr)
 {
-    /* XXXMB - this is not correct! */
-    PR_DetachThread();
+    pthread_detach(thr);
 }
 
-NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
+void systhread_terminate(SYS_THREAD thr)
 {
-    PR_Interrupt((PRThread *)thr);
+    //PR_Interrupt((PRThread *)thr);
 }
 
-NSAPI_PUBLIC void systhread_sleep(int milliseconds)
+void systhread_sleep(int msec)
 {
-#ifdef XP_WIN32
-    PR_Sleep(PR_MillisecondsToInterval(milliseconds));
-#else
-    /* poll() is more efficient than PR_Sleep() */
-    if (milliseconds > 0)
-        poll(NULL, NULL, milliseconds);
-#endif
+    if(msec > 0) {
+        poll(NULL, NULL, msec);
+    } else {
+        sched_yield();
+    }
 }
 
 NSAPI_PUBLIC void systhread_init(char *name)
@@ -158,6 +144,9 @@
     // </WORKAROUND>
 }
 
+/*
+ *  TODO: reimplement with pthread api
+ */
 
 NSAPI_PUBLIC int systhread_newkey()
 {

mercurial