Re: [AD] small bug in uthreads.c

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


Elias Pschernig wrote:

As this thread http://www.allegro.cc/forums/view_thread.php?_id=433516
shows, there is a bug in uthreads.c. It tries to close the background
thread, even if it failed creating it. (I'm puzzled though how the
creation of the thread can fail.)


Your patch is incomplete and I don't think another variable is necessary. Alternative attached.

Peter

Index: src/unix/uthreads.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/unix/uthreads.c,v
retrieving revision 1.18
diff -u -r1.18 uthreads.c
--- src/unix/uthreads.c	29 Jul 2004 08:27:35 -0000	1.18
+++ src/unix/uthreads.c	27 Nov 2004 05:57:05 -0000
@@ -37,7 +37,7 @@
 static bg_func funcs[MAX_FUNCS];
 static int max_func; /* highest+1 used entry */
 
-static pthread_t thread;
+static pthread_t thread = 0;
 static pthread_mutex_t cli_mutex;
 static pthread_cond_t cli_cond;
 static int cli_count;
@@ -122,6 +122,8 @@
 {
    int i;
 
+   ASSERT(thread == 0);
+
    for (i = 0; i < MAX_FUNCS; i++)
       funcs[i] = NULL;
 
@@ -134,6 +136,7 @@
    if (pthread_create(&thread, NULL, bg_man_pthreads_threadfunc, NULL)) {
       pthread_mutex_destroy(&cli_mutex);
       pthread_cond_destroy(&cli_cond);
+      thread = 0;
       return -1;
    }
 
@@ -147,10 +150,15 @@
  */
 static void bg_man_pthreads_exit(void)
 {
-   pthread_cancel(thread);
-   pthread_join(thread, NULL);
-   pthread_mutex_destroy(&cli_mutex);
-   pthread_cond_destroy(&cli_cond);
+   if (thread) {
+      pthread_cancel(thread);
+      pthread_join(thread, NULL);
+      pthread_mutex_destroy(&cli_mutex);
+      pthread_cond_destroy(&cli_cond);
+      thread = 0;
+   }
+
+   ASSERT(thread == 0);
 }
 
 
Index: src/x/xsystem.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/x/xsystem.c,v
retrieving revision 1.30
diff -u -r1.30 xsystem.c
--- src/x/xsystem.c	5 Nov 2004 17:08:15 -0000	1.30
+++ src/x/xsystem.c	27 Nov 2004 05:57:06 -0000
@@ -251,7 +251,10 @@
 #endif
 
 #ifdef ALLEGRO_MULTITHREADED
-   _unix_destroy_mutex (_xwin.mutex);
+   if (_xwin.mutex) {
+      _unix_destroy_mutex(_xwin.mutex);
+      _xwin.mutex = NULL;
+   }
 #endif
 }
 


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