Selaa lähdekoodia

Major cleanup of internal mutex locking. Be more consistant in how we do
things, and avoid potential deadlocks caused when a thread holding a uClibc
internal lock get canceled and terminates without releasing the lock. This
change also provides a single place, bits/uClibc_mutex.h, for thread libraries
to modify to change all instances of internal locking.

Eric Andersen 17 vuotta sitten
vanhempi
commit
275a4c4e6f
4 muutettua tiedostoa jossa 32 lisäystä ja 35 poistoa
  1. 17 20
      libc/inet/rpc/getrpcent.c
  2. 7 7
      libc/stdio/_stdio.c
  3. 4 4
      libc/stdlib/malloc/heap.h
  4. 4 4
      libc/stdlib/malloc/malloc.h

+ 17 - 20
libc/inet/rpc/getrpcent.c

@@ -7,23 +7,23 @@
  * may copy or modify Sun RPC without charge, but are not authorized
  * to license or distribute it to anyone else except as part of a product or
  * program developed by the user.
- * 
+ *
  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- * 
+ *
  * Sun RPC is provided with no support and without any obligation on the
  * part of Sun Microsystems, Inc. to assist in its use, correction,
  * modification or enhancement.
- * 
+ *
  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  * OR ANY PART THEREOF.
- * 
+ *
  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  * or profits or other special, indirect and consequential damages, even if
  * Sun has been advised of the possibility of such damages.
- * 
+ *
  * Sun Microsystems, Inc.
  * 2550 Garcia Avenue
  * Mountain View, California  94043
@@ -275,14 +275,11 @@ static struct rpcent *interpret(register struct rpcdata *d)
 
 #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
 
-#if defined(__UCLIBC_HAS_THREADS__)
-# include <pthread.h>
-static pthread_mutex_t rpcdata_lock = PTHREAD_MUTEX_INITIALIZER;
-#endif
-#define LOCK    __pthread_mutex_lock(&rpcdata_lock)
-#define UNLOCK  __pthread_mutex_unlock(&rpcdata_lock)
+#include <bits/uClibc_mutex.h>
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
+
 
-static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer, 
+static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
 		size_t buflen, struct rpcent **result)
 {
 	size_t i, s;
@@ -298,7 +295,7 @@ static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buff
 
 	result_buf->r_number = r->r_number;
 
-	/* copy the aliases ... need to not only copy the alias strings, 
+	/* copy the aliases ... need to not only copy the alias strings,
 	 * but the array of pointers to the alias strings */
 	i = 0;
 	while (r->r_aliases[i++]) ;
@@ -339,9 +336,9 @@ int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
 		size_t buflen, struct rpcent **result)
 {
 	int ret;
-	LOCK;
+	__UCLIBC_MUTEX_LOCK(mylock);
 	ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
-	UNLOCK;
+	__UCLIBC_MUTEX_UNLOCK(mylock);
 	return ret;
 }
 
@@ -349,19 +346,19 @@ int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
 		size_t buflen, struct rpcent **result)
 {
 	int ret;
-	LOCK;
+	__UCLIBC_MUTEX_LOCK(mylock);
 	ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
-	UNLOCK;
+	__UCLIBC_MUTEX_UNLOCK(mylock);
 	return ret;
 }
 
-int getrpcent_r(struct rpcent *result_buf, char *buffer, 
+int getrpcent_r(struct rpcent *result_buf, char *buffer,
 		size_t buflen, struct rpcent **result)
 {
 	int ret;
-	LOCK;
+	__UCLIBC_MUTEX_LOCK(mylock);
 	ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
-	UNLOCK;
+	__UCLIBC_MUTEX_UNLOCK(mylock);
 	return ret;
 }
 

+ 7 - 7
libc/stdio/_stdio.c

@@ -154,9 +154,9 @@ FILE *__stdout = _stdio_streams + 1; /* For putchar() macro. */
 FILE *_stdio_openlist = _stdio_streams;
 
 # ifdef __UCLIBC_HAS_THREADS__
-pthread_mutex_t _stdio_openlist_add_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+__UCLIBC_MUTEX_INIT(_stdio_openlist_add_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
 #ifdef __STDIO_BUFFERS
-pthread_mutex_t _stdio_openlist_del_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+__UCLIBC_MUTEX_INIT(_stdio_openlist_del_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
 volatile int _stdio_openlist_use_count = 0;
 int _stdio_openlist_del_count = 0;
 #endif
@@ -169,10 +169,10 @@ int _stdio_openlist_del_count = 0;
 /* 2 if threading not initialized and 0 otherwise; */
 int _stdio_user_locking = 2;
 
-void attribute_hidden __stdio_init_mutex(pthread_mutex_t *m)
+void attribute_hidden __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m)
 {
-	static const pthread_mutex_t __stdio_mutex_initializer
-		= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+	const __UCLIBC_MUTEX_STATIC(__stdio_mutex_initializer,
+		PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
 
 	memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
 }
@@ -190,7 +190,7 @@ void attribute_hidden _stdio_term(void)
 	/* First, make sure the open file list is unlocked.  If it was
 	 * locked, then I suppose there is a chance that a pointer in the
 	 * chain might be corrupt due to a partial store.
-	 */ 
+	 */
 	__stdio_init_mutex(&_stdio_openlist_add_lock);
 #warning check
 #ifdef __STDIO_BUFFERS
@@ -214,7 +214,7 @@ void attribute_hidden _stdio_term(void)
 			__STDIO_STREAM_DISABLE_PUTC(ptr);
 			__STDIO_STREAM_INIT_BUFREAD_BUFPOS(ptr);
 		}
-		
+
 		ptr->__user_locking = 1; /* Set locking mode to "by caller". */
 		__stdio_init_mutex(&ptr->__lock); /* Shouldn't be necessary, but... */
 	}

+ 4 - 4
libc/stdlib/malloc/heap.h

@@ -16,7 +16,7 @@
 
 /* On multi-threaded systems, the heap includes a lock.  */
 #ifdef __UCLIBC_HAS_THREADS__
-# include <pthread.h>
+# include <bits/uClibc_mutex.h>
 # define HEAP_USE_LOCKING
 #endif
 
@@ -39,7 +39,7 @@ struct heap
   /* A lock that can be used by callers to control access to the heap.
      The heap code _does not_ use this lock, it's merely here for the
      convenience of users!  */
-  pthread_mutex_t lock;
+  __UCLIBC_MUTEX_TYPE lock;
 #endif
 };
 
@@ -135,8 +135,8 @@ extern void __heap_dump (struct heap *heap, const char *str);
 extern void __heap_check (struct heap *heap, const char *str);
 
 
-#define __heap_lock(heap)	__pthread_mutex_lock (&(heap)->lock)
-#define __heap_unlock(heap)	__pthread_mutex_unlock (&(heap)->lock)
+#define __heap_lock(heap)	__UCLIBC_MUTEX_LOCK (&(heap)->lock)
+#define __heap_unlock(heap)	__UCLIBC_MUTEX_UNLOCK (&(heap)->lock)
 
 
 /* Delete the free-area FA from HEAP.  */

+ 4 - 4
libc/stdlib/malloc/malloc.h

@@ -125,11 +125,11 @@ extern int __malloc_mmb_debug;
 /* Locking for multithreaded apps.  */
 #ifdef __UCLIBC_HAS_THREADS__
 
-# include <pthread.h>
+# include <bits/uClibc_mutex.h>
 
 # define MALLOC_USE_LOCKING
 
-typedef pthread_mutex_t malloc_mutex_t;
+typedef __UCLIBC_MUTEX_TYPE malloc_mutex_t;
 # define MALLOC_MUTEX_INIT	PTHREAD_MUTEX_INITIALIZER
 
 # ifdef MALLOC_USE_SBRK
@@ -138,8 +138,8 @@ typedef pthread_mutex_t malloc_mutex_t;
    things will break if these multiple calls are interleaved with another
    thread's use of sbrk!).  */
 extern malloc_mutex_t __malloc_sbrk_lock;
-#  define __malloc_lock_sbrk()	__pthread_mutex_lock (&__malloc_sbrk_lock)
-#  define __malloc_unlock_sbrk() __pthread_mutex_unlock (&__malloc_sbrk_lock)
+#  define __malloc_lock_sbrk()	__UCLIBC_MUTEX_LOCK (&__malloc_sbrk_lock)
+#  define __malloc_unlock_sbrk() __UCLIBC_MUTEX_UNLOCK (&__malloc_sbrk_lock)
 # endif /* MALLOC_USE_SBRK */
 
 #else /* !__UCLIBC_HAS_THREADS__ */