00001
00002
00003
00004
00005
00006 #ifndef _EC_UMEM_SOL_COMPAT_H_
00007 #define _EC_UMEM_SOL_COMPAT_H_
00008
00009 #include "config.h"
00010
00011 #include <stdint.h>
00012 #include <pthread.h>
00013
00014 #ifdef HAVE_SYS_TIME_H
00015 #include <sys/time.h>
00016 #endif
00017
00018 #ifdef _WIN32
00019 # define THR_RETURN DWORD
00020 # define THR_API WINAPI
00021 # define INLINE __inline
00022 #else
00023 # define THR_RETURN void *
00024 # define THR_API
00025 # define INLINE inline
00026 #endif
00027
00028 #if defined(__MACH__) || defined(_WIN32)
00029 #define NO_WEAK_SYMBOLS
00030 #define _umem_cache_alloc(a,b) umem_cache_alloc(a,b)
00031 #define _umem_cache_free(a,b) umem_cache_free(a,b)
00032 #define _umem_zalloc(a,b) umem_zalloc(a,b)
00033 #define _umem_alloc(a,b) umem_alloc(a,b)
00034 #define _umem_alloc_align(a,b,c) umem_alloc_align(a,b,c)
00035 #define _umem_free(a,b) umem_free(a,b)
00036 #define _umem_free_align(a,b) umem_free_align(a,b)
00037 #endif
00038
00039 #ifdef _WIN32
00040 #define bcopy(s, d, n) memcpy(d, s, n)
00041 #define bzero(m, s) memset(m, 0, s)
00042 #endif
00043
00044 typedef pthread_t thread_t;
00045 typedef pthread_mutex_t mutex_t;
00046 typedef pthread_cond_t cond_t;
00047 typedef u_int64_t hrtime_t;
00048 typedef uint32_t uint_t;
00049 typedef unsigned long ulong_t;
00050 typedef struct timespec timestruc_t;
00051 typedef long long longlong_t;
00052 typedef struct timespec timespec_t;
00053 static INLINE hrtime_t gethrtime(void) {
00054 struct timeval tv;
00055 gettimeofday(&tv, NULL);
00056 return (((u_int64_t)tv.tv_sec) << 32) | tv.tv_usec;
00057 }
00058 # define thr_self() pthread_self()
00059 static INLINE thread_t _thr_self(void) {
00060 return thr_self();
00061 }
00062 #if defined(_MACH_PORT_T)
00063 #define CPUHINT() (pthread_mach_thread_np(pthread_self()))
00064 #endif
00065 # define thr_sigsetmask pthread_sigmask
00066
00067 #define THR_BOUND 1
00068 #define THR_DETACHED 2
00069 #define THR_DAEMON 4
00070
00071 static INLINE int thr_create(void *stack_base,
00072 size_t stack_size, THR_RETURN (THR_API *start_func)(void*),
00073 void *arg, long flags, thread_t *new_thread_ID)
00074 {
00075 int ret;
00076 pthread_attr_t attr;
00077
00078 pthread_attr_init(&attr);
00079
00080 if (flags & THR_DETACHED) {
00081 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
00082 }
00083 ret = pthread_create(new_thread_ID, &attr, start_func, arg);
00084 pthread_attr_destroy(&attr);
00085 return ret;
00086 }
00087
00088
00089 # define mutex_init(mp, type, arg) pthread_mutex_init(mp, NULL)
00090 # define mutex_lock(mp) pthread_mutex_lock(mp)
00091 # define mutex_unlock(mp) pthread_mutex_unlock(mp)
00092 # define mutex_destroy(mp) pthread_mutex_destroy(mp)
00093 # define mutex_trylock(mp) pthread_mutex_trylock(mp)
00094 # define DEFAULTMUTEX PTHREAD_MUTEX_INITIALIZER
00095 # define DEFAULTCV PTHREAD_COND_INITIALIZER
00096 # define MUTEX_HELD(mp) 1
00097
00098 # define cond_init(c, type, arg) pthread_cond_init(c, NULL)
00099 # define cond_wait(c, m) pthread_cond_wait(c, m)
00100 # define _cond_wait(c, m) pthread_cond_wait(c, m)
00101 # define cond_signal(c) pthread_cond_signal(c)
00102 # define cond_broadcast(c) pthread_cond_broadcast(c)
00103 # define cond_destroy(c) pthread_cond_destroy(c)
00104 # define cond_timedwait pthread_cond_timedwait
00105 # define _cond_timedwait pthread_cond_timedwait
00106
00107 #ifndef RTLD_FIRST
00108 # define RTLD_FIRST 0
00109 #endif
00110
00111 #ifdef ECELERITY
00112 # include "ec_atomic.h"
00113 #else
00114 # ifdef _WIN32
00115 # define ec_atomic_inc(a) InterlockedIncrement(a)
00116 # define ec_atomic_inc64(a) InterlockedIncrement64(a)
00117 # elif (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__)
00118 static INLINE uint_t ec_atomic_cas(uint_t *mem, uint_t with, uint_t cmp)
00119 {
00120 uint_t prev;
00121 asm volatile ("lock; cmpxchgl %1, %2"
00122 : "=a" (prev)
00123 : "r" (with), "m" (*(mem)), "0" (cmp)
00124 : "memory");
00125 return prev;
00126 }
00127 # endif
00128
00129 # ifndef ec_atomic_inc
00130 static INLINE uint_t ec_atomic_inc(uint_t *mem)
00131 {
00132 register uint_t last;
00133 do {
00134 last = *mem;
00135 } while (ec_atomic_cas(mem, last+1, last) != last);
00136 return ++last;
00137 }
00138 # endif
00139 # ifndef ec_atomic_inc64
00140
00141
00142 # define ec_atomic_inc64(a) (*a)++
00143 # endif
00144
00145 #endif
00146
00147 #define P2PHASE(x, align) ((x) & ((align) - 1))
00148 #define P2ALIGN(x, align) ((x) & -(align))
00149 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
00150 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
00151 #define P2END(x, align) (-(~(x) & -(align)))
00152 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
00153 #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
00154 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
00155 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
00156 #define ISP2(x) (((x) & ((x) - 1)) == 0)
00157
00158
00159 #define atomic_add_64(lvalptr, delta) ec_atomic_inc64(lvalptr)
00160 #define atomic_add_32_nv(a, b) ec_atomic_inc(a)
00161
00162 #ifndef NANOSEC
00163 #define NANOSEC 1000000000
00164 #endif
00165
00166 #ifdef _WIN32
00167 #define issetugid() 0
00168 #elif !HAVE_ISSETUGID
00169 #define issetugid() (geteuid() == 0)
00170 #endif
00171
00172 #define _sysconf(a) sysconf(a)
00173 #define __NORETURN __attribute__ ((noreturn))
00174
00175 #define EC_UMEM_DUMMY_PCSTACK 1
00176 static INLINE int __nthreads(void)
00177 {
00178
00179 return 2;
00180 }
00181
00182 #if (SIZEOF_VOID_P == 8)
00183 # define _LP64 1
00184 #endif
00185
00186 #ifndef MIN
00187 # define MIN(a,b) ((a) < (b) ? (a) : (b))
00188 #endif
00189 #ifndef MAX
00190 # define MAX(a,b) ((a) > (b) ? (a) : (b))
00191 #endif
00192
00193
00194 #endif