svn commit: trunk/uClibc/libc: inet/rpc stdio stdlib/malloc

andersen at uclibc.org andersen at uclibc.org
Thu Dec 7 23:24:20 UTC 2006


Author: andersen
Date: 2006-12-07 15:24:19 -0800 (Thu, 07 Dec 2006)
New Revision: 16802

Log:
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.


Modified:
   trunk/uClibc/libc/inet/rpc/getrpcent.c
   trunk/uClibc/libc/stdio/_stdio.c
   trunk/uClibc/libc/stdlib/malloc/heap.h
   trunk/uClibc/libc/stdlib/malloc/malloc.h


Changeset:
Modified: trunk/uClibc/libc/inet/rpc/getrpcent.c
===================================================================
--- trunk/uClibc/libc/inet/rpc/getrpcent.c	2006-12-07 23:24:02 UTC (rev 16801)
+++ trunk/uClibc/libc/inet/rpc/getrpcent.c	2006-12-07 23:24:19 UTC (rev 16802)
@@ -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 @@
 
 #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 @@
 
 	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 @@
 		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 @@
 		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;
 }
 

Modified: trunk/uClibc/libc/stdio/_stdio.c
===================================================================
--- trunk/uClibc/libc/stdio/_stdio.c	2006-12-07 23:24:02 UTC (rev 16801)
+++ trunk/uClibc/libc/stdio/_stdio.c	2006-12-07 23:24:19 UTC (rev 16802)
@@ -154,9 +154,9 @@
 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 @@
 /* 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 @@
 	/* 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 @@
 			__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... */
 	}

Modified: trunk/uClibc/libc/stdlib/malloc/heap.h
===================================================================
--- trunk/uClibc/libc/stdlib/malloc/heap.h	2006-12-07 23:24:02 UTC (rev 16801)
+++ trunk/uClibc/libc/stdlib/malloc/heap.h	2006-12-07 23:24:19 UTC (rev 16802)
@@ -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 @@
   /* 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_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.  */

Modified: trunk/uClibc/libc/stdlib/malloc/malloc.h
===================================================================
--- trunk/uClibc/libc/stdlib/malloc/malloc.h	2006-12-07 23:24:02 UTC (rev 16801)
+++ trunk/uClibc/libc/stdlib/malloc/malloc.h	2006-12-07 23:24:19 UTC (rev 16802)
@@ -125,11 +125,11 @@
 /* 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 @@
    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__ */




More information about the uClibc-cvs mailing list