Initial Linux ZFS GIT Repo
[zfs.git] / zfs / lib / libumem / sol_compat.h
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 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 # elif defined(__sparc__) && defined(__GNUC__)
128 static INLINE uint_t ec_atomic_cas(uint_t *mem, uint_t with, uint_t cmp)
129 {
130   __asm volatile ("cas [%3],%2,%0"
131         : "+r"(with), "=m"(*(mem))
132         : "r"(cmp), "r"(mem), "m"(*(mem)) );
133   return with;
134 }
135 # endif
136
137 # ifndef ec_atomic_inc
138 static INLINE uint_t ec_atomic_inc(uint_t *mem)
139 {
140   register uint_t last;
141   do {
142     last = *mem;
143   } while (ec_atomic_cas(mem, last+1, last) != last);
144   return ++last;
145 }
146 # endif
147 # ifndef ec_atomic_inc64
148    /* yeah, it's not great.  It's only used to bump failed allocation
149     * counts, so it is not critical right now. */
150 #  define ec_atomic_inc64(a)  (*a)++
151 # endif
152
153 #endif
154
155 #define P2PHASE(x, align)    ((x) & ((align) - 1))
156 #define P2ALIGN(x, align)    ((x) & -(align))
157 #define P2NPHASE(x, align)    (-(x) & ((align) - 1))
158 #define P2ROUNDUP(x, align)   (-(-(x) & -(align)))
159 #define P2END(x, align)     (-(~(x) & -(align)))
160 #define P2PHASEUP(x, align, phase)  ((phase) - (((phase) - (x)) & -(align)))
161 #define P2CROSS(x, y, align)    (((x) ^ (y)) > (align) - 1)
162 #define P2SAMEHIGHBIT(x, y)    (((x) ^ (y)) < ((x) & (y)))
163 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
164 #define ISP2(x)    (((x) & ((x) - 1)) == 0)
165
166 /* beware! umem only uses these atomic adds for incrementing by 1 */
167 #define atomic_add_64(lvalptr, delta) ec_atomic_inc64(lvalptr)
168 #define atomic_add_32_nv(a, b)            ec_atomic_inc(a) 
169
170 #ifndef NANOSEC
171 #define NANOSEC 1000000000
172 #endif
173
174 #ifdef _WIN32
175 #define issetugid()               0
176 #elif !defined(__FreeBSD__)
177 #define issetugid()       (geteuid() == 0)
178 #endif
179
180 #define _sysconf(a) sysconf(a)
181 #define __NORETURN  __attribute__ ((noreturn))
182
183 #define EC_UMEM_DUMMY_PCSTACK 1
184 static INLINE int __nthreads(void)
185 {
186   /* or more; just to force multi-threaded mode */
187   return 2;
188 }
189
190 #if (SIZEOF_VOID_P == 8)
191 # define _LP64 1
192 #endif
193
194 #ifndef MIN
195 # define MIN(a,b) ((a) < (b) ? (a) : (b))
196 #endif
197 #ifndef MAX
198 # define MAX(a,b) ((a) > (b) ? (a) : (b))
199 #endif
200
201
202 #endif