diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:34:13 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:34:13 (GMT) |
commit | 67ad0519fd165acee4a4d2a94fa502e9e4847bd0 (patch) | |
tree | 1dbf50b3dff8d5ca7e9344733968c72704eb15ff /src/3rdparty/ptmalloc/sysdeps/pthread | |
download | Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.zip Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.gz Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.bz2 |
Long live Qt!
Diffstat (limited to 'src/3rdparty/ptmalloc/sysdeps/pthread')
-rw-r--r-- | src/3rdparty/ptmalloc/sysdeps/pthread/malloc-machine.h | 131 | ||||
-rw-r--r-- | src/3rdparty/ptmalloc/sysdeps/pthread/thread-st.h | 111 |
2 files changed, 242 insertions, 0 deletions
diff --git a/src/3rdparty/ptmalloc/sysdeps/pthread/malloc-machine.h b/src/3rdparty/ptmalloc/sysdeps/pthread/malloc-machine.h new file mode 100644 index 0000000..137a711 --- /dev/null +++ b/src/3rdparty/ptmalloc/sysdeps/pthread/malloc-machine.h @@ -0,0 +1,131 @@ +/* Basic platform-independent macro definitions for mutexes, + thread-specific data and parameters for malloc. + Posix threads (pthreads) version. + Copyright (C) 2004 Wolfram Gloger <wg@malloc.de>. + +Permission to use, copy, modify, distribute, and sell this software +and its documentation for any purpose is hereby granted without fee, +provided that (i) the above copyright notices and this permission +notice appear in all copies of the software and related documentation, +and (ii) the name of Wolfram Gloger may not be used in any advertising +or publicity relating to the software. + +THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, +EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY +WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + +IN NO EVENT SHALL WOLFRAM GLOGER BE LIABLE FOR ANY SPECIAL, +INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY +DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY +OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef _PTHREAD_MALLOC_MACHINE_H +#define _PTHREAD_MALLOC_MACHINE_H + +#include <pthread.h> + +#undef thread_atfork_static + +/* Use fast inline spinlocks with gcc. */ +#if (defined __i386__ || defined __x86_64__) && defined __GNUC__ && \ + !defined USE_NO_SPINLOCKS + +#include <time.h> +#include <sched.h> + +typedef struct { + volatile unsigned int lock; + int pad0_; +} mutex_t; + +#define MUTEX_INITIALIZER { 0 } +#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) { + __asm__ __volatile__ ("movl %1, %0" : "=m" (m->lock) : "g"(0) : "memory"); + return 0; +} + +#else + +/* Normal pthread mutex. */ +typedef pthread_mutex_t mutex_t; + +#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER +#define mutex_init(m) pthread_mutex_init(m, NULL) +#define mutex_lock(m) pthread_mutex_lock(m) +#define mutex_trylock(m) pthread_mutex_trylock(m) +#define mutex_unlock(m) pthread_mutex_unlock(m) + +#endif /* (__i386__ || __x86_64__) && __GNUC__ && !USE_NO_SPINLOCKS */ + +/* thread specific data */ +#if defined(__sgi) || defined(USE_TSD_DATA_HACK) + +/* Hack for thread-specific data, e.g. on Irix 6.x. We can't use + pthread_setspecific because that function calls malloc() itself. + The hack only works when pthread_t can be converted to an integral + type. */ + +typedef void *tsd_key_t[256]; +#define tsd_key_create(key, destr) do { \ + int i; \ + for(i=0; i<256; i++) (*key)[i] = 0; \ +} while(0) +#define tsd_setspecific(key, data) \ + (key[(unsigned)pthread_self() % 256] = (data)) +#define tsd_getspecific(key, vptr) \ + (vptr = key[(unsigned)pthread_self() % 256]) + +#else + +typedef pthread_key_t tsd_key_t; + +#define tsd_key_create(key, destr) pthread_key_create(key, destr) +#define tsd_setspecific(key, data) pthread_setspecific(key, data) +#define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key)) + +#endif + +/* at fork */ +#define thread_atfork(prepare, parent, child) \ + pthread_atfork(prepare, parent, child) + +#include <sysdeps/generic/malloc-machine.h> + +#endif /* !defined(_MALLOC_MACHINE_H) */ diff --git a/src/3rdparty/ptmalloc/sysdeps/pthread/thread-st.h b/src/3rdparty/ptmalloc/sysdeps/pthread/thread-st.h new file mode 100644 index 0000000..f97a0a3 --- /dev/null +++ b/src/3rdparty/ptmalloc/sysdeps/pthread/thread-st.h @@ -0,0 +1,111 @@ +/* + * $Id: thread-st.h$ + * pthread version + * by Wolfram Gloger 2004 + */ + +#include <pthread.h> +#include <stdio.h> + +pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER; +pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER; + +#ifndef USE_PTHREADS_STACKS +#define USE_PTHREADS_STACKS 0 +#endif + +#ifndef STACKSIZE +#define STACKSIZE 32768 +#endif + +struct thread_st { + char *sp; /* stack pointer, can be 0 */ + void (*func)(struct thread_st* st); /* must be set by user */ + pthread_t id; + int flags; + struct user_data u; +}; + +static void +thread_init(void) +{ + printf("Using posix threads.\n"); + pthread_cond_init(&finish_cond, NULL); + pthread_mutex_init(&finish_mutex, NULL); +} + +static void * +thread_wrapper(void *ptr) +{ + struct thread_st *st = (struct thread_st*)ptr; + + /*printf("begin %p\n", st->sp);*/ + st->func(st); + pthread_mutex_lock(&finish_mutex); + st->flags = 1; + pthread_mutex_unlock(&finish_mutex); + pthread_cond_signal(&finish_cond); + /*printf("end %p\n", st->sp);*/ + return NULL; +} + +/* Create a thread. */ +static int +thread_create(struct thread_st *st) +{ + st->flags = 0; + { + pthread_attr_t* attr_p = 0; +#if USE_PTHREADS_STACKS + pthread_attr_t attr; + + pthread_attr_init (&attr); + if(!st->sp) + st->sp = malloc(STACKSIZE+16); + if(!st->sp) + return -1; + if(pthread_attr_setstacksize(&attr, STACKSIZE)) + fprintf(stderr, "error setting stacksize"); + else + pthread_attr_setstackaddr(&attr, st->sp + STACKSIZE); + /*printf("create %p\n", st->sp);*/ + attr_p = &attr; +#endif + return pthread_create(&st->id, attr_p, thread_wrapper, st); + } + return 0; +} + +/* Wait for one of several subthreads to finish. */ +static void +wait_for_thread(struct thread_st st[], int n_thr, + int (*end_thr)(struct thread_st*)) +{ + int i; + + pthread_mutex_lock(&finish_mutex); + for(;;) { + int term = 0; + for(i=0; i<n_thr; i++) + if(st[i].flags) { + /*printf("joining %p\n", st[i].sp);*/ + if(pthread_join(st[i].id, NULL) == 0) { + st[i].flags = 0; + if(end_thr) + end_thr(&st[i]); + } else + fprintf(stderr, "can't join\n"); + ++term; + } + if(term > 0) + break; + pthread_cond_wait(&finish_cond, &finish_mutex); + } + pthread_mutex_unlock(&finish_mutex); +} + +/* + * Local variables: + * tab-width: 4 + * End: + */ |