diff --git a/src/prelude-async.c b/src/prelude-async.c index f79396a..add3a38 100644 --- a/src/prelude-async.c +++ b/src/prelude-async.c @@ -82,6 +82,36 @@ # endif #endif +/* + * In case weak symbol are supported, we should define them for symbols + * used in this implementation so that an application linking to + * libprelude without using pthreads flags won't give undefined + * reference. + */ +#ifdef USE_POSIX_THREADS_WEAK +# pragma weak pthread_create +# pragma weak pthread_join +# pragma weak pthread_sigmask + +# ifdef HAVE_PTHREAD_ATFORK +# pragma weak pthread_atfork +# endif + +# pragma weak pthread_mutex_init +# pragma weak pthread_mutex_lock +# pragma weak pthread_mutex_unlock +# pragma weak pthread_mutex_destroy + +# pragma weak pthread_cond_init +# pragma weak pthread_cond_wait +# pragma weak pthread_cond_signal +# pragma weak pthread_cond_timedwait +# pragma weak pthread_cond_destroy + +# pragma weak pthread_condattr_init +# pragma weak pthread_condattr_setclock +#endif + static PRELUDE_LIST(joblist); diff --git a/src/prelude-thread.c b/src/prelude-thread.c index f0f3b38..702e60b 100644 --- a/src/prelude-thread.c +++ b/src/prelude-thread.c @@ -42,6 +42,44 @@ #ifdef USE_POSIX_THREADS_WEAK +# pragma weak pthread_create +# pragma weak pthread_join +# pragma weak pthread_sigmask +# pragma weak pthread_once +# pragma weak pthread_exit + +# ifdef HAVE_PTHREAD_ATFORK +# pragma weak pthread_atfork +# endif + +# pragma weak pthread_mutex_init +# pragma weak pthread_mutex_lock +# pragma weak pthread_mutex_unlock +# pragma weak pthread_mutex_destroy + +# pragma weak pthread_mutexattr_init +# pragma weak pthread_mutexattr_settype +# pragma weak pthread_mutexattr_destroy + +# pragma weak pthread_cond_init +# pragma weak pthread_cond_wait +# pragma weak pthread_cond_signal +# pragma weak pthread_cond_broadcast +# pragma weak pthread_cond_timedwait +# pragma weak pthread_cond_destroy + +# pragma weak pthread_condattr_init +# pragma weak pthread_condattr_setclock + +# pragma weak pthread_getspecific +# pragma weak pthread_setspecific + +# pragma weak pthread_key_delete + +# ifndef pthread_self +# pragma weak pthread_self +# endif + # if !PTHREAD_IN_USE_DETECTION_HARD # pragma weak pthread_cancel # define __prelude_thread_in_use() (pthread_cancel != NULL) @@ -91,7 +129,7 @@ static void thread_init_if_needed(void) { if ( ! need_init ) return; - + pthread_key_create(&thread_error_key, thread_error_key_destroy); need_init = FALSE; } @@ -171,7 +209,7 @@ int prelude_thread_cond_destroy(pthread_cond_t *cond) } -int prelude_thread_condattr_init(pthread_condattr_t *attr) +int prelude_thread_condattr_init(pthread_condattr_t *attr) { THR_FUNC(pthread_condattr_init(attr)); } @@ -228,15 +266,15 @@ void _prelude_thread_deinit(void) { if ( use_thread ) { char *previous; - + previous = pthread_getspecific(thread_error_key); if ( previous ) free(previous); - + pthread_key_delete(thread_error_key); need_init = TRUE; } - + else if ( shared_error_buffer ) { free(shared_error_buffer); shared_error_buffer = NULL; @@ -262,9 +300,9 @@ static prelude_bool_t _prelude_thread_hard_in_use(void) int ret; void *retval; pthread_t thread; - + ret = pthread_create(&thread, NULL, dummy_thread_func, NULL); - if ( ret != 0 ) + if ( ret != 0 ) /* we're using libc stubs */ return FALSE; @@ -274,7 +312,7 @@ static prelude_bool_t _prelude_thread_hard_in_use(void) ret = pthread_join(thread, &retval); if ( ret != 0 ) abort(); - + return TRUE; } @@ -285,18 +323,18 @@ static prelude_bool_t _prelude_thread_hard_in_use(void) prelude_bool_t _prelude_thread_in_use(void) { static prelude_bool_t tested = FALSE; - + if ( tested ) { thread_init_if_needed(); return use_thread; } - + use_thread = __prelude_thread_in_use(); tested = TRUE; prelude_log(PRELUDE_LOG_DEBUG, "[init] thread used=%d\n", use_thread); thread_init_if_needed(); - + return use_thread; } @@ -305,10 +343,10 @@ int _prelude_thread_set_error(const char *error) { char *previous; - if ( ! use_thread ) { + if ( ! use_thread ) { if ( shared_error_buffer ) free(shared_error_buffer); - + shared_error_buffer = strdup(error); } @@ -316,17 +354,17 @@ int _prelude_thread_set_error(const char *error) previous = pthread_getspecific(thread_error_key); if ( previous ) free(previous); - + pthread_setspecific(thread_error_key, strdup(error)); } - + return 0; } const char *_prelude_thread_get_error(void) -{ +{ if ( use_thread ) return pthread_getspecific(thread_error_key); else