GNUPG-1-9-BRANCH gnupg/jnlib (ChangeLog w32-pth.c)
cvs user wk
cvs at cvs.gnupg.org
Mon Dec 20 12:31:54 CET 2004
Date: Monday, December 20, 2004 @ 12:36:46
Author: wk
Path: /cvs/gnupg/gnupg/jnlib
Tag: GNUPG-1-9-BRANCH
Modified: ChangeLog w32-pth.c
(do_pth_event_free): The events are hold in a ring
buffer. Adjust for that.
(do_pth_event_body): Ditto.
(pth_event_isolate): Ditto.
(do_pth_wait): Ditto.
(_pth_event_count): Renamed to ..
(event_count): .. and adjusted as above.
(pth_init): Define 3 debug levels and change all debug calls to
make use of them. This makes the moule now silent.
-----------+
ChangeLog | 12 ++
w32-pth.c | 258 ++++++++++++++++++++++++++++++++++++------------------------
2 files changed, 169 insertions(+), 101 deletions(-)
Index: gnupg/jnlib/ChangeLog
diff -u gnupg/jnlib/ChangeLog:1.3.2.30 gnupg/jnlib/ChangeLog:1.3.2.31
--- gnupg/jnlib/ChangeLog:1.3.2.30 Sun Dec 19 18:44:20 2004
+++ gnupg/jnlib/ChangeLog Mon Dec 20 12:36:46 2004
@@ -1,3 +1,15 @@
+2004-12-20 Werner Koch <wk at g10code.com>
+
+ * w32-pth.c (do_pth_event_free): The events are hold in a ring
+ buffer. Adjust for that.
+ (do_pth_event_body): Ditto.
+ (pth_event_isolate): Ditto.
+ (do_pth_wait): Ditto.
+ (_pth_event_count): Renamed to ..
+ (event_count): .. and adjusted as above.
+ (pth_init): Define 3 debug levels and change all debug calls to
+ make use of them. This makes the moule now silent.
+
2004-12-19 Werner Koch <wk at g10code.com>
* w32-pth.c (pth_init): Enable debugging depending on env var.
Index: gnupg/jnlib/w32-pth.c
diff -u gnupg/jnlib/w32-pth.c:1.1.2.10 gnupg/jnlib/w32-pth.c:1.1.2.11
--- gnupg/jnlib/w32-pth.c:1.1.2.10 Sun Dec 19 18:44:20 2004
+++ gnupg/jnlib/w32-pth.c Mon Dec 20 12:36:46 2004
@@ -40,8 +40,6 @@
#define W32_PTH_HANDLE_INTERNAL HANDLE
#include "w32-pth.h"
-#define DEBUG_ENTER_LEAVE 1 /* Set to 1 to enable full debugging. */
-
#ifndef FALSE
#define FALSE 0
@@ -54,11 +52,14 @@
#endif
-/* States whether trhis module has been initialized. */
+/* States whether this module has been initialized. */
static int pth_initialized;
-/* Controls whether debugging is enabled. */
-static int debug_enter_leave;
+/* Keeps the current debug level. Define marcos to test them. */
+static int debug_level;
+#define DBG_ERROR (debug_level >= 1)
+#define DBG_INFO (debug_level >= 2)
+#define DBG_CALLS (debug_level >= 3)
/* Variables to support event handling. */
static int pth_signo;
@@ -67,7 +68,7 @@
/* Mutex to make sure only one thread is running. */
static CRITICAL_SECTION pth_shd;
-
+/* Events are store in a double linked event ring. */
struct pth_event_s
{
struct pth_event_s * next;
@@ -124,12 +125,14 @@
{
SECURITY_ATTRIBUTES sa;
WSADATA wsadat;
+ const char *s;
if (pth_initialized)
return TRUE;
- fprintf (stderr, "%s: pth_init: called.\n", log_get_prefix (NULL));
- debug_enter_leave = !!getenv ("DEBUG_PTH");
+ debug_level = (s=getenv ("DEBUG_PTH"))? atoi (s):0;
+ if (debug_level)
+ fprintf (stderr, "%s: pth_init: called.\n", log_get_prefix (NULL));
if (WSAStartup (0x202, &wsadat))
return FALSE;
@@ -184,7 +187,7 @@
{
/* Fixme: I am not sure whether the same thread my enter a critical
section twice. */
- if (debug_enter_leave)
+ if (DBG_CALLS)
fprintf (stderr, "%s: enter_pth (%s)\n",
log_get_prefix (NULL), function? function:"");
LeaveCriticalSection (&pth_shd);
@@ -195,7 +198,7 @@
leave_pth (const char *function)
{
EnterCriticalSection (&pth_shd);
- if (debug_enter_leave)
+ if (DBG_CALLS)
fprintf (stderr, "%s: leave_pth (%s)\n",
log_get_prefix (NULL), function? function:"");
}
@@ -262,9 +265,10 @@
{
char strerr[256];
- fprintf (stderr, "%s: pth_read(%d) failed read from file: %s\n",
- log_get_prefix (NULL), fd,
- w32_strerror (strerr, sizeof strerr));
+ if (DBG_ERROR)
+ fprintf (stderr, "%s: pth_read(%d) failed read from file: %s\n",
+ log_get_prefix (NULL), fd,
+ w32_strerror (strerr, sizeof strerr));
n = -1;
}
else
@@ -302,9 +306,10 @@
n = WriteFile ((HANDLE)fd, buffer, size, &nwrite, NULL);
if (!n)
{
- fprintf (stderr, "%s: pth_write(%d) failed in write: %s\n",
- log_get_prefix (NULL), fd,
- w32_strerror (strerr, sizeof strerr));
+ if (DBG_ERROR)
+ fprintf (stderr, "%s: pth_write(%d) failed in write: %s\n",
+ log_get_prefix (NULL), fd,
+ w32_strerror (strerr, sizeof strerr));
n = -1;
}
else
@@ -395,21 +400,21 @@
(WSAGetLastError () == WSAEINPROGRESS ||
WSAGetLastError () == WSAEWOULDBLOCK))
{
- if (ev == NULL)
+ if (!ev)
{
ev = do_pth_event (PTH_EVENT_FD|PTH_UNTIL_FD_READABLE|
PTH_MODE_STATIC, &ev_key, fd);
- if (ev == NULL)
+ if (!ev)
{
leave_pth (__FUNCTION__);
return -1;
}
- if (ev_extra != NULL)
+ if (ev_extra)
pth_event_concat (ev, ev_extra, NULL);
}
/* Wait until accept has a chance. */
do_pth_wait (ev);
- if (ev_extra != NULL)
+ if (ev_extra)
{
pth_event_isolate (ev);
if (do_pth_event_status (ev) != PTH_STATUS_OCCURRED)
@@ -452,9 +457,10 @@
{
char strerr[256];
- fprintf (stderr, "%s: pth_release_mutex %p failed: %s\n",
- log_get_prefix (NULL), *mutex,
- w32_strerror (strerr, sizeof strerr));
+ if (DBG_ERROR)
+ fprintf (stderr, "%s: pth_release_mutex %p failed: %s\n",
+ log_get_prefix (NULL), *mutex,
+ w32_strerror (strerr, sizeof strerr));
rc = FALSE;
}
else
@@ -483,9 +489,10 @@
{
char strerr[256];
- fprintf (stderr, "%s: pth_mutex_acquire for %p failed: %s\n",
- log_get_prefix (NULL), *mutex,
- w32_strerror (strerr, sizeof strerr));
+ if (DBG_ERROR)
+ fprintf (stderr, "%s: pth_mutex_acquire for %p failed: %s\n",
+ log_get_prefix (NULL), *mutex,
+ w32_strerror (strerr, sizeof strerr));
}
rc = FALSE;
break;
@@ -495,9 +502,10 @@
break;
default:
- fprintf (stderr, "%s: WaitForSingleObject returned unexpected "
- "code %d for mutex %p\n",
- log_get_prefix (NULL), code, *mutex);
+ if (DBG_ERROR)
+ fprintf (stderr, "%s: WaitForSingleObject returned unexpected "
+ "code %d for mutex %p\n",
+ log_get_prefix (NULL), code, *mutex);
rc = FALSE;
break;
}
@@ -576,8 +584,9 @@
if (val)
{
hd->flags |= PTH_ATTR_JOINABLE;
- fprintf (stderr, "%s: pth_attr_set: PTH_ATTR_JOINABLE\n",
- log_get_prefix (NULL));
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_attr_set: PTH_ATTR_JOINABLE\n",
+ log_get_prefix (NULL));
}
break;
@@ -587,8 +596,9 @@
{
hd->flags |= PTH_ATTR_STACK_SIZE;
hd->stack_size = val;
- fprintf (stderr, "%s: pth_attr_set: PTH_ATTR_STACK_SIZE %d\n",
- log_get_prefix (NULL), val);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_attr_set: PTH_ATTR_STACK_SIZE %d\n",
+ log_get_prefix (NULL), val);
}
break;
@@ -602,8 +612,9 @@
if (!hd->name)
return FALSE;
hd->flags |= PTH_ATTR_NAME;
- fprintf (stderr, "%s: pth_attr_set: PTH_ATTR_NAME %s\n",
- log_get_prefix (NULL), hd->name);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_attr_set: PTH_ATTR_NAME %s\n",
+ log_get_prefix (NULL), hd->name);
}
break;
@@ -647,14 +658,16 @@
FIXME: We should no use th W32's Thread handle directly but keep
our own thread control structure. CTX may be used for that. */
- fprintf (stderr, "%s: do_pth_spawn creating thread ...\n",
- log_get_prefix (NULL));
+ if (DBG_INFO)
+ fprintf (stderr, "%s: do_pth_spawn creating thread ...\n",
+ log_get_prefix (NULL));
th = CreateThread (&sa, hd->stack_size,
(LPTHREAD_START_ROUTINE)launch_thread,
ctx, CREATE_SUSPENDED, &tid);
ctx->th = th;
- fprintf (stderr, "%s: do_pth_spawn created thread %p\n",
- log_get_prefix (NULL),th);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: do_pth_spawn created thread %p\n",
+ log_get_prefix (NULL),th);
if (!th)
free (ctx);
else
@@ -787,7 +800,8 @@
case CTRL_BREAK_EVENT: pth_signo = SIGTERM; break;
}
SetEvent (pth_signo_ev);
- fprintf (stderr, "%s: sig_handler=%d\n", log_get_prefix (NULL), pth_signo);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: sig_handler=%d\n", log_get_prefix (NULL), pth_signo);
return TRUE;
}
@@ -799,7 +813,8 @@
pth_event_t ev;
int rc;
- fprintf (stderr, "%s: pth_event spec=%lu\n", log_get_prefix (NULL), spec);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_event spec=%lu\n", log_get_prefix (NULL), spec);
ev = calloc (1, sizeof *ev);
if (!ev)
return NULL;
@@ -811,8 +826,9 @@
ev->u_type = PTH_EVENT_SIGS;
ev->val = va_arg (arg, int *);
rc = SetConsoleCtrlHandler (sig_handler, TRUE);
- fprintf (stderr, "%s: pth_event: sigs rc=%d\n",
- log_get_prefix (NULL), rc);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_event: sigs rc=%d\n",
+ log_get_prefix (NULL), rc);
}
else if (spec & PTH_EVENT_FD)
{
@@ -823,8 +839,9 @@
ev->u_type = PTH_EVENT_FD;
va_arg (arg, pth_key_t);
ev->u.fd = va_arg (arg, int);
- fprintf (stderr, "%s: pth_event: fd=%d\n",
- log_get_prefix (NULL), ev->u.fd);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_event: fd=%d\n",
+ log_get_prefix (NULL), ev->u.fd);
}
else if (spec & PTH_EVENT_TIME)
{
@@ -854,6 +871,8 @@
free (ev);
return NULL;
}
+ ev->next = ev;
+ ev->prev = ev;
return ev;
}
@@ -938,8 +957,9 @@
while (1)
{
n = select (fd+1, &r, &w, NULL, &tv);
- fprintf (stderr, "%s: wait_for_fd=%d fd %d (ec=%d)\n",
- log_get_prefix (NULL), n, fd,(int)WSAGetLastError ());
+ if (DBG_INFO)
+ fprintf (stderr, "%s: wait_for_fd=%d fd %d (ec=%d)\n",
+ log_get_prefix (NULL), n, fd,(int)WSAGetLastError ());
if (n == -1)
break;
if (!n)
@@ -1036,8 +1056,9 @@
if (sigpresent (ev->u.sig, pth_signo) &&
WaitForSingleObject (pth_signo_ev, 0) == WAIT_OBJECT_0)
{
- fprintf (stderr, "%s: pth_event_occurred: sig signaled.\n",
- log_get_prefix (NULL));
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_event_occurred: sig signaled.\n",
+ log_get_prefix (NULL));
(*ev->val) = pth_signo;
ret = 1;
}
@@ -1090,18 +1111,21 @@
static int
do_pth_event_free (pth_event_t ev, int mode)
{
- pth_event_t n;
+ if (!ev)
+ return FALSE;
if (mode == PTH_FREE_ALL)
{
- while (ev)
+ pth_event_t cur = ev;
+ do
{
- n = ev->next;
- CloseHandle (ev->hd);
- ev->hd = NULL;
- free (ev);
- ev = n;
+ pth_event_t next = cur->next;
+ CloseHandle (cur->hd);
+ cur->hd = NULL;
+ free (cur);
+ cur = next;
}
+ while (cur != ev);
}
else if (mode == PTH_FREE_THIS)
{
@@ -1111,6 +1135,8 @@
ev->hd = NULL;
free (ev);
}
+ else
+ return FALSE;
return TRUE;
}
@@ -1131,24 +1157,39 @@
pth_event_t
pth_event_isolate (pth_event_t ev)
{
- pth_event_t ring = NULL;
+ pth_event_t ring;
if (!ev)
return NULL;
+ if (ev->next == ev && ev->prev == ev)
+ return NULL; /* Only one event. */
+
+ ring = ev->next;
+ ev->prev->next = ev->next;
+ ev->next->prev = ev->prev;
+ ev->prev = ev;
+ ev->next = ev;
return ring;
}
static int
-_pth_event_count (pth_event_t ev)
+event_count (pth_event_t ev)
{
- pth_event_t p;
- int cnt=0;
+ pth_event_t r;
+ int cnt = 0;
+
+ if (ev)
+ {
+ r = ev;
+ do
+ {
+ cnt++;
+ r = r->next;
+ }
+ while (r != ev);
+ }
- if (!ev)
- return 0;
- for (p = ev; p; p = p->next)
- cnt++;
return cnt;
}
@@ -1166,13 +1207,15 @@
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof sa;
- fprintf (stderr, "%s: spawn_helper_thread creating thread ...\n",
- log_get_prefix (NULL));
+ if (DBG_INFO)
+ fprintf (stderr, "%s: spawn_helper_thread creating thread ...\n",
+ log_get_prefix (NULL));
th = CreateThread (&sa, 32*1024,
(LPTHREAD_START_ROUTINE)func,
arg, 0, &tid);
- fprintf (stderr, "%s: spawn_helper_thread created thread %p\n",
- log_get_prefix (NULL), th);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: spawn_helper_thread created thread %p\n",
+ log_get_prefix (NULL), th);
return th;
}
@@ -1197,7 +1240,8 @@
pth_event_t ev = ctx;
wait_for_fd (ev->u.fd, ev->flags & PTH_UNTIL_FD_READABLE, 3600);
- fprintf (stderr, "%s: wait_fd_thread: exit.\n", log_get_prefix (NULL));
+ if (DBG_INFO)
+ fprintf (stderr, "%s: wait_fd_thread: exit.\n", log_get_prefix (NULL));
SetEvent (ev->hd);
ExitThread (0);
return NULL;
@@ -1211,7 +1255,8 @@
int n = ev->u.tv.tv_sec*1000;
Sleep (n);
SetEvent (ev->hd);
- fprintf (stderr, "%s: wait_timer_thread: exit.\n", log_get_prefix (NULL));
+ if (DBG_INFO)
+ fprintf (stderr, "%s: wait_timer_thread: exit.\n", log_get_prefix (NULL));
ExitThread (0);
return NULL;
}
@@ -1223,51 +1268,62 @@
HANDLE waitbuf[MAXIMUM_WAIT_OBJECTS/2];
int hdidx[MAXIMUM_WAIT_OBJECTS/2];
DWORD n = 0;
- pth_event_t tmp;
int pos=0, i=0;
if (!ev)
return 0;
- n =_pth_event_count (ev);
+ n = event_count (ev);
if (n > MAXIMUM_WAIT_OBJECTS/2)
return -1;
- fprintf (stderr, "%s: pth_wait: cnt %lu\n", log_get_prefix (NULL), n);
- for (tmp = ev; tmp; tmp = tmp->next)
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_wait: cnt %lu\n", log_get_prefix (NULL), n);
+ if (ev)
{
- switch (tmp->u_type)
+ pth_event_t r = ev;
+ do
{
- case 0:
- waitbuf[pos++] = tmp->hd;
- break;
-
- case PTH_EVENT_SIGS:
- waitbuf[pos++] = pth_signo_ev;
- fprintf (stderr, "pth_wait: add signal event.\n");
- break;
-
- case PTH_EVENT_FD:
- fprintf (stderr, "pth_wait: spawn event wait thread.\n");
- hdidx[i++] = pos;
- waitbuf[pos++] = spawn_helper_thread (wait_fd_thread, tmp);
- break;
-
- case PTH_EVENT_TIME:
- fprintf (stderr, "pth_wait: spawn event timer thread.\n");
- hdidx[i++] = pos;
- waitbuf[pos++] = spawn_helper_thread (wait_timer_thread, tmp);
- break;
-
- case PTH_EVENT_MUTEX:
- fprintf (stderr, "pth_wait: add mutex event.\n");
- break;
+ switch (r->u_type)
+ {
+ case 0:
+ waitbuf[pos++] = r->hd;
+ break;
+
+ case PTH_EVENT_SIGS:
+ waitbuf[pos++] = pth_signo_ev;
+ if (DBG_INFO)
+ fprintf (stderr, "pth_wait: add signal event.\n");
+ break;
+
+ case PTH_EVENT_FD:
+ if (DBG_INFO)
+ fprintf (stderr, "pth_wait: spawn event wait thread.\n");
+ hdidx[i++] = pos;
+ waitbuf[pos++] = spawn_helper_thread (wait_fd_thread, r);
+ break;
+
+ case PTH_EVENT_TIME:
+ if (DBG_INFO)
+ fprintf (stderr, "pth_wait: spawn event timer thread.\n");
+ hdidx[i++] = pos;
+ waitbuf[pos++] = spawn_helper_thread (wait_timer_thread, r);
+ break;
+
+ case PTH_EVENT_MUTEX:
+ if (DBG_INFO)
+ fprintf (stderr, "pth_wait: ignoring mutex event.\n");
+ break;
+ }
}
+ while ( r != ev );
}
- fprintf (stderr, "%s: pth_wait: set %d\n", log_get_prefix (NULL), pos);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_wait: set %d\n", log_get_prefix (NULL), pos);
n = WaitForMultipleObjects (pos, waitbuf, FALSE, INFINITE);
free_helper_threads (waitbuf, hdidx, i);
- fprintf (stderr, "%s: pth_wait: n %ld\n", log_get_prefix (NULL), n);
+ if (DBG_INFO)
+ fprintf (stderr, "%s: pth_wait: n %ld\n", log_get_prefix (NULL), n);
if (n != WAIT_TIMEOUT)
return 1;
More information about the Gnupg-commits
mailing list