summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/_flock_stub.c
diff options
context:
space:
mode:
authordeischen <deischen@FreeBSD.org>2001-03-01 05:22:14 +0000
committerdeischen <deischen@FreeBSD.org>2001-03-01 05:22:14 +0000
commit2d1163e370d4fb97297838d1fe7872a653dce6bb (patch)
tree56a1f702941fc746f602720d30a3421e37510f80 /lib/libc/stdio/_flock_stub.c
parent242ddf3c95fd6a4a22fdc278c485bbbcffe01ae8 (diff)
downloadFreeBSD-src-2d1163e370d4fb97297838d1fe7872a653dce6bb.zip
FreeBSD-src-2d1163e370d4fb97297838d1fe7872a653dce6bb.tar.gz
Hide the definition of struct __sFILEX and add the needed
lock definitions to it. flockfile state is now allocated along with the rest of FILE. This eliminates the need for a separate allocation of flockfile state as well as eliminating the mutex/lock used to serialize its allocation.
Diffstat (limited to 'lib/libc/stdio/_flock_stub.c')
-rw-r--r--lib/libc/stdio/_flock_stub.c104
1 files changed, 23 insertions, 81 deletions
diff --git a/lib/libc/stdio/_flock_stub.c b/lib/libc/stdio/_flock_stub.c
index 7d17f9a..cc0cffe 100644
--- a/lib/libc/stdio/_flock_stub.c
+++ b/lib/libc/stdio/_flock_stub.c
@@ -47,6 +47,9 @@
#include <pthread.h>
#include "un-namespace.h"
+#include "local.h"
+
+
/*
* Weak symbols for externally visible functions in this file:
*/
@@ -55,18 +58,6 @@
#pragma weak ftrylockfile=_ftrylockfile
#pragma weak funlockfile=_funlockfile
-static int init_lock(FILE *);
-
-/*
- * The FILE lock structure. The FILE *fp is locked when the mutex
- * is locked.
- */
-struct __file_lock {
- pthread_mutex_t fl_mutex;
- pthread_t fl_owner; /* current owner */
- int fl_count; /* recursive lock count */
-};
-
/*
* We need to retain binary compatibility for a while. So pretend
* that _lock is part of FILE * even though it is dereferenced off
@@ -75,62 +66,23 @@ struct __file_lock {
* code that has to change in the future (just remove this comment
* and #define).
*/
-#define _lock _extra->_mtlock
-
-/*
- * Allocate and initialize a file lock.
- */
-static int
-init_lock(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)
- 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);
-}
+#define _lock _extra
void
_flockfile(FILE *fp)
{
pthread_t curthread = _pthread_self();
- /*
- * 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->fl_owner == curthread)
- fp->_lock->fl_count++;
- else {
- /*
- * Make sure this mutex is treated as a private
- * internal mutex:
- */
- _pthread_mutex_lock(&fp->_lock->fl_mutex);
- fp->_lock->fl_owner = curthread;
- fp->_lock->fl_count = 1;
- }
+ if (fp->_lock->fl_owner == curthread)
+ fp->_lock->fl_count++;
+ else {
+ /*
+ * Make sure this mutex is treated as a private
+ * internal mutex:
+ */
+ _pthread_mutex_lock(&fp->_lock->fl_mutex);
+ fp->_lock->fl_owner = curthread;
+ fp->_lock->fl_count = 1;
}
}
@@ -149,23 +101,15 @@ _ftrylockfile(FILE *fp)
pthread_t curthread = _pthread_self();
int ret = 0;
+ if (fp->_lock->fl_owner == curthread)
+ fp->_lock->fl_count++;
/*
- * Check if this is a real file with a valid lock, creating
- * the lock if needed:
+ * Make sure this mutex is treated as a private
+ * internal mutex:
*/
- if (((fp->_lock != NULL) || (init_lock(fp) == 0))) {
- if (fp->_lock->fl_owner == curthread)
- fp->_lock->fl_count++;
- /*
- * Make sure this mutex is treated as a private
- * internal mutex:
- */
- else if (_pthread_mutex_trylock(&fp->_lock->fl_mutex) == 0) {
- fp->_lock->fl_owner = curthread;
- fp->_lock->fl_count = 1;
- }
- else
- ret = -1;
+ else if (_pthread_mutex_trylock(&fp->_lock->fl_mutex) == 0) {
+ fp->_lock->fl_owner = curthread;
+ fp->_lock->fl_count = 1;
}
else
ret = -1;
@@ -178,11 +122,9 @@ _funlockfile(FILE *fp)
pthread_t curthread = _pthread_self();
/*
- * Check if this is a real file with a valid lock owned
- * by the current thread:
+ * Check if this file is owned by the current thread:
*/
- if ((fp->_lock != NULL) &&
- (fp->_lock->fl_owner == curthread)) {
+ if (fp->_lock->fl_owner == curthread) {
/*
* Check if this thread has locked the FILE
* more than once:
OpenPOWER on IntegriCloud