umem  1.0.1
sol_compat.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006-2008 Message Systems, Inc. All rights reserved
3  * This header file distributed under the terms of the CDDL.
4  * Portions Copyright 2004 Sun Microsystems, Inc. All Rights reserved.
5  */
6 #ifndef _EC_UMEM_SOL_COMPAT_H_
7 #define _EC_UMEM_SOL_COMPAT_H_
8 
9 #include "config.h"
10 
11 #include <stdint.h>
12 #include <pthread.h>
13 
14 #ifdef HAVE_SYS_TIME_H
15 #include <sys/time.h>
16 #endif
17 
18 #ifdef _WIN32
19 # define THR_RETURN DWORD
20 # define THR_API WINAPI
21 # define INLINE __inline
22 #else
23 # define THR_RETURN void *
24 # define THR_API
25 # define INLINE inline
26 #endif
27 
28 #if defined(__MACH__) || defined(_WIN32)
29 #define NO_WEAK_SYMBOLS
30 #define _umem_cache_alloc(a,b) umem_cache_alloc(a,b)
31 #define _umem_cache_free(a,b) umem_cache_free(a,b)
32 #define _umem_zalloc(a,b) umem_zalloc(a,b)
33 #define _umem_alloc(a,b) umem_alloc(a,b)
34 #define _umem_alloc_align(a,b,c) umem_alloc_align(a,b,c)
35 #define _umem_free(a,b) umem_free(a,b)
36 #define _umem_free_align(a,b) umem_free_align(a,b)
37 #endif
38 
39 #ifdef _WIN32
40 #define bcopy(s, d, n) memcpy(d, s, n)
41 #define bzero(m, s) memset(m, 0, s)
42 #endif
43 
44 typedef pthread_t thread_t;
45 typedef pthread_mutex_t mutex_t;
46 typedef pthread_cond_t cond_t;
47 typedef u_int64_t hrtime_t;
48 typedef uint32_t uint_t;
49 typedef unsigned long ulong_t;
50 typedef struct timespec timestruc_t;
51 typedef long long longlong_t;
52 typedef struct timespec timespec_t;
53 static INLINE hrtime_t gethrtime(void) {
54  struct timeval tv;
55  gettimeofday(&tv, NULL);
56  return (((u_int64_t)tv.tv_sec) << 32) | tv.tv_usec;
57 }
58 # define thr_self() pthread_self()
59 static INLINE thread_t _thr_self(void) {
60  return thr_self();
61 }
62 #if defined(_MACH_PORT_T)
63 #define CPUHINT() (pthread_mach_thread_np(pthread_self()))
64 #endif
65 # define thr_sigsetmask pthread_sigmask
66 
67 #define THR_BOUND 1
68 #define THR_DETACHED 2
69 #define THR_DAEMON 4
70 
71 static INLINE int thr_create(void *stack_base,
72  size_t stack_size, THR_RETURN (THR_API *start_func)(void*),
73  void *arg, long flags, thread_t *new_thread_ID)
74 {
75  int ret;
76  pthread_attr_t attr;
77 
78  pthread_attr_init(&attr);
79 
80  if (flags & THR_DETACHED) {
81  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
82  }
83  ret = pthread_create(new_thread_ID, &attr, start_func, arg);
84  pthread_attr_destroy(&attr);
85  return ret;
86 }
87 
88 
89 # define mutex_init(mp, type, arg) pthread_mutex_init(mp, NULL)
90 # define mutex_lock(mp) pthread_mutex_lock(mp)
91 # define mutex_unlock(mp) pthread_mutex_unlock(mp)
92 # define mutex_destroy(mp) pthread_mutex_destroy(mp)
93 # define mutex_trylock(mp) pthread_mutex_trylock(mp)
94 # define DEFAULTMUTEX PTHREAD_MUTEX_INITIALIZER
95 # define DEFAULTCV PTHREAD_COND_INITIALIZER
96 # define MUTEX_HELD(mp) 1 /* not really, but only used in an assert */
97 
98 # define cond_init(c, type, arg) pthread_cond_init(c, NULL)
99 # define cond_wait(c, m) pthread_cond_wait(c, m)
100 # define _cond_wait(c, m) pthread_cond_wait(c, m)
101 # define cond_signal(c) pthread_cond_signal(c)
102 # define cond_broadcast(c) pthread_cond_broadcast(c)
103 # define cond_destroy(c) pthread_cond_destroy(c)
104 # define cond_timedwait pthread_cond_timedwait
105 # define _cond_timedwait pthread_cond_timedwait
106 
107 #ifndef RTLD_FIRST
108 # define RTLD_FIRST 0
109 #endif
110 
111 #ifdef ECELERITY
112 # include "ec_atomic.h"
113 #else
114 # ifdef _WIN32
115 # define ec_atomic_inc(a) InterlockedIncrement(a)
116 # define ec_atomic_inc64(a) InterlockedIncrement64(a)
117 # elif (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__)
118 static INLINE uint_t ec_atomic_cas(uint_t *mem, uint_t with, uint_t cmp)
119 {
120  uint_t prev;
121  asm volatile ("lock; cmpxchgl %1, %2"
122  : "=a" (prev)
123  : "r" (with), "m" (*(mem)), "0" (cmp)
124  : "memory");
125  return prev;
126 }
127 # endif
128 
129 # ifndef ec_atomic_inc
130 static INLINE uint_t ec_atomic_inc(uint_t *mem)
131 {
132  register uint_t last;
133  do {
134  last = *mem;
135  } while (ec_atomic_cas(mem, last+1, last) != last);
136  return ++last;
137 }
138 # endif
139 # ifndef ec_atomic_inc64
140  /* yeah, it's not great. It's only used to bump failed allocation
141  * counts, so it is not critical right now. */
142 # define ec_atomic_inc64(a) (*a)++
143 # endif
144 
145 #endif
146 
147 #define P2PHASE(x, align) ((x) & ((align) - 1))
148 #define P2ALIGN(x, align) ((x) & -(align))
149 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
150 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
151 #define P2END(x, align) (-(~(x) & -(align)))
152 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
153 #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
154 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
155 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
156 #define ISP2(x) (((x) & ((x) - 1)) == 0)
157 
158 /* beware! umem only uses these atomic adds for incrementing by 1 */
159 #define atomic_add_64(lvalptr, delta) ec_atomic_inc64(lvalptr)
160 #define atomic_add_32_nv(a, b) ec_atomic_inc(a)
161 
162 #ifndef NANOSEC
163 #define NANOSEC 1000000000
164 #endif
165 
166 #ifdef _WIN32
167 #define issetugid() 0
168 #elif !HAVE_ISSETUGID
169 #define issetugid() (geteuid() == 0)
170 #endif
171 
172 #define _sysconf(a) sysconf(a)
173 #define __NORETURN __attribute__ ((noreturn))
174 
175 #define EC_UMEM_DUMMY_PCSTACK 1
176 static INLINE int __nthreads(void)
177 {
178  /* or more; just to force multi-threaded mode */
179  return 2;
180 }
181 
182 #if (SIZEOF_VOID_P == 8)
183 # define _LP64 1
184 #endif
185 
186 #ifndef MIN
187 # define MIN(a,b) ((a) < (b) ? (a) : (b))
188 #endif
189 #ifndef MAX
190 # define MAX(a,b) ((a) > (b) ? (a) : (b))
191 #endif
192 
193 
194 #endif
long long longlong_t
Definition: sol_compat.h:51
struct timespec timespec_t
Definition: sol_compat.h:52
#define THR_RETURN
Definition: sol_compat.h:23
#define THR_API
Definition: sol_compat.h:24
pthread_cond_t cond_t
Definition: sol_compat.h:46
pthread_mutex_t mutex_t
Definition: sol_compat.h:45
#define thr_self()
Definition: sol_compat.h:58
#define THR_DETACHED
Definition: sol_compat.h:68
u_int64_t hrtime_t
Definition: sol_compat.h:47
pthread_t thread_t
Definition: sol_compat.h:44
#define INLINE
Definition: sol_compat.h:25
unsigned long ulong_t
Definition: sol_compat.h:49
thread_t _thr_self(void)
struct timespec timestruc_t
Definition: sol_compat.h:50
uint32_t uint_t
Definition: sol_compat.h:48