Re: [AD] Potential uthreads hang on cleanup?

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Chris wrote:

As detailed here:
http://www.allegro.cc/forums/view_thread.php?_id=438553#target

it seems pthread_cancel has some problems shutting down on some systems. The poster says it returns what resolves to "Resource temporarily unavailable", although my man pages say pthread_cancel can only fail if the requested thread doesn't exist, so I'm not really sure what's going on or what the right way to deal with it is. In either case, I attached a simple patch that fixes his issue.


The right way to do things is not to use pthread_cancel at all. Sorry, I was still new to threads when I wrote that code. Patch attached, but I didn't test it with Kdevelop or whatever.

Peter
Index: uthreads.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/unix/uthreads.c,v
retrieving revision 1.19
diff -u -r1.19 uthreads.c
--- uthreads.c	29 Nov 2004 07:58:34 -0000	1.19
+++ uthreads.c	10 Dec 2004 08:08:03 -0000
@@ -38,6 +38,7 @@
 static int max_func; /* highest+1 used entry */
 
 static pthread_t thread = 0;
+static int thread_alive = FALSE;
 static pthread_mutex_t cli_mutex;
 static pthread_cond_t cli_cond;
 static int cli_count;
@@ -70,12 +71,14 @@
    unsigned long interval;
    int n;
 
+   ASSERT(thread_alive);
+
    block_all_signals();
 
    interval = 0;
    gettimeofday(&old_time, 0);
 
-   while (1) {
+   while (thread_alive) {
       gettimeofday(&new_time, 0);
       /* add the new time difference to the remainder of the old difference */
       interval += ((new_time.tv_sec - old_time.tv_sec) * 1000000L +
@@ -107,7 +110,6 @@
       delay.tv_sec = 0;
       delay.tv_usec = 1000;
       select(0, NULL, NULL, NULL, &delay);
-      pthread_testcancel();
    }
 
    return NULL;
@@ -123,6 +125,7 @@
    int i;
 
    ASSERT(thread == 0);
+   ASSERT(!thread_alive);
 
    for (i = 0; i < MAX_FUNCS; i++)
       funcs[i] = NULL;
@@ -133,7 +136,9 @@
    pthread_mutex_init(&cli_mutex, NULL);
    pthread_cond_init(&cli_cond, NULL);
 
+   thread_alive = TRUE;
    if (pthread_create(&thread, NULL, bg_man_pthreads_threadfunc, NULL)) {
+      thread_alive = FALSE;
       pthread_mutex_destroy(&cli_mutex);
       pthread_cond_destroy(&cli_cond);
       thread = 0;
@@ -150,8 +155,10 @@
  */
 static void bg_man_pthreads_exit(void)
 {
+   ASSERT(!!thread == !!thread_alive);
+
    if (thread) {
-      pthread_cancel(thread);
+      thread_alive = FALSE;
       pthread_join(thread, NULL);
       pthread_mutex_destroy(&cli_mutex);
       pthread_cond_destroy(&cli_cond);


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/