From 1478c2de052374c6356db5513749a144c13791b1 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 7 Dec 2006 23:24:02 +0000 Subject: 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. --- libc/inet/getnetent.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'libc/inet/getnetent.c') diff --git a/libc/inet/getnetent.c b/libc/inet/getnetent.c index d3fdb988a..d5c25034c 100644 --- a/libc/inet/getnetent.c +++ b/libc/inet/getnetent.c @@ -29,12 +29,8 @@ libc_hidden_proto(rewind) libc_hidden_proto(fgets) libc_hidden_proto(abort) -#ifdef __UCLIBC_HAS_THREADS__ -# include -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; -#endif -#define LOCK __pthread_mutex_lock(&mylock) -#define UNLOCK __pthread_mutex_unlock(&mylock) +#include +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); @@ -50,13 +46,13 @@ int _net_stayopen attribute_hidden; libc_hidden_proto(setnetent) void setnetent(int f) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (netf == NULL) netf = fopen(NETDB, "r" ); else rewind(netf); _net_stayopen |= f; - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return; } libc_hidden_def(setnetent) @@ -64,13 +60,13 @@ libc_hidden_def(setnetent) libc_hidden_proto(endnetent) void endnetent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (netf) { fclose(netf); netf = NULL; } _net_stayopen = 0; - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_def(endnetent) @@ -92,11 +88,11 @@ struct netent *getnetent(void) { char *p; register char *cp, **q; + struct netent *rv = NULL; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) { - UNLOCK; - return (NULL); + goto DONE; } again: @@ -108,8 +104,7 @@ again: p = fgets(line, BUFSIZ, netf); if (p == NULL) { - UNLOCK; - return (NULL); + goto DONE; } if (*p == '#') goto again; @@ -144,7 +139,9 @@ again: *cp++ = '\0'; } *q = NULL; - UNLOCK; - return (&net); + rv = &net; +DONE: + __UCLIBC_MUTEX_UNLOCK(mylock); + return rv; } libc_hidden_def(getnetent) -- cgit v1.2.3