Index: stdio/_flock_stub.c =================================================================== RCS file: /usr2/ncvs/src/lib/libc/stdio/_flock_stub.c,v retrieving revision 1.6 diff -u -r1.6 _flock_stub.c --- stdio/_flock_stub.c 2001/02/16 06:11:22 1.6 +++ stdio/_flock_stub.c 2001/02/23 04:42:29 @@ -83,6 +83,7 @@ static int init_lock(FILE *fp) { + static pthread_mutex_t init_lock_mutex = PTHREAD_MUTEX_INITIALIZER; struct __file_lock *p; int ret; @@ -92,7 +93,16 @@ 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); + return (0); + } fp->_lock = p; + pthread_mutex_unlock(&init_lock_mutex); ret = 0; } return (ret); @@ -142,8 +152,7 @@ * Check if this is a real file with a valid lock, creating * the lock if needed: */ - if ((fp->_file >= 0) && - ((fp->_lock != NULL) || (init_lock(fp) == 0))) { + if (((fp->_lock != NULL) || (init_lock(fp) == 0))) { if (fp->_lock->fl_owner == curthread) fp->_lock->fl_count++; /* @@ -171,7 +180,7 @@ * Check if this is a real file with a valid lock owned * by the current thread: */ - if ((fp->_file >= 0) && (fp->_lock != NULL) && + if ((fp->_lock != NULL) && (fp->_lock->fl_owner == curthread)) { /* * Check if this thread has locked the FILE