Fix the problems I (and others, undoubtedly) have been having for a

while with threaded software in -CURRENT acting very "weird".  It has
seemed, for example, in Mozilla that threads attempting to do host
lookups have been locking up.  That's exactly the case.

There was a race condition in the implementation of the initialization
of the mutex used to protect FILE operations, first of all: multiple
instances of FLOCKFILE() in libc could occur on the same FILE at
the same time and cause strange behavior by overwriting eachothers'
creation of the mutex and the rest of the file lock.

Secondly, it's not appropriate to test the "validity" of the file
descriptor referenced by the FILE; if the code is calling FLOCKFILE()
or FUNLOCKFILE(), it wants the FILE to be locked or unlocked, not
to be locked or unlocked on the condition that _file is >= 0.  This
also could quite easily cause leaks by failing to perform the lock or
unlock operation when it actually is needed.

Mozilla now works again on -CURRENT when linked to libc_r.so.5 and
libc.so.5.
This commit is contained in:
Brian Feldman 2001-02-23 04:59:12 +00:00
parent 4fd2864862
commit 4524204190
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=72937

View File

@ -83,6 +83,7 @@ struct __file_lock {
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 @@ init_lock(FILE *fp)
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 @@ _ftrylockfile(FILE *fp)
* 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 @@ _funlockfile(FILE *fp)
* 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