--- glibc-2.3.1-20030130/malloc/thread-m.h.malloc 2003-01-01 17:44:43.000000000 +0100 +++ glibc-2.3.1-20030130/malloc/thread-m.h 2003-02-04 23:43:56.000000000 +0100 @@ -29,7 +29,66 @@ #undef thread_atfork_static -#if defined(_LIBC) /* The GNU C library, a special case of Posix threads */ +/* mutex */ +#if (defined __i386__ || defined __x86_64__) && defined __GNUC__ && \ + !defined USE_NO_SPINLOCKS + +#include <time.h> +#include <sched.h> + +/* Use fast inline spinlocks. */ +typedef struct { + volatile unsigned int lock; + int pad0_; +} mutex_t; + +#define mutex_init(m) ((m)->lock = 0) +static inline int mutex_lock(mutex_t *m) { + int cnt = 0, r; + struct timespec tm; + + for(;;) { + __asm__ __volatile__ + ("xchgl %0, %1" + : "=r"(r), "=m"(m->lock) + : "0"(1), "m"(m->lock) + : "memory"); + if(!r) + return 0; + if(cnt < 50) { +#ifdef _LIBC + __sched_yield(); +#else + sched_yield(); +#endif + cnt++; + } else { + tm.tv_sec = 0; + tm.tv_nsec = 2000001; + nanosleep(&tm, NULL); + cnt = 0; + } + } +} +static inline int mutex_trylock(mutex_t *m) { + int r; + + __asm__ __volatile__ + ("xchgl %0, %1" + : "=r"(r), "=m"(m->lock) + : "0"(1), "m"(m->lock) + : "memory"); + return r; +} +static inline int mutex_unlock(mutex_t *m) { + m->lock = 0; + __asm __volatile ("" : "=m" (m->lock) : "0" (m->lock)); + return 0; +} +#define HAVE_MUTEX +#endif /* (__i386__ || __x86_64__) && __GNUC__ && !USE_NO_SPINLOCKS */ + +#if defined(_LIBC) && !defined(HAVE_MUTEX) /* The GNU C library, a special case of Posix threads */ #include <bits/libc-lock.h> @@ -89,6 +148,7 @@ #elif defined(MUTEX_INITIALIZER) /* Assume hurd, with cthreads */ +#ifndef HAVE_MUTEX /* Cthreads `mutex_t' is a pointer to a mutex, and malloc wants just the mutex itself. */ #undef mutex_t @@ -104,6 +164,7 @@ #define mutex_unlock(m) (__mutex_unlock(m), 0) #define mutex_trylock(m) (!__mutex_trylock(m)) +#endif #define thread_atfork(prepare, parent, child) do {} while(0) #define thread_atfork_static(prepare, parent, child) \ @@ -138,61 +199,8 @@ #include <pthread.h> -/* mutex */ -#if (defined __i386__ || defined __x86_64__) && defined __GNUC__ && \ - !defined USE_NO_SPINLOCKS - -#include <time.h> - -/* Use fast inline spinlocks. */ -typedef struct { - volatile unsigned int lock; - int pad0_; -} mutex_t; - -#define mutex_init(m) ((m)->lock = 0) -static inline int mutex_lock(mutex_t *m) { - int cnt = 0, r; - struct timespec tm; - - for(;;) { - __asm__ __volatile__ - ("xchgl %0, %1" - : "=r"(r), "=m"(m->lock) - : "0"(1), "m"(m->lock) - : "memory"); - if(!r) - return 0; - if(cnt < 50) { - sched_yield(); - cnt++; - } else { - tm.tv_sec = 0; - tm.tv_nsec = 2000001; - nanosleep(&tm, NULL); - cnt = 0; - } - } -} -static inline int mutex_trylock(mutex_t *m) { - int r; - - __asm__ __volatile__ - ("xchgl %0, %1" - : "=r"(r), "=m"(m->lock) - : "0"(1), "m"(m->lock) - : "memory"); - return r; -} -static inline int mutex_unlock(mutex_t *m) { - m->lock = 0; - __asm __volatile ("" : "=m" (m->lock) : "0" (m->lock)); - return 0; -} - -#else - /* Normal pthread mutex. */ +#ifndef HAVE_MUTEX typedef pthread_mutex_t mutex_t; #define mutex_init(m) pthread_mutex_init(m, NULL) @@ -200,7 +208,7 @@ #define mutex_trylock(m) pthread_mutex_trylock(m) #define mutex_unlock(m) pthread_mutex_unlock(m) -#endif /* (__i386__ || __x86_64__) && __GNUC__ && !USE_NO_SPINLOCKS */ +#endif /* !HAVE_MUTEX */ /* thread specific data */ #if defined(__sgi) || defined(USE_TSD_DATA_HACK) @@ -284,6 +292,7 @@ #ifdef NO_THREADS /* No threads, provide dummy macros */ +#ifndef HAVE_MUTEX /* The mutex functions used to do absolutely nothing, i.e. lock, trylock and unlock would always just return 0. However, even without any concurrently active threads, a mutex can be used @@ -296,6 +305,7 @@ #define mutex_lock(m) ((*(m) = 1), 0) #define mutex_trylock(m) (*(m) ? 1 : ((*(m) = 1), 0)) #define mutex_unlock(m) (*(m) = 0) +#endif typedef void *tsd_key_t; #define tsd_key_create(key, destr) do {} while(0)