OpenMPI  0.1.1
malloc-machine.h
1 /* Basic platform-independent macro definitions for mutexes,
2  thread-specific data and parameters for malloc.
3  Posix threads (pthreads) version.
4  Copyright (C) 2004 Wolfram Gloger <wg@malloc.de>.
5 
6 Permission to use, copy, modify, distribute, and sell this software
7 and its documentation for any purpose is hereby granted without fee,
8 provided that (i) the above copyright notices and this permission
9 notice appear in all copies of the software and related documentation,
10 and (ii) the name of Wolfram Gloger may not be used in any advertising
11 or publicity relating to the software.
12 
13 THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 
17 IN NO EVENT SHALL WOLFRAM GLOGER BE LIABLE FOR ANY SPECIAL,
18 INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
19 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY
21 OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 PERFORMANCE OF THIS SOFTWARE.
23 */
24 
25 #ifndef _PTHREAD_MALLOC_MACHINE_H
26 #define _PTHREAD_MALLOC_MACHINE_H
27 
28 #include <pthread.h>
29 
30 #undef thread_atfork_static
31 
32 /* Use fast inline spinlocks with gcc. */
33 #if (defined __i386__ || defined __x86_64__) && defined __GNUC__ && \
34  !defined USE_NO_SPINLOCKS
35 
36 #include <time.h>
37 #include <sched.h>
38 
39 typedef struct {
40  volatile unsigned int lock;
41  int pad0_;
42 } mutex_t;
43 
44 #define MUTEX_INITIALIZER { 0 }
45 #define mutex_init(m) ((m)->lock = 0)
46 static inline int mutex_lock(mutex_t *m) {
47  int cnt = 0, r;
48  struct timespec tm;
49 
50  for(;;) {
51  __asm__ __volatile__
52  ("xchgl %0, %1"
53  : "=r"(r), "=m"(m->lock)
54  : "0"(1), "m"(m->lock)
55  : "memory");
56  if(!r)
57  return 0;
58  if(cnt < 50) {
59  sched_yield();
60  cnt++;
61  } else {
62  tm.tv_sec = 0;
63  tm.tv_nsec = 2000001;
64  nanosleep(&tm, NULL);
65  cnt = 0;
66  }
67  }
68 }
69 static inline int mutex_trylock(mutex_t *m) {
70  int r;
71 
72  __asm__ __volatile__
73  ("xchgl %0, %1"
74  : "=r"(r), "=m"(m->lock)
75  : "0"(1), "m"(m->lock)
76  : "memory");
77  return r;
78 }
79 static inline int mutex_unlock(mutex_t *m) {
80  m->lock = 0;
81  __asm __volatile__ ("" : : : "memory");
82  return 0;
83 }
84 
85 #else
86 
87 /* Normal pthread mutex. */
88 typedef pthread_mutex_t mutex_t;
89 
90 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
91 #define mutex_init(m) pthread_mutex_init(m, NULL)
92 #define mutex_lock(m) pthread_mutex_lock(m)
93 #define mutex_trylock(m) pthread_mutex_trylock(m)
94 #define mutex_unlock(m) pthread_mutex_unlock(m)
95 
96 #endif /* (__i386__ || __x86_64__) && __GNUC__ && !USE_NO_SPINLOCKS */
97 
98 /* thread specific data */
99 #if defined(__sgi) || defined(USE_TSD_DATA_HACK)
100 
101 /* Hack for thread-specific data, e.g. on Irix 6.x. We can't use
102  pthread_setspecific because that function calls malloc() itself.
103  The hack only works when pthread_t can be converted to an integral
104  type. */
105 
106 typedef void *tsd_key_t[256];
107 #define tsd_key_create(key, destr) do { \
108  int i; \
109  for(i=0; i<256; i++) (*key)[i] = 0; \
110 } while(0)
111 #define tsd_setspecific(key, data) \
112  (key[(unsigned)pthread_self() % 256] = (data))
113 #define tsd_getspecific(key, vptr) \
114  (vptr = key[(unsigned)pthread_self() % 256])
115 
116 #else
117 
118 typedef pthread_key_t tsd_key_t;
119 
120 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
121 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
122 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
123 
124 #endif
125 
126 /* at fork */
127 #define thread_atfork(prepare, parent, child) \
128  pthread_atfork(prepare, parent, child)
129 
130 #include <sysdeps/generic/malloc-machine.h>
131 
132 #endif /* !defined(_MALLOC_MACHINE_H) */
Definition: ompi_time.h:160