Changing threading semantics from init early to init before second thread

Alexander Larsson alexl at redhat.com
Mon Aug 28 08:06:30 PDT 2006


On Mon, 2006-08-28 at 11:02 -0400, Havoc Pennington wrote:
> Alexander Larsson wrote:
> > 
> > I saw another interesting approach in libgamin. They use ELF magic (weak
> > symbols) to avoid linking to libpthreads, and then only use threads if
> > the app that pulled in libgamin linked to libpthreads.
> > 
> > We could use the same approach when selecting the default thread
> > implementation.
> 
> How gross is it? I mean, is it a bunch of weird magic that will keep 
> breaking, or pretty straightforward?

Its pretty ok:

#ifdef HAVE_PTHREAD_H
#include <pthread.h>

static int is_threaded = -1;
#endif

/*
 * Use weak symbols on gcc/linux to avoid forcing link with pthreads for
 * non threaded apps using the gamin library.
 * patches to extends this to other platforms/compilers welcome, note that
 * this also affects configure.in
 * #pragma weak .... might be more portable but may not work from shared
 * libraries.
 */
#ifdef __GNUC__
#ifdef linux || defined(__GLIBC__)
#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
extern int pthread_mutexattr_init(pthread_mutexattr_t *attr)
           __attribute((weak));
extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
           __attribute((weak));
extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr)
           __attribute((weak));
extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
           __attribute((weak));
extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)
           __attribute((weak));
extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr)
           __attribute((weak));
#endif
#endif /* linux */
#endif /* __GNUC__ */

...

#ifdef HAVE_PTHREAD_H
    if (is_threaded == -1) {
        if ((pthread_mutexattr_init != NULL) &&
	    (pthread_mutexattr_settype != NULL) &&
	    (pthread_mutex_init != NULL) &&
	    (pthread_mutex_lock != NULL) &&
	    (pthread_mutex_unlock != NULL) &&
	    (pthread_mutexattr_destroy != NULL)) {
	    is_threaded = 1;
	    GAM_DEBUG(DEBUG_INFO, "Activating thread safety\n");
	} else {
	    GAM_DEBUG(DEBUG_INFO, "Not activating thread safety\n");
	}
    }
    if (is_threaded > 0) {
	pthread_mutexattr_init(&attr);
#ifdef __GLIBC__
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
#else
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
#endif
	pthread_mutex_init(&ret->lock, &attr);
	pthread_mutexattr_destroy(&attr);
    }
#endif

...

void gamin_data_lock(GAMDataPtr data)
{
#ifdef HAVE_PTHREAD_H
    if (is_threaded > 0)
	pthread_mutex_lock(&data->lock);
#endif
}



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Alexander Larsson                                            Red Hat, Inc 
                   alexl at redhat.com    alla at lysator.liu.se 
He's a jaded overambitious cat burglar who must take medication to keep him 
sane. She's a supernatural bisexual barmaid who believes she is the 
reincarnation of an ancient Egyptian queen. They fight crime! 



More information about the dbus mailing list