From 2f9d7fddd64d160025a657e9f5949b5575fd4525 Mon Sep 17 00:00:00 2001 From: ebotcazou Date: Tue, 31 Oct 2006 17:54:58 +0000 Subject: PR target/24071 * gthr-posix.h (__gthread_active_p): New implementation on Solaris. * gthr-posix95.h (__gthread_active_p): Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@118259 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/gthr-posix.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'gcc/gthr-posix.h') diff --git a/gcc/gthr-posix.h b/gcc/gthr-posix.h index 62f83b3ffbc..fb58be94ea6 100644 --- a/gcc/gthr-posix.h +++ b/gcc/gthr-posix.h @@ -142,6 +142,59 @@ __gthrw(pthread_setschedparam) #if SUPPORTS_WEAK && GTHREAD_USE_WEAK +/* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if + -pthreads is not specified. The functions are dummies and most return an + error value. However pthread_once returns 0 without invoking the routine + it is passed so we cannot pretend that the interface is active if -pthreads + is not specified. On Solaris 2.5.1, the interface is not exposed at all so + we need to play the usual game with weak symbols. On Solaris 10 and up, a + working interface is always exposed. */ + +#if defined(__sun) && defined(__svr4__) + +static volatile int __gthread_active = -1; + +static void +__gthread_trigger (void) +{ + __gthread_active = 1; +} + +static inline int +__gthread_active_p (void) +{ + static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER; + static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT; + + /* Avoid reading __gthread_active twice on the main code path. */ + int __gthread_active_latest_value = __gthread_active; + + /* This test is not protected to avoid taking a lock on the main code + path so every update of __gthread_active in a threaded program must + be atomic with regard to the result of the test. */ + if (__builtin_expect (__gthread_active_latest_value < 0, 0)) + { + if (__gthrw_(pthread_once)) + { + /* If this really is a threaded program, then we must ensure that + __gthread_active has been set to 1 before exiting this block. */ + __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex); + __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger); + __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex); + } + + /* Make sure we'll never enter this block again. */ + if (__gthread_active < 0) + __gthread_active = 0; + + __gthread_active_latest_value = __gthread_active; + } + + return __gthread_active_latest_value != 0; +} + +#else /* not Solaris */ + static inline int __gthread_active_p (void) { @@ -150,6 +203,8 @@ __gthread_active_p (void) return __gthread_active_ptr != 0; } +#endif /* Solaris */ + #else /* not SUPPORTS_WEAK */ static inline int -- cgit v1.2.1