pthreads, part 4

Alex Waugh alex at alexwaugh.com
Thu Dec 26 10:33:43 PST 2002


The first in a series of patches to make Unixlib thread-safe. Most of
the leg-work for this has been done by Jeffrey Lee (Thanks).

This installment covers all unixlib/source/*.c

Index: unixlib/source/abort.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/abort.c,v
retrieving revision 1.2
diff -u -r1.2 abort.c
--- unixlib/source/abort.c	29 Jan 2001 15:10:18 -0000	1.2
+++ unixlib/source/abort.c	26 Dec 2002 17:40:34 -0000
@@ -15,11 +15,14 @@
 #include <signal.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <pthread.h>
 
 void
 abort (void)
 {
   sigset_t sigs;
+
+  PTHREAD_UNSAFE
 
   sigs = sigmask (SIGABRT);
   sigprocmask (SIG_UNBLOCK, &sigs, NULL);
Index: unixlib/source/assert.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/assert.c,v
retrieving revision 1.3
diff -u -r1.3 assert.c
--- unixlib/source/assert.c	14 Feb 2002 15:56:33 -0000	1.3
+++ unixlib/source/assert.c	26 Dec 2002 17:40:34 -0000
@@ -15,6 +15,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
+#include <pthread.h>
 
 /* Output something of the form:
       foo.c, line 1234: bar: Assertion failed: a == b
@@ -22,6 +23,8 @@
 void
 __assert_fail (const char *exp, const char *file, int line, const char *function)
 {
+  PTHREAD_UNSAFE /* Not strictly unsafe, but should help debugging */
+
   fprintf (stderr, "\n\"%s\", line %d: %s%sAssertion failed: %s\n",
   	   file, line,
   	   function ? function : "",
Index: unixlib/source/atexit.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/atexit.c,v
retrieving revision 1.2
diff -u -r1.2 atexit.c
--- unixlib/source/atexit.c	29 Jan 2001 15:10:18 -0000	1.2
+++ unixlib/source/atexit.c	26 Dec 2002 17:40:34 -0000
@@ -13,10 +13,13 @@
 #endif
 
 #include <stdlib.h>
+#include <pthread.h>
 
 int
 atexit (void (*atexit_function) (void))
 {
+  PTHREAD_UNSAFE
+
   if (__atexit_function_count >= __MAX_ATEXIT_FUNCTION_COUNT)
     return -1;
 
Index: unixlib/source/qsort.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/qsort.c,v
retrieving revision 1.3
diff -u -r1.3 qsort.c
--- unixlib/source/qsort.c	14 Feb 2002 15:56:33 -0000	1.3
+++ unixlib/source/qsort.c	26 Dec 2002 17:40:35 -0000
@@ -17,6 +17,7 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
+#include <pthread.h>
 
 #define N_INSERT	8
 
@@ -168,11 +169,19 @@
        register int (*c) (const void *, const void *))
 
 {
+  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+  /* Use a mutex so that threads aren't disabled while calling the user function */
+
   if (n < 2)
     return;
 
+  pthread_mutex_lock (&mutex);
+
   if (!(__t = malloc (z)))
-    return;
+    {
+      pthread_mutex_unlock (&mutex);
+      return;
+    }
 
   __z = z;
   __c = c;
@@ -188,4 +197,6 @@
     __isort ((char *) v, n);
 
   free (__t);
+
+  pthread_mutex_unlock (&mutex);
 }
Index: unixlib/source/strsignal.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/strsignal.c,v
retrieving revision 1.3
diff -u -r1.3 strsignal.c
--- unixlib/source/strsignal.c	24 Sep 2002 21:02:36 -0000	1.3
+++ unixlib/source/strsignal.c	26 Dec 2002 17:40:36 -0000
@@ -22,12 +22,7 @@
 strsignal (int signum)
 {
   if (signum < 0 || signum >= NSIG)
-    {
-      static char unknown_signal[] = "Unknown signal 00000000000";
-
-      sprintf (unknown_signal, "Unknown signal %d", signum);
-      return unknown_signal;
-    }
+    return "Unknown signal";
 
   return (char *) sys_siglist[signum];
 }
Index: unixlib/source/strtod.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/strtod.c,v
retrieving revision 1.2
diff -u -r1.2 strtod.c
--- unixlib/source/strtod.c	29 Jan 2001 15:10:19 -0000	1.2
+++ unixlib/source/strtod.c	26 Dec 2002 17:40:38 -0000
@@ -203,6 +203,8 @@
 #include "math.h"
 #endif
 
+#include <pthread.h>
+
 #ifdef __cplusplus
 extern "C"
 {
@@ -378,6 +380,8 @@
     int x;
     Bigint *rv;
 
+    PTHREAD_UNSAFE
+
     if ((rv = freelist[k]))
       {
 	freelist[k] = rv->next;
@@ -397,6 +401,8 @@
     Bfree
     (Bigint * v)
   {
+    PTHREAD_UNSAFE
+
     if (v)
       {
 	v->next = freelist[v->k];
@@ -689,6 +695,8 @@
     static int p05[3] =
     {5, 25, 125};
 
+    PTHREAD_UNSAFE
+
     if ((i = k & 3))
         b = multadd (b, p05[i - 1], 0);
 
@@ -1981,6 +1989,8 @@
     char *s, *s0;
     static Bigint *result;
     static int result_k;
+
+    PTHREAD_UNSAFE
 
     if (result)
       {
Index: unixlib/source/strtok.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/strtok.c,v
retrieving revision 1.2
diff -u -r1.2 strtok.c
--- unixlib/source/strtok.c	29 Jan 2001 15:10:19 -0000	1.2
+++ unixlib/source/strtok.c	26 Dec 2002 17:40:38 -0000
@@ -14,6 +14,7 @@
 
 #include <string.h>
 
+/* Defined by POSIX as not threadsafe */ 
 char *
 strtok (register char *s1, register const char *s2)
 
Index: unixlib/source/termcap.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/termcap.c,v
retrieving revision 1.3
diff -u -r1.3 termcap.c
--- unixlib/source/termcap.c	14 Feb 2002 15:56:33 -0000	1.3
+++ unixlib/source/termcap.c	26 Dec 2002 17:40:39 -0000
@@ -18,6 +18,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <pthread.h>
 
 #include <termcap.h>
 
@@ -80,6 +81,8 @@
   FILE *tfile = NULL;
   int nbyt, rval;
 
+  PTHREAD_UNSAFE
+
   if (bp == NULL)
     return -1;
 
@@ -287,6 +290,8 @@
   char *t_tbp_;
   char *tcnam, *tcn;
 
+  PTHREAD_UNSAFE
+
 #ifdef T_DEBUG
   fputs ("t_tentcp()\n", t_debug);
 #endif
@@ -380,6 +385,8 @@
   bufp = buf;
   nbyt = 0;
 
+  PTHREAD_UNSAFE
+
   while (-1)
     {
       if (!fgets ((char *) bufp, t_tbpspace - nbyt, tfile))
@@ -410,6 +417,8 @@
   unsigned char *bptr;
   int found;
 
+  PTHREAD_UNSAFE
+
   if (id == NULL || t_tbpstart == NULL)
     return NULL;
 
@@ -504,6 +513,8 @@
   unsigned char obuf[4];
   char *rval;
 
+  PTHREAD_UNSAFE
+  
 #ifdef T_DEBUG
   fprintf (t_debug, "tgetstr(\"%s\")\n", id);
 #endif
@@ -632,6 +643,8 @@
   char *cmp, *rstrp, *cp;
   unsigned int f;
 
+  PTHREAD_UNSAFE
+  
 #define TC(f) (((f) & TG_revxy) ? destcol : destline)
 
 #ifdef T_DEBUG
@@ -776,6 +789,8 @@
 {
   unsigned int f;
 
+  PTHREAD_UNSAFE
+  
 t_tccalc:
 
   f = (*_f);

-- 
Alex Waugh                                           alex at alexwaugh.com

PHP, Roots, Subversion, WebJames and more from http://www.alexwaugh.com/



More information about the gcc mailing list