]> git.pond.sub.org Git - empserver/blobdiff - src/lib/empthread/ntthread.c
Generation numbers to catch write back of stale copies
[empserver] / src / lib / empthread / ntthread.c
index 17d4b53f92aa08ccd3eeb68a2552516e06b4c4c9..67ae3ef49966f330941dfc81495efb73d2f9b543 100644 (file)
 #include <stdarg.h>
 #include <sys/types.h>
 #include <time.h>
-#include <winsock2.h>
-#undef NS_ALL
 #include <windows.h>
 #include <process.h>
-/* Note: unistd.h(posixio.c) is not thread-safe.
- * It may be used *only* while holding hThreadMutex.
- */
-#include "unistd.h"
 #include "misc.h"
 #include "empthread.h"
+#include "file.h"
 #include "prototypes.h"
 #include "server.h"
+#include "sys/socket.h"
 
 #define loc_MIN_THREAD_STACK  16384
 
@@ -428,6 +424,7 @@ empth_create(void (*entry)(void *), int size, int flags,
     empth_t *pThread = NULL;
 
     loc_debug("creating new thread %s", name);
+    ef_make_stale();
 
     pThread = malloc(sizeof(*pThread));
     if (!pThread) {
@@ -506,6 +503,7 @@ empth_exit(void)
     empth_t *pThread = TlsGetValue(dwTLSIndex);
 
     loc_debug("empth_exit");
+    ef_make_stale();
     loc_BlockThisThread();
 
     TlsSetValue(dwTLSIndex, NULL);
@@ -521,6 +519,7 @@ empth_exit(void)
 void
 empth_yield(void)
 {
+    ef_make_stale();
     loc_BlockThisThread();
     Sleep(0);
     loc_RunThisThread(NULL);
@@ -537,7 +536,7 @@ empth_yield(void)
 int
 empth_select(int fd, int flags, struct timeval *timeout)
 {
-    int handle;
+    SOCKET sock;
     WSAEVENT hEventObject[2];
     long events;
     DWORD result, msec;
@@ -546,20 +545,21 @@ empth_select(int fd, int flags, struct timeval *timeout)
 
     loc_debug("%s select on %d",
              flags == EMPTH_FD_READ ? "read" : "write", fd);
+    ef_make_stale();
     loc_BlockThisThread();
 
     hEventObject[0] = WSACreateEvent();
     hEventObject[1] = pThread->hThreadEvent;
 
-    handle = posix_fd2socket(fd);
-    CANT_HAPPEN(handle < 0);
+    sock = w32_fd2socket(fd);
+    CANT_HAPPEN(sock == (SOCKET)-1);
 
     events = 0;
     if (flags & EMPTH_FD_READ)
        events |= FD_READ | FD_ACCEPT | FD_CLOSE;
     if (flags & EMPTH_FD_WRITE)
        events |= FD_WRITE | FD_CLOSE;
-    WSAEventSelect(handle, hEventObject[0], events);
+    WSAEventSelect(sock, hEventObject[0], events);
 
     if (timeout)
        msec = timeout->tv_sec * 1000L + timeout->tv_usec / 1000L;
@@ -576,7 +576,7 @@ empth_select(int fd, int flags, struct timeval *timeout)
        res = 0;
        break;
     case WSA_WAIT_FAILED:
-       errno = WSAGetLastError();
+       w32_set_winsock_errno();
        res = -1;
        break;
     default:
@@ -585,7 +585,7 @@ empth_select(int fd, int flags, struct timeval *timeout)
        res = -1;
     }
 
-    WSAEventSelect(handle, hEventObject[0], 0);
+    WSAEventSelect(sock, hEventObject[0], 0);
 
     WSACloseEvent(hEventObject[0]);
 
@@ -620,6 +620,7 @@ empth_sleep(time_t until)
     empth_t *pThread = TlsGetValue(dwTLSIndex);
     DWORD result;
 
+    ef_make_stale();
     loc_BlockThisThread();
 
     do {
@@ -650,6 +651,7 @@ empth_request_shutdown(void)
 int
 empth_wait_for_signal(void)
 {
+    ef_make_stale();
     loc_BlockThisThread();
     loc_RunThisThread(hShutdownEvent);
     return SIGTERM;
@@ -701,6 +703,7 @@ empth_rwlock_destroy(empth_rwlock_t *rwlock)
 void
 empth_rwlock_wrlock(empth_rwlock_t *rwlock)
 {
+    ef_make_stale();
     /* block any new readers */
     ResetEvent(rwlock->can_read);
     rwlock->nwrite++;
@@ -712,6 +715,7 @@ empth_rwlock_wrlock(empth_rwlock_t *rwlock)
 void
 empth_rwlock_rdlock(empth_rwlock_t *rwlock)
 {
+    ef_make_stale();
     loc_BlockThisThread();
     loc_RunThisThread(rwlock->can_read);
     ResetEvent(rwlock->can_write);