Index: include/libc_private.h =================================================================== RCS file: /usr2/ncvs/src/lib/libc/include/libc_private.h,v retrieving revision 1.4 diff -u -r1.4 libc_private.h --- include/libc_private.h 2001/01/24 13:00:08 1.4 +++ include/libc_private.h 2001/02/23 16:06:00 @@ -51,9 +51,9 @@ * records the source file and line number of each lock call. */ #ifdef _FLOCK_DEBUG -#define _FLOCKFILE(x) _flockfile_debug(x, __FILE__, __LINE__) +#define _FLOCKFILE(x) flockfile_debug(x, __FILE__, __LINE__) #else -#define _FLOCKFILE(x) _flockfile(x) +#define _FLOCKFILE(x) flockfile(x) #endif /* @@ -61,6 +61,15 @@ * process is threaded to avoid locking when not required. */ #define FLOCKFILE(fp) if (__isthreaded) _FLOCKFILE(fp) -#define FUNLOCKFILE(fp) if (__isthreaded) _funlockfile(fp) +#define FUNLOCKFILE(fp) if (__isthreaded) funlockfile(fp) +#define FINITLOCKFILE(fp) (__isthreaded ? finitlockfile(fp) : 0) + +struct __sFILE; + +void flockfile(struct __sFILE *); +void flockfile_debug(struct __sFILE *); +int ftrylockfile(struct __sFILE *); +void funlockfile(struct __sFILE *); +int finitlockfile(struct __sFILE *); #endif /* _LIBC_PRIVATE_H_ */ Index: stdio/_flock_stub.c =================================================================== RCS file: /usr2/ncvs/src/lib/libc/stdio/_flock_stub.c,v retrieving revision 1.9 diff -u -r1.9 _flock_stub.c --- stdio/_flock_stub.c 2001/02/23 17:55:01 1.9 +++ stdio/_flock_stub.c 2001/02/23 18:04:06 @@ -54,9 +54,8 @@ #pragma weak _flockfile_debug=_flockfile_debug_stub #pragma weak ftrylockfile=_ftrylockfile #pragma weak funlockfile=_funlockfile +#pragma weak finitlockfile=_finitlockfile -static int init_lock(FILE *); - /* * The FILE lock structure. The FILE *fp is locked when the mutex * is locked. @@ -80,30 +79,21 @@ /* * Allocate and initialize a file lock. */ -static int -init_lock(FILE *fp) +int +_finitlockfile(FILE *fp) { - static pthread_mutex_t init_lock_mutex = PTHREAD_MUTEX_INITIALIZER; struct __file_lock *p; int ret; - if ((p = malloc(sizeof(struct __file_lock))) == NULL) + if (fp->_lock != NULL) + ret = 0; + else if ((p = malloc(sizeof(struct __file_lock))) == NULL) ret = -1; else { p->fl_mutex = PTHREAD_MUTEX_INITIALIZER; p->fl_owner = NULL; p->fl_count = 0; - if (_pthread_mutex_lock(&init_lock_mutex) != 0) { - free(p); - return (-1); - } - if (fp->_lock != NULL) { /* lost the race */ - free(p); - _pthread_mutex_unlock(&init_lock_mutex); - return (0); - } fp->_lock = p; - _pthread_mutex_unlock(&init_lock_mutex); ret = 0; } return (ret); @@ -115,11 +105,9 @@ pthread_t curthread = _pthread_self(); /* - * Check if this is a real file with a valid lock, creating - * the lock if needed: + * Check if this is a real file with a valid lock. */ - if ((fp->_file >= 0) && - ((fp->_lock != NULL) || (init_lock(fp) == 0))) { + if (fp->_lock != NULL) { if (fp->_lock->fl_owner == curthread) fp->_lock->fl_count++; else { @@ -150,10 +138,9 @@ int ret = 0; /* - * Check if this is a real file with a valid lock, creating - * the lock if needed: + * Check if this is a real file with a valid lock. */ - if (((fp->_lock != NULL) || (init_lock(fp) == 0))) { + if (fp->_lock != NULL) { if (fp->_lock->fl_owner == curthread) fp->_lock->fl_count++; /* Index: stdio/findfp.c =================================================================== RCS file: /usr2/ncvs/src/lib/libc/stdio/findfp.c,v retrieving revision 1.16 diff -u -r1.16 findfp.c --- stdio/findfp.c 2001/02/20 01:56:52 1.16 +++ stdio/findfp.c 2001/02/23 16:12:49 @@ -71,11 +71,17 @@ static struct __sFILEX __sFX[3]; +#if defined(__i386__) || defined(__alpha__) FILE __sF[3] = { std(__SRD, STDIN_FILENO), std(__SWR, STDOUT_FILENO), std(__SWR|__SNBF, STDERR_FILENO) }; +#else +struct FILE __stdin = std(__SRD, STDIN_FILENO); +struct FILE __stdout = std(__SWR, STDOUT_FILENO); +struct FILE __stderr = std(__SWR|__SNBF, STDERR_FILENO); +#endif /* * The following kludge is done to ensure enough binary compatibility @@ -183,7 +189,14 @@ fp->_ub._size = 0; fp->_lb._base = NULL; /* no line buffer */ fp->_lb._size = 0; -/* fp->_lock = NULL; */ /* once set always set (reused) */ + /* + * Only init the lock once; don't bother ever freeing it. If we + * fail the lock allocation, just fail the entire allocation. + */ + if (FINITLOCKFILE(fp) != 0) { + fp->_flags = 0; + return (NULL); + } return (fp); }