1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-01-21 14:47:03 +01: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:
Timo Schulz 2004-12-13 18:00:35 +00:00
parent 801ab88522
commit db27cdff41
2 changed files with 668 additions and 587 deletions

View File

@ -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>
* stringhelp.c (w32_strerror) [W32]: New.

View File

@ -27,8 +27,9 @@
#include <config.h>
#ifdef HAVE_W32_SYSTEM
#include <stdio.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <signal.h>
@ -38,14 +39,19 @@
#include "w32-pth.h"
static int pth_signo = 0;
static HANDLE pth_signo_ev = NULL;
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)
static void * helper_thread (void * ctx);
struct pth_event_s
@ -53,7 +59,8 @@ struct pth_event_s
struct pth_event_s * next;
struct pth_event_s * prev;
HANDLE hd;
union {
union
{
struct sigset_s * sig;
int fd;
struct timeval tv;
@ -73,6 +80,13 @@ struct pth_attr_s
};
struct _pth_priv_hd_s
{
void *(*thread)(void *);
void * arg;
};
int
pth_init (void)
@ -83,8 +97,9 @@ pth_init (void)
printf ("pth_init: called.\n");
pth_initialized = 1;
if (WSAStartup (0x202, &wsadat))
return -1;
abort ();
pth_signo = 0;
InitializeCriticalSection (&pth_shd);
if (pth_signo_ev)
CloseHandle (pth_signo_ev);
memset (&sa, 0, sizeof sa);
@ -92,7 +107,9 @@ pth_init (void)
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof sa;
pth_signo_ev = CreateEvent (&sa, TRUE, FALSE, NULL);
if (!pth_signo_ev)
abort ();
EnterCriticalSection (&pth_shd);
return 0;
}
@ -103,18 +120,36 @@ pth_kill (void)
pth_signo = 0;
if (pth_signo_ev)
CloseHandle (pth_signo_ev);
DeleteCriticalSection (&pth_shd);
WSACleanup ();
pth_initialized = 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
pth_ctrl (unsigned long query, ...)
{
implicit_init ();
switch (query) {
switch (query)
{
case PTH_CTRL_GETAVLOAD:
case PTH_CTRL_GETPRIO:
case PTH_CTRL_GETNAME:
@ -124,7 +159,7 @@ pth_ctrl (unsigned long query, ...)
case PTH_CTRL_GETTHREADS_WAITING:
case PTH_CTRL_GETTHREADS_SUSPENDED:
case PTH_CTRL_GETTHREADS_DEAD:
/*case PTH_CTRL_GETTHREADS:*/
case PTH_CTRL_GETTHREADS:
default:
return -1;
}
@ -159,7 +194,8 @@ pth_read (int fd, void * buffer, size_t size)
implicit_init ();
n = recv (fd, buffer, size, 0);
if (n == -1 && WSAGetLastError () == WSAENOTSOCK) {
if (n == -1 && WSAGetLastError () == WSAENOTSOCK)
{
DWORD nread = 0;
n = ReadFile ((HANDLE)fd, buffer, size, &nread, NULL);
if (!n)
@ -185,7 +221,8 @@ pth_write (int fd, const void * buffer, size_t size)
implicit_init ();
n = send (fd, buffer, size, 0);
if (n == -1 && WSAGetLastError () == WSAENOTSOCK) {
if (n == -1 && WSAGetLastError () == WSAENOTSOCK)
{
DWORD nwrite;
n = WriteFile ((HANDLE)fd, buffer, size, &nwrite, NULL);
if (!n)
@ -212,7 +249,8 @@ pth_fdmode (int fd, int mode)
implicit_init ();
/* XXX: figure out original fd mode */
switch (mode) {
switch (mode)
{
case PTH_FDMODE_NONBLOCK:
val = 1;
if (ioctlsocket (fd, FIONBIO, &val) == SOCKET_ERROR)
@ -238,7 +276,8 @@ pth_accept (int fd, struct sockaddr *addr, int *addrlen)
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_event_t ev;
@ -254,9 +293,11 @@ pth_accept_ev (int fd, struct sockaddr *addr, int *addrlen, pth_event_t ev_extra
ev = NULL;
while ((rv = accept (fd, addr, addrlen)) == -1 &&
(WSAGetLastError () == WSAEINPROGRESS ||
WSAGetLastError () == WSAEWOULDBLOCK)) {
WSAGetLastError () == WSAEWOULDBLOCK))
{
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)
return -1;
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 */
pth_wait (ev);
if (ev_extra != NULL) {
if (ev_extra != NULL)
{
pth_event_isolate (ev);
if (pth_event_status (ev) != PTH_STATUS_OCCURRED) {
if (pth_event_status (ev) != PTH_STATUS_OCCURRED)
{
pth_fdmode (fd, fdmode);
return -1;
}
@ -292,7 +335,8 @@ pth_mutex_release (pth_mutex_t *hd)
if (!hd)
return -1;
implicit_init ();
if (hd->mx) {
if (hd->mx)
{
CloseHandle (hd->mx);
hd->mx = NULL;
}
@ -313,7 +357,8 @@ pth_mutex_acquire (pth_mutex_t *hd, int tryonly, pth_event_t ev_extra)
#if 0
/* 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_count = 1;
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? */
if (mutex->mx_count >= 1 && mutex->mx_owner == pth_current) {
if (mutex->mx_count >= 1 && mutex->mx_owner == pth_current)
{
/* recursive lock */
mutex->mx_count++;
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)
return return -1;
for (;;) {
for (;;)
{
ev = pth_event(PTH_EVENT_MUTEX|PTH_MODE_STATIC, &ev_key, mutex);
if (ev_extra != NULL)
pth_event_concat (ev, ev_extra, NULL);
@ -356,8 +403,10 @@ int
pth_mutex_init (pth_mutex_t *hd)
{
SECURITY_ATTRIBUTES sa;
implicit_init ();
if (hd->mx) {
if (hd->mx)
{
ReleaseMutex (hd->mx);
CloseHandle (hd->mx);
}
@ -407,10 +456,12 @@ pth_attr_set (pth_attr_t hd, int field, ...)
implicit_init ();
va_start (args, field);
switch (field) {
switch (field)
{
case PTH_ATTR_JOINABLE:
val = va_arg (args, int);
if (val) {
if (val)
{
hd->flags |= PTH_ATTR_JOINABLE;
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:
val = va_arg (args, int);
if (val) {
if (val)
{
hd->flags |= PTH_ATTR_STACK_SIZE;
hd->stack_size = 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*);
if (hd->name)
free (hd->name);
if (str) {
if (str)
{
hd->name = strdup (str);
if (!hd->name)
return -1;
@ -453,6 +506,7 @@ pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg)
SECURITY_ATTRIBUTES sa;
DWORD tid;
HANDLE th;
struct _pth_priv_hd_s * ctx;
if (!hd)
return NULL;
@ -464,7 +518,17 @@ pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg)
sa.lpSecurityDescriptor = NULL;
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;
}
@ -521,17 +585,20 @@ pth_waitpid (unsigned pid, int * status, int options)
pth_debug2("pth_waitpid: called from thread \"%s\"", pth_current->name);
for (;;) {
for (;;)
{
/* do a non-blocking poll for the pid */
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 == -1 || pid > 0 || (pid == 0 && (options & WNOHANG)))
break;
/* 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);
}
@ -544,7 +611,8 @@ pth_waitpid (unsigned pid, int * status, int options)
static BOOL WINAPI
sig_handler (DWORD signo)
{
switch (signo) {
switch (signo)
{
case CTRL_C_EVENT: pth_signo = SIGINT; break;
case CTRL_BREAK_EVENT: pth_signo = SIGTERM; break;
}
@ -570,14 +638,16 @@ pth_event (unsigned long spec, ...)
return NULL;
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_type = PTH_EVENT_SIGS;
ev->val = va_arg (arg, int *);
rc = SetConsoleCtrlHandler (sig_handler, TRUE);
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)
ev->flags |= PTH_UNTIL_FD_READABLE;
if (spec & PTH_MODE_STATIC)
@ -587,7 +657,8 @@ pth_event (unsigned long spec, ...)
ev->u.fd = va_arg (arg, int);
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;
if (spec & 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_usec = t.tv_usec;
}
else if (spec & PTH_EVENT_MUTEX) {
else if (spec & PTH_EVENT_MUTEX)
{
va_arg (arg, pth_key_t);
ev->u_type = PTH_EVENT_MUTEX;
ev->u.mx = va_arg (arg, pth_mutex_t*);
@ -609,7 +681,8 @@ pth_event (unsigned long spec, ...)
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof sa;
ev->hd = CreateEvent (&sa, FALSE, FALSE, NULL);
if (!ev->hd) {
if (!ev->hd)
{
free (ev);
return NULL;
}
@ -663,14 +736,16 @@ wait_for_fd (int fd, int is_read, int nwait)
tv.tv_sec = nwait;
tv.tv_usec = 0;
while (1) {
while (1)
{
n = select (fd+1, &r, &w, NULL, &tv);
printf ("wait_for_fd=%d fd %d (ec=%d)\n", n, fd,(int)WSAGetLastError ());
if (n == -1)
break;
if (!n)
continue;
if (n == 1) {
if (n == 1)
{
if (is_read && FD_ISSET (fd, &r))
break;
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 *
wait_fd_thread (void * ctx)
{
@ -749,7 +838,8 @@ pth_event_occured (pth_event_t ev)
if (!ev)
return 0;
implicit_init ();
switch (ev->u_type) {
switch (ev->u_type)
{
case 0:
if (WaitForSingleObject (ev->hd, 0) == WAIT_OBJECT_0)
return 1;
@ -757,7 +847,8 @@ pth_event_occured (pth_event_t ev)
case PTH_EVENT_SIGS:
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");
(*ev->val) = pth_signo;
return 1;
@ -792,15 +883,18 @@ pth_event_free (pth_event_t ev, int mode)
pth_event_t n;
implicit_init ();
if (mode == PTH_FREE_ALL) {
while (ev) {
if (mode == PTH_FREE_ALL)
{
while (ev)
{
n = ev->next;
CloseHandle (ev->hd); ev->hd = NULL;
free (ev);
ev = n;
}
}
else if (mode == PTH_FREE_THIS) {
else if (mode == PTH_FREE_THIS)
{
ev->prev->next = ev->next;
ev->next->prev = ev->prev;
CloseHandle (ev->hd); ev->hd = NULL;
@ -820,7 +914,6 @@ pth_event_isolate (pth_event_t ev)
return NULL;
implicit_init ();
return ring;
}
@ -867,13 +960,16 @@ pth_wait (pth_event_t ev)
pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 4096);
printf ("pth_wait: cnt %d\n", pth_event_count (ev));
for (tmp = ev; tmp; tmp = tmp->next) {
if (pos+1 > MAXIMUM_WAIT_OBJECTS/2) {
for (tmp = ev; tmp; tmp = tmp->next)
{
if (pos+1 > MAXIMUM_WAIT_OBJECTS/2)
{
free_threads (waitbuf, hdidx, i);
pth_attr_destroy (attr);
return -1;
}
switch (tmp->u_type) {
switch (tmp->u_type)
{
case 0:
waitbuf[pos++] = tmp->hd;
break;
@ -911,31 +1007,6 @@ pth_wait (pth_event_t ev)
if (n != WAIT_TIMEOUT)
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;
}
@ -950,7 +1021,8 @@ pth_sleep (int sec)
if (sec == 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)
return -1;
pth_wait (ev);
@ -1055,7 +1127,8 @@ main_3 (int argc, char ** argv)
ev = setup_signals (&sigs, &signo);
n = sizeof addr;
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);
pth_event_free (ev, PTH_FREE_ALL);