mirror of
git://git.gnupg.org/gnupg.git
synced 2025-05-24 16:43:28 +02:00
2004-12-13 Timo Schulz <twoaday@g10code.com>
* w32-pth.c (enter_pth, leave_pth): New. (pth_init): Initialize global mutex section. (pth_kill): Release global mutex section. (helper_thread): New. (pth_spawn): Make sure only one thread is running.
This commit is contained in:
parent
801ab88522
commit
db27cdff41
@ -1,3 +1,11 @@
|
|||||||
|
2004-12-13 Timo Schulz <twoaday@g10code.com>
|
||||||
|
|
||||||
|
* w32-pth.c (enter_pth, leave_pth): New.
|
||||||
|
(pth_init): Initialize global mutex section.
|
||||||
|
(pth_kill): Release global mutex section.
|
||||||
|
(helper_thread): New.
|
||||||
|
(pth_spawn): Make sure only one thread is running.
|
||||||
|
|
||||||
2004-12-13 Werner Koch <wk@g10code.com>
|
2004-12-13 Werner Koch <wk@g10code.com>
|
||||||
|
|
||||||
* stringhelp.c (w32_strerror) [W32]: New.
|
* stringhelp.c (w32_strerror) [W32]: New.
|
||||||
|
221
jnlib/w32-pth.c
221
jnlib/w32-pth.c
@ -27,8 +27,9 @@
|
|||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#ifdef HAVE_W32_SYSTEM
|
#ifdef HAVE_W32_SYSTEM
|
||||||
#include <stdio.h>
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
@ -38,14 +39,19 @@
|
|||||||
#include "w32-pth.h"
|
#include "w32-pth.h"
|
||||||
|
|
||||||
|
|
||||||
static int pth_signo = 0;
|
|
||||||
static HANDLE pth_signo_ev = NULL;
|
|
||||||
static int pth_initialized = 0;
|
static int pth_initialized = 0;
|
||||||
|
|
||||||
|
/* Variables to support event handling. */
|
||||||
|
static int pth_signo = 0;
|
||||||
|
static HANDLE pth_signo_ev = NULL;
|
||||||
|
|
||||||
|
/* Mutex to make sure only one thread is running. */
|
||||||
|
static CRITICAL_SECTION pth_shd;
|
||||||
|
|
||||||
|
|
||||||
#define implicit_init() do { if (!pth_initialized) pth_init(); } while (0)
|
#define implicit_init() do { if (!pth_initialized) pth_init(); } while (0)
|
||||||
|
|
||||||
|
static void * helper_thread (void * ctx);
|
||||||
|
|
||||||
|
|
||||||
struct pth_event_s
|
struct pth_event_s
|
||||||
@ -53,7 +59,8 @@ struct pth_event_s
|
|||||||
struct pth_event_s * next;
|
struct pth_event_s * next;
|
||||||
struct pth_event_s * prev;
|
struct pth_event_s * prev;
|
||||||
HANDLE hd;
|
HANDLE hd;
|
||||||
union {
|
union
|
||||||
|
{
|
||||||
struct sigset_s * sig;
|
struct sigset_s * sig;
|
||||||
int fd;
|
int fd;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
@ -73,6 +80,13 @@ struct pth_attr_s
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct _pth_priv_hd_s
|
||||||
|
{
|
||||||
|
void *(*thread)(void *);
|
||||||
|
void * arg;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
pth_init (void)
|
pth_init (void)
|
||||||
@ -83,8 +97,9 @@ pth_init (void)
|
|||||||
printf ("pth_init: called.\n");
|
printf ("pth_init: called.\n");
|
||||||
pth_initialized = 1;
|
pth_initialized = 1;
|
||||||
if (WSAStartup (0x202, &wsadat))
|
if (WSAStartup (0x202, &wsadat))
|
||||||
return -1;
|
abort ();
|
||||||
pth_signo = 0;
|
pth_signo = 0;
|
||||||
|
InitializeCriticalSection (&pth_shd);
|
||||||
if (pth_signo_ev)
|
if (pth_signo_ev)
|
||||||
CloseHandle (pth_signo_ev);
|
CloseHandle (pth_signo_ev);
|
||||||
memset (&sa, 0, sizeof sa);
|
memset (&sa, 0, sizeof sa);
|
||||||
@ -92,7 +107,9 @@ pth_init (void)
|
|||||||
sa.lpSecurityDescriptor = NULL;
|
sa.lpSecurityDescriptor = NULL;
|
||||||
sa.nLength = sizeof sa;
|
sa.nLength = sizeof sa;
|
||||||
pth_signo_ev = CreateEvent (&sa, TRUE, FALSE, NULL);
|
pth_signo_ev = CreateEvent (&sa, TRUE, FALSE, NULL);
|
||||||
|
if (!pth_signo_ev)
|
||||||
|
abort ();
|
||||||
|
EnterCriticalSection (&pth_shd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,18 +120,36 @@ pth_kill (void)
|
|||||||
pth_signo = 0;
|
pth_signo = 0;
|
||||||
if (pth_signo_ev)
|
if (pth_signo_ev)
|
||||||
CloseHandle (pth_signo_ev);
|
CloseHandle (pth_signo_ev);
|
||||||
|
DeleteCriticalSection (&pth_shd);
|
||||||
WSACleanup ();
|
WSACleanup ();
|
||||||
pth_initialized = 0;
|
pth_initialized = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
enter_pth (void)
|
||||||
|
{
|
||||||
|
EnterCriticalSection (&pth_shd);
|
||||||
|
/*LeaveCriticalSection (&pth_shd);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
leave_pth (void)
|
||||||
|
{
|
||||||
|
LeaveCriticalSection (&pth_shd);
|
||||||
|
/*EnterCriticalSection (&pth_shd);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
long
|
long
|
||||||
pth_ctrl (unsigned long query, ...)
|
pth_ctrl (unsigned long query, ...)
|
||||||
{
|
{
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
|
|
||||||
switch (query) {
|
switch (query)
|
||||||
|
{
|
||||||
case PTH_CTRL_GETAVLOAD:
|
case PTH_CTRL_GETAVLOAD:
|
||||||
case PTH_CTRL_GETPRIO:
|
case PTH_CTRL_GETPRIO:
|
||||||
case PTH_CTRL_GETNAME:
|
case PTH_CTRL_GETNAME:
|
||||||
@ -124,7 +159,7 @@ pth_ctrl (unsigned long query, ...)
|
|||||||
case PTH_CTRL_GETTHREADS_WAITING:
|
case PTH_CTRL_GETTHREADS_WAITING:
|
||||||
case PTH_CTRL_GETTHREADS_SUSPENDED:
|
case PTH_CTRL_GETTHREADS_SUSPENDED:
|
||||||
case PTH_CTRL_GETTHREADS_DEAD:
|
case PTH_CTRL_GETTHREADS_DEAD:
|
||||||
/*case PTH_CTRL_GETTHREADS:*/
|
case PTH_CTRL_GETTHREADS:
|
||||||
default:
|
default:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -159,7 +194,8 @@ pth_read (int fd, void * buffer, size_t size)
|
|||||||
|
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
n = recv (fd, buffer, size, 0);
|
n = recv (fd, buffer, size, 0);
|
||||||
if (n == -1 && WSAGetLastError () == WSAENOTSOCK) {
|
if (n == -1 && WSAGetLastError () == WSAENOTSOCK)
|
||||||
|
{
|
||||||
DWORD nread = 0;
|
DWORD nread = 0;
|
||||||
n = ReadFile ((HANDLE)fd, buffer, size, &nread, NULL);
|
n = ReadFile ((HANDLE)fd, buffer, size, &nread, NULL);
|
||||||
if (!n)
|
if (!n)
|
||||||
@ -185,7 +221,8 @@ pth_write (int fd, const void * buffer, size_t size)
|
|||||||
|
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
n = send (fd, buffer, size, 0);
|
n = send (fd, buffer, size, 0);
|
||||||
if (n == -1 && WSAGetLastError () == WSAENOTSOCK) {
|
if (n == -1 && WSAGetLastError () == WSAENOTSOCK)
|
||||||
|
{
|
||||||
DWORD nwrite;
|
DWORD nwrite;
|
||||||
n = WriteFile ((HANDLE)fd, buffer, size, &nwrite, NULL);
|
n = WriteFile ((HANDLE)fd, buffer, size, &nwrite, NULL);
|
||||||
if (!n)
|
if (!n)
|
||||||
@ -212,7 +249,8 @@ pth_fdmode (int fd, int mode)
|
|||||||
|
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
/* XXX: figure out original fd mode */
|
/* XXX: figure out original fd mode */
|
||||||
switch (mode) {
|
switch (mode)
|
||||||
|
{
|
||||||
case PTH_FDMODE_NONBLOCK:
|
case PTH_FDMODE_NONBLOCK:
|
||||||
val = 1;
|
val = 1;
|
||||||
if (ioctlsocket (fd, FIONBIO, &val) == SOCKET_ERROR)
|
if (ioctlsocket (fd, FIONBIO, &val) == SOCKET_ERROR)
|
||||||
@ -238,7 +276,8 @@ pth_accept (int fd, struct sockaddr *addr, int *addrlen)
|
|||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
pth_accept_ev (int fd, struct sockaddr *addr, int *addrlen, pth_event_t ev_extra)
|
pth_accept_ev (int fd, struct sockaddr *addr, int *addrlen,
|
||||||
|
pth_event_t ev_extra)
|
||||||
{
|
{
|
||||||
pth_key_t ev_key;
|
pth_key_t ev_key;
|
||||||
pth_event_t ev;
|
pth_event_t ev;
|
||||||
@ -254,9 +293,11 @@ pth_accept_ev (int fd, struct sockaddr *addr, int *addrlen, pth_event_t ev_extra
|
|||||||
ev = NULL;
|
ev = NULL;
|
||||||
while ((rv = accept (fd, addr, addrlen)) == -1 &&
|
while ((rv = accept (fd, addr, addrlen)) == -1 &&
|
||||||
(WSAGetLastError () == WSAEINPROGRESS ||
|
(WSAGetLastError () == WSAEINPROGRESS ||
|
||||||
WSAGetLastError () == WSAEWOULDBLOCK)) {
|
WSAGetLastError () == WSAEWOULDBLOCK))
|
||||||
|
{
|
||||||
if (ev == NULL) {
|
if (ev == NULL) {
|
||||||
ev = pth_event (PTH_EVENT_FD|PTH_UNTIL_FD_READABLE|PTH_MODE_STATIC, &ev_key, fd);
|
ev = pth_event (PTH_EVENT_FD|PTH_UNTIL_FD_READABLE|PTH_MODE_STATIC,
|
||||||
|
&ev_key, fd);
|
||||||
if (ev == NULL)
|
if (ev == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
if (ev_extra != NULL)
|
if (ev_extra != NULL)
|
||||||
@ -264,9 +305,11 @@ pth_accept_ev (int fd, struct sockaddr *addr, int *addrlen, pth_event_t ev_extra
|
|||||||
}
|
}
|
||||||
/* wait until accept has a chance */
|
/* wait until accept has a chance */
|
||||||
pth_wait (ev);
|
pth_wait (ev);
|
||||||
if (ev_extra != NULL) {
|
if (ev_extra != NULL)
|
||||||
|
{
|
||||||
pth_event_isolate (ev);
|
pth_event_isolate (ev);
|
||||||
if (pth_event_status (ev) != PTH_STATUS_OCCURRED) {
|
if (pth_event_status (ev) != PTH_STATUS_OCCURRED)
|
||||||
|
{
|
||||||
pth_fdmode (fd, fdmode);
|
pth_fdmode (fd, fdmode);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -292,7 +335,8 @@ pth_mutex_release (pth_mutex_t *hd)
|
|||||||
if (!hd)
|
if (!hd)
|
||||||
return -1;
|
return -1;
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
if (hd->mx) {
|
if (hd->mx)
|
||||||
|
{
|
||||||
CloseHandle (hd->mx);
|
CloseHandle (hd->mx);
|
||||||
hd->mx = NULL;
|
hd->mx = NULL;
|
||||||
}
|
}
|
||||||
@ -313,7 +357,8 @@ pth_mutex_acquire (pth_mutex_t *hd, int tryonly, pth_event_t ev_extra)
|
|||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
/* still not locked, so simply acquire mutex? */
|
/* still not locked, so simply acquire mutex? */
|
||||||
if (!(mutex->mx_state & PTH_MUTEX_LOCKED)) {
|
if (!(mutex->mx_state & PTH_MUTEX_LOCKED))
|
||||||
|
{
|
||||||
mutex->mx_state |= PTH_MUTEX_LOCKED;
|
mutex->mx_state |= PTH_MUTEX_LOCKED;
|
||||||
mutex->mx_count = 1;
|
mutex->mx_count = 1;
|
||||||
pth_ring_append(&(pth_current->mutexring), &(mutex->mx_node));
|
pth_ring_append(&(pth_current->mutexring), &(mutex->mx_node));
|
||||||
@ -322,7 +367,8 @@ pth_mutex_acquire (pth_mutex_t *hd, int tryonly, pth_event_t ev_extra)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* already locked by caller? */
|
/* already locked by caller? */
|
||||||
if (mutex->mx_count >= 1 && mutex->mx_owner == pth_current) {
|
if (mutex->mx_count >= 1 && mutex->mx_owner == pth_current)
|
||||||
|
{
|
||||||
/* recursive lock */
|
/* recursive lock */
|
||||||
mutex->mx_count++;
|
mutex->mx_count++;
|
||||||
pth_debug1("pth_mutex_acquire: recursive locking");
|
pth_debug1("pth_mutex_acquire: recursive locking");
|
||||||
@ -332,7 +378,8 @@ pth_mutex_acquire (pth_mutex_t *hd, int tryonly, pth_event_t ev_extra)
|
|||||||
if (tryonly)
|
if (tryonly)
|
||||||
return return -1;
|
return return -1;
|
||||||
|
|
||||||
for (;;) {
|
for (;;)
|
||||||
|
{
|
||||||
ev = pth_event(PTH_EVENT_MUTEX|PTH_MODE_STATIC, &ev_key, mutex);
|
ev = pth_event(PTH_EVENT_MUTEX|PTH_MODE_STATIC, &ev_key, mutex);
|
||||||
if (ev_extra != NULL)
|
if (ev_extra != NULL)
|
||||||
pth_event_concat (ev, ev_extra, NULL);
|
pth_event_concat (ev, ev_extra, NULL);
|
||||||
@ -356,8 +403,10 @@ int
|
|||||||
pth_mutex_init (pth_mutex_t *hd)
|
pth_mutex_init (pth_mutex_t *hd)
|
||||||
{
|
{
|
||||||
SECURITY_ATTRIBUTES sa;
|
SECURITY_ATTRIBUTES sa;
|
||||||
|
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
if (hd->mx) {
|
if (hd->mx)
|
||||||
|
{
|
||||||
ReleaseMutex (hd->mx);
|
ReleaseMutex (hd->mx);
|
||||||
CloseHandle (hd->mx);
|
CloseHandle (hd->mx);
|
||||||
}
|
}
|
||||||
@ -407,10 +456,12 @@ pth_attr_set (pth_attr_t hd, int field, ...)
|
|||||||
|
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
va_start (args, field);
|
va_start (args, field);
|
||||||
switch (field) {
|
switch (field)
|
||||||
|
{
|
||||||
case PTH_ATTR_JOINABLE:
|
case PTH_ATTR_JOINABLE:
|
||||||
val = va_arg (args, int);
|
val = va_arg (args, int);
|
||||||
if (val) {
|
if (val)
|
||||||
|
{
|
||||||
hd->flags |= PTH_ATTR_JOINABLE;
|
hd->flags |= PTH_ATTR_JOINABLE;
|
||||||
printf ("pth_attr_set: PTH_ATTR_JOINABLE\n");
|
printf ("pth_attr_set: PTH_ATTR_JOINABLE\n");
|
||||||
}
|
}
|
||||||
@ -418,7 +469,8 @@ pth_attr_set (pth_attr_t hd, int field, ...)
|
|||||||
|
|
||||||
case PTH_ATTR_STACK_SIZE:
|
case PTH_ATTR_STACK_SIZE:
|
||||||
val = va_arg (args, int);
|
val = va_arg (args, int);
|
||||||
if (val) {
|
if (val)
|
||||||
|
{
|
||||||
hd->flags |= PTH_ATTR_STACK_SIZE;
|
hd->flags |= PTH_ATTR_STACK_SIZE;
|
||||||
hd->stack_size = val;
|
hd->stack_size = val;
|
||||||
printf ("pth_attr_set: PTH_ATTR_STACK_SIZE %d\n", val);
|
printf ("pth_attr_set: PTH_ATTR_STACK_SIZE %d\n", val);
|
||||||
@ -429,7 +481,8 @@ pth_attr_set (pth_attr_t hd, int field, ...)
|
|||||||
str = va_arg (args, char*);
|
str = va_arg (args, char*);
|
||||||
if (hd->name)
|
if (hd->name)
|
||||||
free (hd->name);
|
free (hd->name);
|
||||||
if (str) {
|
if (str)
|
||||||
|
{
|
||||||
hd->name = strdup (str);
|
hd->name = strdup (str);
|
||||||
if (!hd->name)
|
if (!hd->name)
|
||||||
return -1;
|
return -1;
|
||||||
@ -453,6 +506,7 @@ pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg)
|
|||||||
SECURITY_ATTRIBUTES sa;
|
SECURITY_ATTRIBUTES sa;
|
||||||
DWORD tid;
|
DWORD tid;
|
||||||
HANDLE th;
|
HANDLE th;
|
||||||
|
struct _pth_priv_hd_s * ctx;
|
||||||
|
|
||||||
if (!hd)
|
if (!hd)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -464,7 +518,17 @@ pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg)
|
|||||||
sa.lpSecurityDescriptor = NULL;
|
sa.lpSecurityDescriptor = NULL;
|
||||||
sa.nLength = sizeof sa;
|
sa.nLength = sizeof sa;
|
||||||
|
|
||||||
th = CreateThread (&sa, hd->stack_size, (LPTHREAD_START_ROUTINE)func, arg, 0, &tid);
|
ctx = calloc (1, sizeof * ctx);
|
||||||
|
ctx->thread = func;
|
||||||
|
ctx->arg = arg;
|
||||||
|
|
||||||
|
/* XXX: we don't use all thread attributes. */
|
||||||
|
enter_pth ();
|
||||||
|
th = CreateThread (&sa, hd->stack_size,
|
||||||
|
(LPTHREAD_START_ROUTINE)helper_thread,
|
||||||
|
ctx, 0, &tid);
|
||||||
|
leave_pth ();
|
||||||
|
|
||||||
return th;
|
return th;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -521,17 +585,20 @@ pth_waitpid (unsigned pid, int * status, int options)
|
|||||||
|
|
||||||
pth_debug2("pth_waitpid: called from thread \"%s\"", pth_current->name);
|
pth_debug2("pth_waitpid: called from thread \"%s\"", pth_current->name);
|
||||||
|
|
||||||
for (;;) {
|
for (;;)
|
||||||
|
{
|
||||||
/* do a non-blocking poll for the pid */
|
/* do a non-blocking poll for the pid */
|
||||||
while ( (pid = pth_sc(waitpid)(wpid, status, options|WNOHANG)) < 0
|
while ( (pid = pth_sc(waitpid)(wpid, status, options|WNOHANG)) < 0
|
||||||
&& errno == EINTR) ;
|
&& errno == EINTR)
|
||||||
|
;
|
||||||
|
|
||||||
/* if pid was found or caller requested a polling return immediately */
|
/* if pid was found or caller requested a polling return immediately */
|
||||||
if (pid == -1 || pid > 0 || (pid == 0 && (options & WNOHANG)))
|
if (pid == -1 || pid > 0 || (pid == 0 && (options & WNOHANG)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* else wait a little bit */
|
/* else wait a little bit */
|
||||||
ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key, pth_timeout(0,250000));
|
ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key,
|
||||||
|
pth_timeout (0,250000));
|
||||||
pth_wait(ev);
|
pth_wait(ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -544,7 +611,8 @@ pth_waitpid (unsigned pid, int * status, int options)
|
|||||||
static BOOL WINAPI
|
static BOOL WINAPI
|
||||||
sig_handler (DWORD signo)
|
sig_handler (DWORD signo)
|
||||||
{
|
{
|
||||||
switch (signo) {
|
switch (signo)
|
||||||
|
{
|
||||||
case CTRL_C_EVENT: pth_signo = SIGINT; break;
|
case CTRL_C_EVENT: pth_signo = SIGINT; break;
|
||||||
case CTRL_BREAK_EVENT: pth_signo = SIGTERM; break;
|
case CTRL_BREAK_EVENT: pth_signo = SIGTERM; break;
|
||||||
}
|
}
|
||||||
@ -570,14 +638,16 @@ pth_event (unsigned long spec, ...)
|
|||||||
return NULL;
|
return NULL;
|
||||||
if (spec == 0)
|
if (spec == 0)
|
||||||
;
|
;
|
||||||
else if (spec & PTH_EVENT_SIGS) {
|
else if (spec & PTH_EVENT_SIGS)
|
||||||
|
{
|
||||||
ev->u.sig = va_arg (arg, struct sigset_s *);
|
ev->u.sig = va_arg (arg, struct sigset_s *);
|
||||||
ev->u_type = PTH_EVENT_SIGS;
|
ev->u_type = PTH_EVENT_SIGS;
|
||||||
ev->val = va_arg (arg, int *);
|
ev->val = va_arg (arg, int *);
|
||||||
rc = SetConsoleCtrlHandler (sig_handler, TRUE);
|
rc = SetConsoleCtrlHandler (sig_handler, TRUE);
|
||||||
printf ("pth_event: sigs rc=%d\n", rc);
|
printf ("pth_event: sigs rc=%d\n", rc);
|
||||||
}
|
}
|
||||||
else if (spec & PTH_EVENT_FD) {
|
else if (spec & PTH_EVENT_FD)
|
||||||
|
{
|
||||||
if (spec & PTH_UNTIL_FD_READABLE)
|
if (spec & PTH_UNTIL_FD_READABLE)
|
||||||
ev->flags |= PTH_UNTIL_FD_READABLE;
|
ev->flags |= PTH_UNTIL_FD_READABLE;
|
||||||
if (spec & PTH_MODE_STATIC)
|
if (spec & PTH_MODE_STATIC)
|
||||||
@ -587,7 +657,8 @@ pth_event (unsigned long spec, ...)
|
|||||||
ev->u.fd = va_arg (arg, int);
|
ev->u.fd = va_arg (arg, int);
|
||||||
printf ("pth_event: fd=%d\n", ev->u.fd);
|
printf ("pth_event: fd=%d\n", ev->u.fd);
|
||||||
}
|
}
|
||||||
else if (spec & PTH_EVENT_TIME) {
|
else if (spec & PTH_EVENT_TIME)
|
||||||
|
{
|
||||||
pth_time_t t;
|
pth_time_t t;
|
||||||
if (spec & PTH_MODE_STATIC)
|
if (spec & PTH_MODE_STATIC)
|
||||||
ev->flags |= PTH_MODE_STATIC;
|
ev->flags |= PTH_MODE_STATIC;
|
||||||
@ -597,7 +668,8 @@ pth_event (unsigned long spec, ...)
|
|||||||
ev->u.tv.tv_sec = t.tv_sec;
|
ev->u.tv.tv_sec = t.tv_sec;
|
||||||
ev->u.tv.tv_usec = t.tv_usec;
|
ev->u.tv.tv_usec = t.tv_usec;
|
||||||
}
|
}
|
||||||
else if (spec & PTH_EVENT_MUTEX) {
|
else if (spec & PTH_EVENT_MUTEX)
|
||||||
|
{
|
||||||
va_arg (arg, pth_key_t);
|
va_arg (arg, pth_key_t);
|
||||||
ev->u_type = PTH_EVENT_MUTEX;
|
ev->u_type = PTH_EVENT_MUTEX;
|
||||||
ev->u.mx = va_arg (arg, pth_mutex_t*);
|
ev->u.mx = va_arg (arg, pth_mutex_t*);
|
||||||
@ -609,7 +681,8 @@ pth_event (unsigned long spec, ...)
|
|||||||
sa.lpSecurityDescriptor = NULL;
|
sa.lpSecurityDescriptor = NULL;
|
||||||
sa.nLength = sizeof sa;
|
sa.nLength = sizeof sa;
|
||||||
ev->hd = CreateEvent (&sa, FALSE, FALSE, NULL);
|
ev->hd = CreateEvent (&sa, FALSE, FALSE, NULL);
|
||||||
if (!ev->hd) {
|
if (!ev->hd)
|
||||||
|
{
|
||||||
free (ev);
|
free (ev);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -663,14 +736,16 @@ wait_for_fd (int fd, int is_read, int nwait)
|
|||||||
tv.tv_sec = nwait;
|
tv.tv_sec = nwait;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
|
|
||||||
while (1) {
|
while (1)
|
||||||
|
{
|
||||||
n = select (fd+1, &r, &w, NULL, &tv);
|
n = select (fd+1, &r, &w, NULL, &tv);
|
||||||
printf ("wait_for_fd=%d fd %d (ec=%d)\n", n, fd, (int)WSAGetLastError ());
|
printf ("wait_for_fd=%d fd %d (ec=%d)\n", n, fd,(int)WSAGetLastError ());
|
||||||
if (n == -1)
|
if (n == -1)
|
||||||
break;
|
break;
|
||||||
if (!n)
|
if (!n)
|
||||||
continue;
|
continue;
|
||||||
if (n == 1) {
|
if (n == 1)
|
||||||
|
{
|
||||||
if (is_read && FD_ISSET (fd, &r))
|
if (is_read && FD_ISSET (fd, &r))
|
||||||
break;
|
break;
|
||||||
else if (FD_ISSET (fd, &w))
|
else if (FD_ISSET (fd, &w))
|
||||||
@ -681,6 +756,20 @@ wait_for_fd (int fd, int is_read, int nwait)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void *
|
||||||
|
helper_thread (void * ctx)
|
||||||
|
{
|
||||||
|
struct _pth_priv_hd_s * c = ctx;
|
||||||
|
|
||||||
|
leave_pth ();
|
||||||
|
c->thread (c->arg);
|
||||||
|
enter_pth ();
|
||||||
|
free (c);
|
||||||
|
ExitThread (0);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void *
|
static void *
|
||||||
wait_fd_thread (void * ctx)
|
wait_fd_thread (void * ctx)
|
||||||
{
|
{
|
||||||
@ -749,7 +838,8 @@ pth_event_occured (pth_event_t ev)
|
|||||||
if (!ev)
|
if (!ev)
|
||||||
return 0;
|
return 0;
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
switch (ev->u_type) {
|
switch (ev->u_type)
|
||||||
|
{
|
||||||
case 0:
|
case 0:
|
||||||
if (WaitForSingleObject (ev->hd, 0) == WAIT_OBJECT_0)
|
if (WaitForSingleObject (ev->hd, 0) == WAIT_OBJECT_0)
|
||||||
return 1;
|
return 1;
|
||||||
@ -757,7 +847,8 @@ pth_event_occured (pth_event_t ev)
|
|||||||
|
|
||||||
case PTH_EVENT_SIGS:
|
case PTH_EVENT_SIGS:
|
||||||
if (sigpresent (ev->u.sig, pth_signo) &&
|
if (sigpresent (ev->u.sig, pth_signo) &&
|
||||||
WaitForSingleObject (pth_signo_ev, 0) == WAIT_OBJECT_0) {
|
WaitForSingleObject (pth_signo_ev, 0) == WAIT_OBJECT_0)
|
||||||
|
{
|
||||||
printf ("pth_event_occured: sig signaled.\n");
|
printf ("pth_event_occured: sig signaled.\n");
|
||||||
(*ev->val) = pth_signo;
|
(*ev->val) = pth_signo;
|
||||||
return 1;
|
return 1;
|
||||||
@ -792,15 +883,18 @@ pth_event_free (pth_event_t ev, int mode)
|
|||||||
pth_event_t n;
|
pth_event_t n;
|
||||||
|
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
if (mode == PTH_FREE_ALL) {
|
if (mode == PTH_FREE_ALL)
|
||||||
while (ev) {
|
{
|
||||||
|
while (ev)
|
||||||
|
{
|
||||||
n = ev->next;
|
n = ev->next;
|
||||||
CloseHandle (ev->hd); ev->hd = NULL;
|
CloseHandle (ev->hd); ev->hd = NULL;
|
||||||
free (ev);
|
free (ev);
|
||||||
ev = n;
|
ev = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (mode == PTH_FREE_THIS) {
|
else if (mode == PTH_FREE_THIS)
|
||||||
|
{
|
||||||
ev->prev->next = ev->next;
|
ev->prev->next = ev->next;
|
||||||
ev->next->prev = ev->prev;
|
ev->next->prev = ev->prev;
|
||||||
CloseHandle (ev->hd); ev->hd = NULL;
|
CloseHandle (ev->hd); ev->hd = NULL;
|
||||||
@ -820,7 +914,6 @@ pth_event_isolate (pth_event_t ev)
|
|||||||
return NULL;
|
return NULL;
|
||||||
implicit_init ();
|
implicit_init ();
|
||||||
return ring;
|
return ring;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -867,13 +960,16 @@ pth_wait (pth_event_t ev)
|
|||||||
pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 4096);
|
pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 4096);
|
||||||
|
|
||||||
printf ("pth_wait: cnt %d\n", pth_event_count (ev));
|
printf ("pth_wait: cnt %d\n", pth_event_count (ev));
|
||||||
for (tmp = ev; tmp; tmp = tmp->next) {
|
for (tmp = ev; tmp; tmp = tmp->next)
|
||||||
if (pos+1 > MAXIMUM_WAIT_OBJECTS/2) {
|
{
|
||||||
|
if (pos+1 > MAXIMUM_WAIT_OBJECTS/2)
|
||||||
|
{
|
||||||
free_threads (waitbuf, hdidx, i);
|
free_threads (waitbuf, hdidx, i);
|
||||||
pth_attr_destroy (attr);
|
pth_attr_destroy (attr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
switch (tmp->u_type) {
|
switch (tmp->u_type)
|
||||||
|
{
|
||||||
case 0:
|
case 0:
|
||||||
waitbuf[pos++] = tmp->hd;
|
waitbuf[pos++] = tmp->hd;
|
||||||
break;
|
break;
|
||||||
@ -911,31 +1007,6 @@ pth_wait (pth_event_t ev)
|
|||||||
if (n != WAIT_TIMEOUT)
|
if (n != WAIT_TIMEOUT)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/*
|
|
||||||
switch (ev->u_type) {
|
|
||||||
case 0:
|
|
||||||
n = WaitForSingleObject (ev->hd, INFINITE);
|
|
||||||
if (n != WAIT_OBJECT_0)
|
|
||||||
return 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PTH_EVENT_SIGS:
|
|
||||||
n = WaitForSingleObject (pth_signo_ev, INFINITE);
|
|
||||||
if (n != WAIT_OBJECT_0)
|
|
||||||
return 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PTH_EVENT_FD:
|
|
||||||
if (wait_for_fd (ev->u.fd, ev->flags & PTH_UNTIL_FD_READABLE)) {
|
|
||||||
SetEvent (ev->hd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -950,7 +1021,8 @@ pth_sleep (int sec)
|
|||||||
if (sec == 0)
|
if (sec == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
ev = pth_event (PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key, pth_timeout (sec, 0));
|
ev = pth_event (PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key,
|
||||||
|
pth_timeout (sec, 0));
|
||||||
if (ev == NULL)
|
if (ev == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
pth_wait (ev);
|
pth_wait (ev);
|
||||||
@ -1055,7 +1127,8 @@ main_3 (int argc, char ** argv)
|
|||||||
ev = setup_signals (&sigs, &signo);
|
ev = setup_signals (&sigs, &signo);
|
||||||
n = sizeof addr;
|
n = sizeof addr;
|
||||||
infd = pth_accept_ev (fd, (struct sockaddr *)&rem, &n, ev);
|
infd = pth_accept_ev (fd, (struct sockaddr *)&rem, &n, ev);
|
||||||
printf ("infd %d: %s:%d\n", infd, inet_ntoa (rem.sin_addr), htons (rem.sin_port));
|
printf ("infd %d: %s:%d\n", infd, inet_ntoa (rem.sin_addr),
|
||||||
|
htons (rem.sin_port));
|
||||||
|
|
||||||
closesocket (infd);
|
closesocket (infd);
|
||||||
pth_event_free (ev, PTH_FREE_ALL);
|
pth_event_free (ev, PTH_FREE_ALL);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user