Simple POSIX I/O emulation layer to work around Windows's defective

Unix I/O:
[_WIN32] (socklen_t, accept, posix_accept, bind. posix_bind, listen)
(posix_listen, setsockopt, posix_setsockopt, shutdown, posix_shutdown)
(socket, posix_socket, close, posix_close, creat, fstat, posix_fstat)
(lseek, posix_lseek, open, posix_open, read, posix_read, write)
(posix_write, fileno, posix_fileno, fcntl, O_NONBLOCK, F_RDLCK)
(F_WRLCK, F_GETFL, F_SETFL, F_SETLK, EWOULDBLOCK, ENOTSOCK)
(flock, fsync, posix_fsync):
New.
(ef_open, io_close, io_input, io_output, io_shutdown, io_noblocking)
(player_accept): Use them to simplify.
[_WIN32] (posix_fd2socket): New.
(empth_select): Use it.
(gen_power): Use it.
This commit is contained in:
Ron Koenderink 2007-08-14 03:33:28 +00:00
parent e7b04123a9
commit 4bbc3f4286
17 changed files with 669 additions and 109 deletions

View file

@ -55,6 +55,10 @@
#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 "prototypes.h"
@ -313,6 +317,9 @@ loc_Exit_Handler(DWORD fdwCtrlType)
*
* This is the main line of each thread.
* This is really a static local func....
* Note: As the POSIX compatibility layer is not thread safe
* this function can not open or create any files or sockets until
* loc_RunThisThread() is called
*/
static void
empth_threadMain(void *pvData)
@ -535,6 +542,7 @@ empth_terminate(empth_t *pThread)
void
empth_select(int fd, int flags)
{
int handle;
WSAEVENT hEventObject[2];
empth_t *pThread = TlsGetValue(dwTLSIndex);
@ -545,10 +553,13 @@ empth_select(int fd, int flags)
hEventObject[0] = WSACreateEvent();
hEventObject[1] = pThread->hThreadEvent;
handle = posix_fd2socket(fd);
CANT_HAPPEN(handle < 0);
if (flags == EMPTH_FD_READ)
WSAEventSelect(fd, hEventObject[0], FD_READ | FD_ACCEPT | FD_CLOSE);
WSAEventSelect(handle, hEventObject[0], FD_READ | FD_ACCEPT | FD_CLOSE);
else if (flags == EMPTH_FD_WRITE)
WSAEventSelect(fd, hEventObject[0], FD_WRITE | FD_CLOSE);
WSAEventSelect(handle, hEventObject[0], FD_WRITE | FD_CLOSE);
else {
logerror("bad flag %d passed to empth_select", flags);
empth_exit();
@ -556,7 +567,7 @@ empth_select(int fd, int flags)
WSAWaitForMultipleEvents(2, hEventObject, FALSE, WSA_INFINITE, FALSE);
WSAEventSelect(fd, hEventObject[0], 0);
WSAEventSelect(handle, hEventObject[0], 0);
WSACloseEvent(hEventObject[0]);