summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorKurt Miller <kurt@cvs.openbsd.org>2007-06-05 18:11:50 +0000
committerKurt Miller <kurt@cvs.openbsd.org>2007-06-05 18:11:50 +0000
commit5ea227edb4b41ecee104b943846b3df89ceafb8c (patch)
tree5e3932033b9b169395a0474156a37318fa74db42 /lib/libc
parent5727900f0e332290d68166eab7d4e4d71eea2264 (diff)
_FD_LOCK/UNLOCK() is libpthread specific and not needed for librthread, so
isolate its usage to libpthread only and replace with generic non-static mutex support in the one place it is needed: - remove _FD_LOCK/UNLOCK from lseek and ftruncate in libc and make the functions weak so that libpthread can override with its own new versions that do the locking. - remove _thread_fd_lock/unlock() weak functions from libc and adjust libpthread for the change. - add generic _thread_mutex_lock/unlock/destroy() weak functions in libc to support non-static mutexes in libc and add libpthread and librthread implementations for them. libc can utilize non-static mutexes via the new _MUTEX_LOCK/UNLOCK/DESTROY() macros. Actually these new macros can support both static and non-static mutexes but currently only using them for non-static. - make opendir/closedir/readdir/readdir_r/seekdir/telldir() thread-safe for both thread libraries by using a non-static mutex in the struct _dirdesc (typedef DIR), utilizing it in the *dir functions and remove remaining and incorrect _FD_LOCK/UNLOCK() use in libc. - add comments to both thread libraries to indicate libc depends on the current implementation of static mutex initialization. suggested by marc@ - major bump libc and libpthread due to function removal, structure change and weak symbol conversions. okay marc@, tedu@
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/gen/closedir.c12
-rw-r--r--lib/libc/gen/opendir.c3
-rw-r--r--lib/libc/gen/readdir.c32
-rw-r--r--lib/libc/gen/seekdir.c6
-rw-r--r--lib/libc/gen/telldir.c13
-rw-r--r--lib/libc/include/thread_private.h56
-rw-r--r--lib/libc/shlib_version4
-rw-r--r--lib/libc/sys/ftruncate.c21
-rw-r--r--lib/libc/sys/lseek.c21
-rw-r--r--lib/libc/thread/Makefile.inc4
-rw-r--r--lib/libc/thread/thread_fd.c22
-rw-r--r--lib/libc/thread/unithread_mutex.c49
12 files changed, 146 insertions, 97 deletions
diff --git a/lib/libc/gen/closedir.c b/lib/libc/gen/closedir.c
index 7315bcc1e41..48d2775d7ea 100644
--- a/lib/libc/gen/closedir.c
+++ b/lib/libc/gen/closedir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: closedir.c,v 1.7 2006/04/01 18:06:59 otto Exp $ */
+/* $OpenBSD: closedir.c,v 1.8 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1983, 1993
* Regents of the University of California. All rights reserved.
@@ -42,17 +42,15 @@ int
closedir(DIR *dirp)
{
int fd;
- int ret;
- if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0)
- return (ret);
+ _MUTEX_LOCK(&dirp->dd_lock);
fd = dirp->dd_fd;
dirp->dd_fd = -1;
dirp->dd_loc = 0;
free(dirp->dd_td->td_locs);
free(dirp->dd_buf);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
+ _MUTEX_DESTROY(&dirp->dd_lock);
free(dirp);
- ret = close(fd);
- _FD_UNLOCK(fd, FD_READ);
- return (ret);
+ return (close(fd));
}
diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c
index 065c1aa9411..4028ba9c1eb 100644
--- a/lib/libc/gen/opendir.c
+++ b/lib/libc/gen/opendir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: opendir.c,v 1.18 2007/02/09 14:58:09 millert Exp $ */
+/* $OpenBSD: opendir.c,v 1.19 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
@@ -97,6 +97,7 @@ __opendir2(const char *name, int flags)
dirp->dd_loc = 0;
dirp->dd_fd = fd;
dirp->dd_flags = flags;
+ dirp->dd_lock = NULL;
/*
* Set up seek point for rewinddir.
diff --git a/lib/libc/gen/readdir.c b/lib/libc/gen/readdir.c
index b028c822396..5f4c447e576 100644
--- a/lib/libc/gen/readdir.c
+++ b/lib/libc/gen/readdir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: readdir.c,v 1.11 2007/02/08 16:34:39 millert Exp $ */
+/* $OpenBSD: readdir.c,v 1.12 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
@@ -38,7 +38,7 @@
* get next entry in a directory.
*/
struct dirent *
-readdir(DIR *dirp)
+_readdir_unlocked(DIR *dirp)
{
struct dirent *dp;
@@ -65,28 +65,34 @@ readdir(DIR *dirp)
}
}
+struct dirent *
+readdir(DIR *dirp)
+{
+ struct dirent *dp;
+
+ _MUTEX_LOCK(&dirp->dd_lock);
+ dp = _readdir_unlocked(dirp);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
+
+ return (dp);
+}
+
int
readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
{
struct dirent *dp;
- int ret;
- if (dirp->dd_fd < 0) {
- return EBADF;
- }
- if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0)
- return ret;
- errno = 0;
- dp = readdir(dirp);
+ _MUTEX_LOCK(&dirp->dd_lock);
+ dp = _readdir_unlocked(dirp);
if (dp == NULL && errno != 0) {
- _FD_UNLOCK(dirp->dd_fd, FD_READ);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
return errno;
}
if (dp != NULL)
memcpy(entry, dp, sizeof (struct dirent) - MAXNAMLEN + dp->d_namlen);
- _FD_UNLOCK(dirp->dd_fd, FD_READ);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
if (dp != NULL)
- *result = entry;
+ *result = entry;
else
*result = NULL;
return 0;
diff --git a/lib/libc/gen/seekdir.c b/lib/libc/gen/seekdir.c
index e472f62b789..2affbdd1ef1 100644
--- a/lib/libc/gen/seekdir.c
+++ b/lib/libc/gen/seekdir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: seekdir.c,v 1.8 2006/04/01 18:06:59 otto Exp $ */
+/* $OpenBSD: seekdir.c,v 1.9 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
@@ -30,6 +30,7 @@
#include <sys/param.h>
#include <dirent.h>
+#include "thread_private.h"
#include "telldir.h"
/*
@@ -39,6 +40,7 @@
void
seekdir(DIR *dirp, long loc)
{
-
+ _MUTEX_LOCK(&dirp->dd_lock);
__seekdir(dirp, loc);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
}
diff --git a/lib/libc/gen/telldir.c b/lib/libc/gen/telldir.c
index 708b923f3a0..1314397ce93 100644
--- a/lib/libc/gen/telldir.c
+++ b/lib/libc/gen/telldir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: telldir.c,v 1.9 2006/04/10 12:04:20 otto Exp $ */
+/* $OpenBSD: telldir.c,v 1.10 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
@@ -34,6 +34,7 @@
#include <stdlib.h>
#include <unistd.h>
+#include "thread_private.h"
#include "telldir.h"
/*
@@ -42,9 +43,11 @@
long
telldir(DIR *dirp)
{
- long i = dirp->dd_td->td_last;
+ long i;
struct ddloc *lp;
+ _MUTEX_LOCK(&dirp->dd_lock);
+ i = dirp->dd_td->td_last;
lp = &dirp->dd_td->td_locs[i];
/* return previous telldir, if there */
@@ -52,6 +55,7 @@ telldir(DIR *dirp)
if (lp->loc_seek == dirp->dd_seek &&
lp->loc_loc == dirp->dd_loc) {
dirp->dd_td->td_last = i;
+ _MUTEX_UNLOCK(&dirp->dd_lock);
return (i);
}
}
@@ -60,8 +64,10 @@ telldir(DIR *dirp)
size_t newsz = dirp->dd_td->td_sz * 2 + 1;
struct ddloc *p;
p = realloc(dirp->dd_td->td_locs, newsz * sizeof(*p));
- if (p == NULL)
+ if (p == NULL) {
+ _MUTEX_UNLOCK(&dirp->dd_lock);
return (-1);
+ }
dirp->dd_td->td_sz = newsz;
dirp->dd_td->td_locs = p;
lp = &dirp->dd_td->td_locs[i];
@@ -70,6 +76,7 @@ telldir(DIR *dirp)
lp->loc_seek = dirp->dd_seek;
lp->loc_loc = dirp->dd_loc;
dirp->dd_td->td_last = i;
+ _MUTEX_UNLOCK(&dirp->dd_lock);
return (i);
}
diff --git a/lib/libc/include/thread_private.h b/lib/libc/include/thread_private.h
index c5acc4b5e43..a365c25e66a 100644
--- a/lib/libc/include/thread_private.h
+++ b/lib/libc/include/thread_private.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: thread_private.h,v 1.19 2006/09/26 14:18:28 kurt Exp $ */
+/* $OpenBSD: thread_private.h,v 1.20 2007/06/05 18:11:48 kurt Exp $ */
/* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
@@ -54,10 +54,24 @@ extern int __isthreaded;
* return a pointer to per thread instance of data associated
* with the given tag. If the given tag is NULL a tag is first
* allocated.
+ *
+ * _thread_mutex_lock:
+ * lock the given mutex. If the given mutex is NULL,
+ * rely on rthreads/pthreads implementation to initialize
+ * the mutex before locking.
+ *
+ * _thread_mutex_unlock:
+ * unlock the given mutex.
+ *
+ * _thread_mutex_destroy:
+ * destroy the given mutex.
*/
void _thread_tag_lock(void **);
void _thread_tag_unlock(void **);
void *_thread_tag_storage(void **, void *, size_t, void *);
+void _thread_mutex_lock(void **);
+void _thread_mutex_unlock(void **);
+void _thread_mutex_destroy(void **);
/*
* Macros used in libc to access thread mutex, keys, and per thread storage.
@@ -77,32 +91,32 @@ void *_thread_tag_storage(void **, void *, size_t, void *);
#define _THREAD_PRIVATE(keyname, storage, error) \
_thread_tag_storage(&(__THREAD_NAME(keyname)), &(storage), \
sizeof (storage), error)
-/*
- * Resolver code is special cased in that it uses global keys.
- */
-extern void *__THREAD_NAME(_res);
-extern void *__THREAD_NAME(_res_ext);
-extern void *__THREAD_NAME(serv_mutex);
/*
- * File descriptor locking definitions.
+ * Macros used in libc to access non-static mutexes.
*/
-#define FD_READ 0x1
-#define FD_WRITE 0x2
-#define FD_RDWR (FD_READ | FD_WRITE)
-#define FD_RDWR_CLOSE (FD_RDWR | 0x4)
-
-struct timespec;
-int _thread_fd_lock(int, int, struct timespec *);
-void _thread_fd_unlock(int, int);
+#define _MUTEX_LOCK(mutex) \
+ do { \
+ if (__isthreaded) \
+ _thread_mutex_lock(mutex); \
+ } while (0)
+#define _MUTEX_UNLOCK(mutex) \
+ do { \
+ if (__isthreaded) \
+ _thread_mutex_unlock(mutex); \
+ } while (0)
+#define _MUTEX_DESTROY(mutex) \
+ do { \
+ if (__isthreaded) \
+ _thread_mutex_destroy(mutex); \
+ } while (0)
/*
- * Macros are used in libc code for historical (debug) reasons.
- * Define them here.
+ * Resolver code is special cased in that it uses global keys.
*/
-#define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts)
-#define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type)
-
+extern void *__THREAD_NAME(_res);
+extern void *__THREAD_NAME(_res_ext);
+extern void *__THREAD_NAME(serv_mutex);
/*
* malloc lock/unlock prototypes and definitions
diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version
index acb4851773e..2041edd3fce 100644
--- a/lib/libc/shlib_version
+++ b/lib/libc/shlib_version
@@ -1,4 +1,4 @@
-major=40
-minor=3
+major=41
+minor=0
# note: If changes were made to include/thread_private.h or if system
# calls were added/changed then libpthread must also be updated.
diff --git a/lib/libc/sys/ftruncate.c b/lib/libc/sys/ftruncate.c
index c1722580859..d0f5e9837c4 100644
--- a/lib/libc/sys/ftruncate.c
+++ b/lib/libc/sys/ftruncate.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ftruncate.c,v 1.14 2005/08/08 08:05:37 espie Exp $ */
+/* $OpenBSD: ftruncate.c,v 1.15 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
@@ -28,27 +28,24 @@
* SUCH DAMAGE.
*/
-#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "thread_private.h"
register_t __syscall(quad_t, ...);
+/* ftruncate is weak to support libpthread locking */
+
+WEAK_PROTOTYPE(ftruncate);
+
+WEAK_ALIAS(ftruncate);
+
/*
* This function provides 64-bit offset padding that
* is not supplied by GCC 1.X but is supplied by GCC 2.X.
*/
int
-ftruncate(int fd, off_t length)
+WEAK_NAME(ftruncate)(int fd, off_t length)
{
- int retval;
-
- if (_FD_LOCK(fd, FD_RDWR, NULL) != 0) {
- retval = -1;
- } else {
- retval = __syscall((quad_t)SYS_ftruncate, fd, 0, length);
- _FD_UNLOCK(fd, FD_RDWR);
- }
- return retval;
+ return (__syscall((quad_t)SYS_ftruncate, fd, 0, length));
}
diff --git a/lib/libc/sys/lseek.c b/lib/libc/sys/lseek.c
index f9e9ef98aa3..1abb688a348 100644
--- a/lib/libc/sys/lseek.c
+++ b/lib/libc/sys/lseek.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: lseek.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
+/* $OpenBSD: lseek.c,v 1.14 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
@@ -28,27 +28,24 @@
* SUCH DAMAGE.
*/
-#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "thread_private.h"
off_t __syscall(quad_t, ...);
+/* lseek is weak to support libpthread locking */
+
+WEAK_PROTOTYPE(lseek);
+
+WEAK_ALIAS(lseek);
+
/*
* This function provides 64-bit offset padding that
* is not supplied by GCC 1.X but is supplied by GCC 2.X.
*/
off_t
-lseek(int fd, off_t offset, int whence)
+WEAK_NAME(lseek)(int fd, off_t offset, int whence)
{
- off_t retval;
-
- if (_FD_LOCK(fd, FD_RDWR, NULL) != 0) {
- retval = -1;
- } else {
- retval = __syscall((quad_t)SYS_lseek, fd, 0, offset, whence);
- _FD_UNLOCK(fd, FD_RDWR);
- }
- return retval;
+ return (__syscall((quad_t)SYS_lseek, fd, 0, offset, whence));
}
diff --git a/lib/libc/thread/Makefile.inc b/lib/libc/thread/Makefile.inc
index 9d323879900..6d4c1cacdf6 100644
--- a/lib/libc/thread/Makefile.inc
+++ b/lib/libc/thread/Makefile.inc
@@ -1,6 +1,6 @@
-# $OpenBSD: Makefile.inc,v 1.6 2004/06/07 21:11:23 marc Exp $
+# $OpenBSD: Makefile.inc,v 1.7 2007/06/05 18:11:48 kurt Exp $
.PATH: ${LIBCSRCDIR}/thread
-SRCS+= thread_fd.c unithread_malloc_lock.c unithread_tag.c
+SRCS+= unithread_malloc_lock.c unithread_mutex.c unithread_tag.c
diff --git a/lib/libc/thread/thread_fd.c b/lib/libc/thread/thread_fd.c
index 9f10fedf837..e69de29bb2d 100644
--- a/lib/libc/thread/thread_fd.c
+++ b/lib/libc/thread/thread_fd.c
@@ -1,22 +0,0 @@
-/* $OpenBSD: thread_fd.c,v 1.8 2004/06/07 21:11:23 marc Exp $ */
-
-#include <sys/time.h>
-#include "thread_private.h"
-
-WEAK_PROTOTYPE(_thread_fd_lock);
-WEAK_PROTOTYPE(_thread_fd_unlock);
-
-WEAK_ALIAS(_thread_fd_lock);
-WEAK_ALIAS(_thread_fd_unlock);
-
-int
-WEAK_NAME(_thread_fd_lock)(int fd, int lock_type, struct timespec *timeout)
-{
- return 0;
-}
-
-void
-WEAK_NAME(_thread_fd_unlock)(int fd, int lock_type)
-{
-}
-
diff --git a/lib/libc/thread/unithread_mutex.c b/lib/libc/thread/unithread_mutex.c
new file mode 100644
index 00000000000..44009de7c01
--- /dev/null
+++ b/lib/libc/thread/unithread_mutex.c
@@ -0,0 +1,49 @@
+/* $OpenBSD: unithread_mutex.c,v 1.1 2007/06/05 18:11:48 kurt Exp $ */
+
+/*
+ * Copyright (c) 2007 Kurt Miller <kurt@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include "thread_private.h"
+
+WEAK_PROTOTYPE(_thread_mutex_lock);
+WEAK_PROTOTYPE(_thread_mutex_unlock);
+WEAK_PROTOTYPE(_thread_mutex_destroy);
+
+WEAK_ALIAS(_thread_mutex_lock);
+WEAK_ALIAS(_thread_mutex_unlock);
+WEAK_ALIAS(_thread_mutex_destroy);
+
+/* ARGSUSED */
+void
+WEAK_NAME(_thread_mutex_lock)(void **mutex)
+{
+ return;
+}
+
+/* ARGSUSED */
+void
+WEAK_NAME(_thread_mutex_unlock)(void **mutex)
+{
+ return;
+}
+
+/* ARGSUSED */
+void
+WEAK_NAME(_thread_mutex_destroy)(void **mutex)
+{
+ return;
+}