Use recursive mutexes from libpthread rather than implementing

our own with normal mutexes and condition variables.
This commit is contained in:
nathanw 2003-01-21 23:26:02 +00:00
parent 831ba492c3
commit 6003a24dd4
4 changed files with 23 additions and 57 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: fileext.h,v 1.2 2003/01/18 11:29:51 thorpej Exp $ */
/* $NetBSD: fileext.h,v 1.3 2003/01/21 23:26:02 nathanw Exp $ */
/*-
* Copyright (c)2001 Citrus Project,
@ -36,9 +36,6 @@ struct __sfileext {
struct wchar_io_data _wcio; /* wide char i/o status */
#ifdef _REENTRANT
mutex_t _lock; /* Lock for FLOCKFILE/FUNLOCKFILE */
cond_t _lockcond; /* Condition variable for signalling lock releases */
thr_t _lockowner; /* The thread currently holding the lock */
int _lockcount; /* Count of recursive locks */
#endif
};
@ -46,15 +43,9 @@ struct __sfileext {
#define _UB(fp) _EXT(fp)->_ub
#ifdef _REENTRANT
#define _LOCK(fp) (_EXT(fp)->_lock)
#define _LOCKCOND(fp) (_EXT(fp)->_lockcond)
#define _LOCKOWNER(fp) (_EXT(fp)->_lockowner)
#define _LOCKCOUNT(fp) (_EXT(fp)->_lockcount)
#define _FILEEXT_SETUP(f, fext) do { \
/* LINTED */(f)->_ext._base = (unsigned char *)(fext); \
mutex_init(&_LOCK(f), NULL); \
cond_init(&_LOCKCOND(f), 0, NULL); \
_LOCKOWNER(f) = NULL; \
_LOCKCOUNT(f) = 0; \
__smutex_init(&_LOCK(f)); \
} while (/* LINTED */ 0)
#else
#define _FILEEXT_SETUP(f, fext) /* LINTED */(f)->_ext._base = (unsigned char *)(fext)

View File

@ -1,4 +1,4 @@
/* $NetBSD: findfp.c,v 1.16 2003/01/18 11:29:52 thorpej Exp $ */
/* $NetBSD: findfp.c,v 1.17 2003/01/21 23:26:02 nathanw Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
#else
__RCSID("$NetBSD: findfp.c,v 1.16 2003/01/18 11:29:52 thorpej Exp $");
__RCSID("$NetBSD: findfp.c,v 1.17 2003/01/21 23:26:02 nathanw Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -73,14 +73,7 @@ static FILE usual[FOPEN_MAX - 3];
static struct __sfileext usualext[FOPEN_MAX - 3];
static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
#ifdef _REENTRANT
#define STDEXT { {0}, {{{0}}}, MUTEX_INITIALIZER, COND_INITIALIZER, NULL, 0}
struct __sfileext __sFext[3] = { STDEXT,
STDEXT,
STDEXT};
#else
struct __sfileext __sFext[3];
#endif
FILE __sF[3] = {
std(__SRD, STDIN_FILENO), /* stdin */
@ -204,6 +197,8 @@ __sinit()
{
int i;
for (i = 0; i < 3; i ++)
__smutex_init(&__sFext[i]._lock);
for (i = 0; i < FOPEN_MAX - 3; i++)
_FILEEXT_SETUP(&usual[i], &usualext[i]);

View File

@ -1,4 +1,4 @@
/* $NetBSD: flockfile.c,v 1.2 2003/01/18 11:29:52 thorpej Exp $ */
/* $NetBSD: flockfile.c,v 1.3 2003/01/21 23:26:03 nathanw Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: flockfile.c,v 1.2 2003/01/18 11:29:52 thorpej Exp $");
__RCSID("$NetBSD: flockfile.c,v 1.3 2003/01/21 23:26:03 nathanw Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
@ -61,6 +61,18 @@ __weak_alias(funlockfile,_funlockfile)
* XXX This code makes the assumption that a thr_t (pthread_t) is a
* XXX pointer.
*/
void
__smutex_init(mutex_t *m)
{
mutexattr_t attr;
mutexattr_init(&attr);
mutexattr_settype(&attr, MUTEX_TYPE_RECURSIVE);
mutex_init(m, &attr);
mutexattr_destroy(&attr);
}
extern int __isthreaded;
@ -72,41 +84,16 @@ flockfile(FILE *fp)
return;
mutex_lock(&_LOCK(fp));
if (_LOCKOWNER(fp) == thr_self()) {
_LOCKCOUNT(fp)++;
} else {
while (_LOCKOWNER(fp) != NULL)
cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
_LOCKOWNER(fp) = thr_self();
_LOCKCOUNT(fp) = 1;
}
mutex_unlock(&_LOCK(fp));
}
int
ftrylockfile(FILE *fp)
{
int retval;
if (__isthreaded == 0)
return 0;
retval = 0;
mutex_lock(&_LOCK(fp));
if (_LOCKOWNER(fp) == thr_self()) {
_LOCKCOUNT(fp)++;
} else if (_LOCKOWNER(fp) == NULL) {
_LOCKOWNER(fp) = thr_self();
_LOCKCOUNT(fp) = 1;
} else
retval = -1;
mutex_unlock(&_LOCK(fp));
return retval;
return mutex_trylock(&_LOCK(fp));
}
void
@ -116,14 +103,6 @@ funlockfile(FILE *fp)
if (__isthreaded == 0)
return;
mutex_lock(&_LOCK(fp));
_LOCKCOUNT(fp)--;
if (_LOCKCOUNT(fp) == 0) {
_LOCKOWNER(fp) = NULL;
cond_signal(&_LOCKCOND(fp));
}
mutex_unlock(&_LOCK(fp));
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: local.h,v 1.12 2003/01/18 11:29:55 thorpej Exp $ */
/* $NetBSD: local.h,v 1.13 2003/01/21 23:26:03 nathanw Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -73,6 +73,7 @@ extern int __sdidinit;
extern int __gettemp __P((char *, int *, int));
extern void __smutex_init __P((mutex_t *));
/*
* Return true iff the given FILE cannot be written now.
*/