Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/stdio Revert the conversion to libpthread's recursi...



details:   https://anonhg.NetBSD.org/src/rev/1d0f880eee71
branches:  trunk
changeset: 542532:1d0f880eee71
user:      nathanw <nathanw%NetBSD.org@localhost>
date:      Sat Feb 01 03:25:00 2003 +0000

description:
Revert the conversion to libpthread's recursive mutexes. Too much
trouble is caused by the memory allocation in the mutex initialization,
and uncontested mutexes and condition variables have become faster in the
meantime.

diffstat:

 lib/libc/stdio/fileext.h   |  13 +++++++++-
 lib/libc/stdio/findfp.c    |  13 ++++++++---
 lib/libc/stdio/flockfile.c |  51 ++++++++++++++++++++++++++++++++-------------
 lib/libc/stdio/local.h     |   3 +-
 4 files changed, 57 insertions(+), 23 deletions(-)

diffs (190 lines):

diff -r d7b06738e72e -r 1d0f880eee71 lib/libc/stdio/fileext.h
--- a/lib/libc/stdio/fileext.h  Sat Feb 01 00:57:31 2003 +0000
+++ b/lib/libc/stdio/fileext.h  Sat Feb 01 03:25:00 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fileext.h,v 1.3 2003/01/21 23:26:02 nathanw Exp $ */
+/* $NetBSD: fileext.h,v 1.4 2003/02/01 03:25:00 nathanw Exp $ */
 
 /*-
  * Copyright (c)2001 Citrus Project,
@@ -36,6 +36,9 @@
        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 
 };
 
@@ -43,9 +46,15 @@
 #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); \
-       __smutex_init(&_LOCK(f)); \
+       mutex_init(&_LOCK(f), NULL); \
+       cond_init(&_LOCKCOND(f), 0, NULL); \
+       _LOCKOWNER(f) = NULL; \
+       _LOCKCOUNT(f) = 0; \
        } while (/* LINTED */ 0)
 #else
 #define _FILEEXT_SETUP(f, fext) /* LINTED */(f)->_ext._base = (unsigned char *)(fext)
diff -r d7b06738e72e -r 1d0f880eee71 lib/libc/stdio/findfp.c
--- a/lib/libc/stdio/findfp.c   Sat Feb 01 00:57:31 2003 +0000
+++ b/lib/libc/stdio/findfp.c   Sat Feb 01 03:25:00 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: findfp.c,v 1.17 2003/01/21 23:26:02 nathanw Exp $      */
+/*     $NetBSD: findfp.c,v 1.18 2003/02/01 03:25:00 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.17 2003/01/21 23:26:02 nathanw Exp $");
+__RCSID("$NetBSD: findfp.c,v 1.18 2003/02/01 03:25:00 nathanw Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -73,7 +73,14 @@
 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 */
@@ -197,8 +204,6 @@
 {
        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]);
 
diff -r d7b06738e72e -r 1d0f880eee71 lib/libc/stdio/flockfile.c
--- a/lib/libc/stdio/flockfile.c        Sat Feb 01 00:57:31 2003 +0000
+++ b/lib/libc/stdio/flockfile.c        Sat Feb 01 03:25:00 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: flockfile.c,v 1.3 2003/01/21 23:26:03 nathanw Exp $    */
+/*     $NetBSD: flockfile.c,v 1.4 2003/02/01 03:25:00 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.3 2003/01/21 23:26:03 nathanw Exp $");
+__RCSID("$NetBSD: flockfile.c,v 1.4 2003/02/01 03:25:00 nathanw Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -61,18 +61,6 @@
  * 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;
 
@@ -84,16 +72,41 @@
                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;
 
-       return mutex_trylock(&_LOCK(fp));
+       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;
 }
 
 void
@@ -103,6 +116,14 @@
        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));
 }
 
diff -r d7b06738e72e -r 1d0f880eee71 lib/libc/stdio/local.h
--- a/lib/libc/stdio/local.h    Sat Feb 01 00:57:31 2003 +0000
+++ b/lib/libc/stdio/local.h    Sat Feb 01 03:25:00 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: local.h,v 1.13 2003/01/21 23:26:03 nathanw Exp $       */
+/*     $NetBSD: local.h,v 1.14 2003/02/01 03:25:00 nathanw Exp $       */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -73,7 +73,6 @@
 
 extern int     __gettemp __P((char *, int *, int));
 
-extern void    __smutex_init __P((mutex_t *));
 /*
  * Return true iff the given FILE cannot be written now.
  */



Home | Main Index | Thread Index | Old Index