diff options
| author | nathanw <nathanw@NetBSD.org> | 2004-12-10 16:40:40 +0000 |
|---|---|---|
| committer | nathanw <nathanw@NetBSD.org> | 2004-12-10 16:40:40 +0000 |
| commit | 0eaa8971b75ca19285a4ebb37816f150314d0bb1 (patch) | |
| tree | 19a82dc994a186cfd100ec3dcc2e183586182a9d /lib/libpthread | |
| parent | 8d1f13638c6ae51ded4266beab792aa7a07c79be (diff) | |
Use CPP macros to cause many libpthread functions used by applications
to be transformed into the do-nothing-when-libpthread-isn't-linked libc
stub names. This will permit library code that uses <pthread.h> and pthread
functions "defensively" to not need to link against libpthread and not need
to be patched to the threadlib.h API.
Diffstat (limited to 'lib/libpthread')
| -rw-r--r-- | lib/libpthread/Makefile | 3 | ||||
| -rw-r--r-- | lib/libpthread/pthread.h | 111 |
2 files changed, 112 insertions, 2 deletions
diff --git a/lib/libpthread/Makefile b/lib/libpthread/Makefile index 4f06370098e..93dd37e30ba 100644 --- a/lib/libpthread/Makefile +++ b/lib/libpthread/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.27 2004/06/02 14:07:07 pooka Exp $ +# $NetBSD: Makefile,v 1.28 2004/12/10 16:40:40 nathanw Exp $ # WARNS= 2 @@ -26,6 +26,7 @@ ARCHDIR= ${.CURDIR}/arch/${ARCHSUBDIR} .PATH: ${ARCHDIR} CPPFLAGS+= -I${ARCHDIR} -I${.CURDIR} -I${.OBJDIR} -D_LIBC +CPPFLAGS+= -D__LIBPTHREAD_SOURCE__ DPSRCS+= assym.h CLEANFILES+= assym.h diff --git a/lib/libpthread/pthread.h b/lib/libpthread/pthread.h index e51593e2440..7e0bc2cd9ea 100644 --- a/lib/libpthread/pthread.h +++ b/lib/libpthread/pthread.h @@ -1,4 +1,4 @@ -/* $NetBSD: pthread.h,v 1.16 2003/12/07 20:29:07 christos Exp $ */ +/* $NetBSD: pthread.h,v 1.17 2004/12/10 16:40:40 nathanw Exp $ */ /*- * Copyright (c) 2001 The NetBSD Foundation, Inc. @@ -220,4 +220,113 @@ __END_DECLS #define PTHREAD_RWLOCK_INITIALIZER _PTHREAD_RWLOCK_INITIALIZER #define PTHREAD_SPINLOCK_INITIALIZER _PTHREAD_SPINLOCK_INITIALIZER +/* + * Use macros to rename many pthread functions to the corresponding + * libc symbols which are either trivial/no-op stubs or the real + * thing, depending on whether libpthread is linked in to the + * program. This permits code, particularly libraries that do not + * directly use threads but want to be thread-safe in the presence of + * threaded callers, to use pthread mutexes and the like without + * unnecessairly including libpthread in their linkage. + * + * Left out of this list are functions that can't sensibly be trivial + * or no-op stubs in a single-threaded process (pthread_create, + * pthread_kill, pthread_detach), functions that normally block and + * wait for another thread to do something (pthread_join, + * pthread_cond_wait, pthread_cond_timedwait), and functions that + * don't make sense without the previous functions (pthread_attr_*). + * + * The rename is done as: + * #define pthread_foo __libc_foo + * instead of + * #define pthread_foo(x) __libc_foo((x)) + * in order that taking the address of the function ("func = + * &pthread_foo;") continue to work. + * + * POSIX/SUSv3 requires that its functions exist as functions (even if + * macro versions exist) and specifically that "#undef pthread_foo" is + * legal and should not break anything. Code that does such will not + * successfully get the stub behavior implemented here and will + * require libpthread to be linked in. + */ + +#ifndef __LIBPTHREAD_SOURCE__ +__BEGIN_DECLS +int __libc_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +int __libc_mutex_lock(pthread_mutex_t *); +int __libc_mutex_trylock(pthread_mutex_t *); +int __libc_mutex_unlock(pthread_mutex_t *); +int __libc_mutex_destroy(pthread_mutex_t *); + +int __libc_mutexattr_init(pthread_mutexattr_t *); +int __libc_mutexattr_settype(pthread_mutexattr_t *, int); +int __libc_mutexattr_destroy(pthread_mutexattr_t *); +__END_DECLS + +#define pthread_mutex_init __libc_mutex_init +#define pthread_mutex_lock __libc_mutex_lock +#define pthread_mutex_trylock __libc_mutex_trylock +#define pthread_mutex_unlock __libc_mutex_unlock +#define pthread_mutex_destroy __libc_mutex_destroy + +#define pthread_mutexattr_init __libc_mutexattr_init +#define pthread_mutexattr_settype __libc_mutexattr_settype +#define pthread_mutexattr_destroy __libc_mutexattr_destroy + +__BEGIN_DECLS +int __libc_cond_init(pthread_cond_t *, const pthread_condattr_t *); +int __libc_cond_signal(pthread_cond_t *); +int __libc_cond_broadcast(pthread_cond_t *); +int __libc_cond_destroy(pthread_cond_t *); +__END_DECLS + +#define pthread_cond_init __libc_cond_init +#define pthread_cond_signal __libc_cond_signal +#define pthread_cond_broadcast __libc_cond_broadcast +#define pthread_cond_destroy __libc_cond_destroy + +__BEGIN_DECLS +int __libc_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *); +int __libc_rwlock_rdlock(pthread_rwlock_t *); +int __libc_rwlock_wrlock(pthread_rwlock_t *); +int __libc_rwlock_tryrdlock(pthread_rwlock_t *); +int __libc_rwlock_trywrlock(pthread_rwlock_t *); +int __libc_rwlock_unlock(pthread_rwlock_t *); +int __libc_rwlock_destroy(pthread_rwlock_t *); +__END_DECLS + +#define pthread_rwlock_init __libc_rwlock_init +#define pthread_rwlock_rdlock __libc_rwlock_rdlock +#define pthread_rwlock_wrlock __libc_rwlock_wrlock +#define pthread_rwlock_tryrdlock __libc_rwlock_tryrdlock +#define pthread_rwlock_trywrlock __libc_rwlock_trywrlock +#define pthread_rwlock_unlock __libc_rwlock_unlock +#define pthread_rwlock_destroy __libc_rwlock_destroy + +__BEGIN_DECLS +int __libc_thr_keycreate(pthread_key_t *, void (*)(void *)); +int __libc_thr_setspecific(pthread_key_t, const void *); +void *__libc_thr_getspecific(pthread_key_t); +int __libc_thr_keydelete(pthread_key_t); +__END_DECLS + +#define pthread_key_create __libc_thr_keycreate +#define pthread_setspecific __libc_thr_setspecific +#define pthread_getspecific __libc_thr_getspecific +#define pthread_key_delete __libc_thr_keydelete + +__BEGIN_DECLS +int __libc_thr_once(pthread_once_t *, void (*)(void)); +pthread_t __libc_thr_self(void); +void __libc_thr_exit(void *) __attribute__((__noreturn__)); +int __libc_thr_setcancelstate(int, int *); +__END_DECLS + +#define pthread_once __libc_thr_once +#define pthread_self __libc_thr_self +#define pthread_exit __libc_thr_exit +#define pthread_setcancelstate __libc_thr_setcancelstate + +#endif /* __LIBPTHREAD_SOURCE__ */ + #endif /* _LIB_PTHREAD_H */ |
