root/trunk/sol_compat.h

Revision 9, 5.3 kB (checked in by wez, 8 years ago)

make the build experience nicer outside of ecelerity.
Add a simple test app.
Note: we don't compile the malloc replacement component.

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