Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/net make this thread-safe.



details:   https://anonhg.NetBSD.org/src/rev/8893abb0300d
branches:  trunk
changeset: 566894:8893abb0300d
user:      christos <christos%NetBSD.org@localhost>
date:      Sun May 23 16:53:22 2004 +0000

description:
make this thread-safe.

diffstat:

 lib/libc/net/nsdispatch.c |  32 +++++++++++++++++++++++---------
 1 files changed, 23 insertions(+), 9 deletions(-)

diffs (89 lines):

diff -r b7f33e519d4a -r 8893abb0300d lib/libc/net/nsdispatch.c
--- a/lib/libc/net/nsdispatch.c Sun May 23 16:10:25 2004 +0000
+++ b/lib/libc/net/nsdispatch.c Sun May 23 16:53:22 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nsdispatch.c,v 1.18 2002/05/26 14:48:19 wiz Exp $      */
+/*     $NetBSD: nsdispatch.c,v 1.19 2004/05/23 16:53:22 christos Exp $ */
 
 /*-
  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: nsdispatch.c,v 1.18 2002/05/26 14:48:19 wiz Exp $");
+__RCSID("$NetBSD: nsdispatch.c,v 1.19 2004/05/23 16:53:22 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -57,6 +57,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <threadlib.h>
 
 extern FILE    *_nsyyin;
 extern int      _nsyyparse __P((void));
@@ -78,6 +79,15 @@
 
 static int                      _nsmapsize = 0;
 static ns_dbt                  *_nsmap = NULL;
+#ifdef _REENTRANT
+static         mutex_t                  _nsmutex = MUTEX_INITIALIZER;
+#define NSLOCK()               mutex_lock(&_nsmutex)
+#define NSUNLOCK()             mutex_unlock(&_nsmutex)
+#else
+#define NSLOCK()
+#define NSUNLOCK()
+#endif
+
 
 /*
  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
@@ -165,9 +175,10 @@
 
        dbt.name = name;
 
+       NSLOCK();
+       if (stat(_PATH_NS_CONF, &statbuf) == -1)
+               return (NULL);
        if (confmod) {
-               if (stat(_PATH_NS_CONF, &statbuf) == -1)
-                       return (NULL);
                if (confmod < statbuf.st_mtime) {
                        int i, j;
 
@@ -194,16 +205,17 @@
                }
        }
        if (!confmod) {
-               if (stat(_PATH_NS_CONF, &statbuf) == -1)
+               _nsyyin = fopen(_PATH_NS_CONF, "r");
+               if (_nsyyin == NULL) {
+                       NSUNLOCK();
                        return (NULL);
-               _nsyyin = fopen(_PATH_NS_CONF, "r");
-               if (_nsyyin == NULL)
-                       return (NULL);
+               }
                _nsyyparse();
                (void)fclose(_nsyyin);
                qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
                confmod = statbuf.st_mtime;
        }
+       NSUNLOCK();
        return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
            _nscmp));
 }
@@ -232,8 +244,10 @@
 
                new = (ns_dbt *)realloc(_nsmap,
                    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
-               if (new == NULL)
+               if (new == NULL) {
+                       NSUNLOCK();
                        return (-1);
+               }
                _nsmap = new;
        }
        memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));



Home | Main Index | Thread Index | Old Index