summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/findfp.c
diff options
context:
space:
mode:
authordt <dt@FreeBSD.org>1999-11-20 14:01:48 +0000
committerdt <dt@FreeBSD.org>1999-11-20 14:01:48 +0000
commit669c69aa24a5ba814eb863e42fda2b128040f429 (patch)
treed92a6676956fe2b898fa25a8d7262ce97931bc3a /lib/libc/stdio/findfp.c
parent4d591d1e002faa6351bb6f7b2cfc37ac772dac1e (diff)
downloadFreeBSD-src-669c69aa24a5ba814eb863e42fda2b128040f429.zip
FreeBSD-src-669c69aa24a5ba814eb863e42fda2b128040f429.tar.gz
Make __sfp() (FILE allocator) thread-safe: added locking like in malloc().
Diffstat (limited to 'lib/libc/stdio/findfp.c')
-rw-r--r--lib/libc/stdio/findfp.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/libc/stdio/findfp.c b/lib/libc/stdio/findfp.c
index f0b37cf..3cc08a9 100644
--- a/lib/libc/stdio/findfp.c
+++ b/lib/libc/stdio/findfp.c
@@ -48,6 +48,10 @@ static const char rcsid[] =
#include <errno.h>
#include <stdlib.h>
#include <string.h>
+
+#include <libc_private.h>
+#include <spinlock.h>
+
#include "local.h"
#include "glue.h"
@@ -72,6 +76,10 @@ struct glue __sglue = { &uglue, 3, __sF };
static struct glue * moreglue __P((int));
+static spinlock_t thread_lock = _SPINLOCK_INITIALIZER;
+#define THREAD_LOCK() if (__isthreaded) _SPINLOCK(&thread_lock)
+#define THREAD_UNLOCK() if (__isthreaded) _SPINUNLOCK(&thread_lock)
+
static struct glue *
moreglue(n)
register int n;
@@ -104,6 +112,7 @@ __sfp()
if (!__sdidinit)
__sinit();
+ THREAD_LOCK();
for (g = &__sglue;; g = g->next) {
for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
if (fp->_flags == 0)
@@ -111,9 +120,11 @@ __sfp()
if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
break;
}
+ THREAD_UNLOCK();
return (NULL);
found:
fp->_flags = 1; /* reserve this slot; caller sets real flags */
+ THREAD_UNLOCK();
fp->_p = NULL; /* no current pointer */
fp->_w = 0; /* nothing to read or write */
fp->_r = 0;
OpenPOWER on IntegriCloud