xref: /6.0.3/forestdb/src/arch.h (revision 99e2edb2)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2010 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#ifndef _JSAHN_ARCH_H
19#define _JSAHN_ARCH_H
20
21#ifndef __STDC_FORMAT_MACROS
22#define __STDC_FORMAT_MACROS
23#endif
24
25#include <stdio.h>
26#include <fcntl.h>
27
28#include "forestdb_endian.h"
29/* Large File Support */
30#define _LARGE_FILE 1
31#ifndef _FILE_OFFSET_BITS
32#  define _FILE_OFFSET_BITS 64
33#elif (_FILE_OFFSET_BITS != 64)
34#error "bad things"
35#endif
36#define _LARGEFILE_SOURCE 1
37#ifndef O_LARGEFILE
38# define O_LARGEFILE 0
39#endif
40
41#ifndef _MSC_VER
42#include <stdbool.h>
43#else
44#ifndef __cplusplus
45#pragma once
46#define false (0)
47#define true (1)
48#define bool int
49#endif
50#endif
51
52#ifdef HAVE_JEMALLOC
53#ifdef WIN32
54/* jemalloc.h tries to include strings.h, but on win32
55   that is our own "hacked" version that provides stuff
56   we use elsewhere in the system. By including that
57   file you might end up having to link with the
58   platform lib which supplies a lot of the functions
59   forestdb also provides an implementation of (but
60   with a different linkage)
61*/
62#define STRINGS_H
63#endif
64
65#include <jemalloc/jemalloc.h>
66
67#ifdef WIN32
68#undef STRINGS_H
69#endif
70
71#undef malloc
72#undef calloc
73#undef realloc
74#undef free
75#undef posix_memalign
76#undef memalign
77#undef aligned_malloc
78#undef aligned_free
79
80#define malloc(size) je_malloc(size)
81#define calloc(nmemb, size) je_calloc(nmemb, size)
82#define realloc(ptr, size) je_realloc(ptr, size)
83#define free(addr) je_free(addr)
84#define posix_memalign(memptr, alignment, size) \
85        je_posix_memalign(memptr, alignment, size)
86#define memalign(alignment, size) je_memalign(alignment, size)
87#define aligned_malloc(size, align) je_aligned_malloc(size, align)
88#define aligned_free(addr) je_aligned_free(addr)
89#endif //HAVE_JEMALLOC
90
91#ifdef __APPLE__
92    #include <inttypes.h>
93    #include <alloca.h>
94    #include <TargetConditionals.h>
95    #include <AvailabilityMacros.h>
96
97    #define INLINE extern inline
98
99    #define _X64 "llx"
100    #define _F64 "lld"
101    #define _FSEC "ld"
102    #define _FUSEC "d"
103
104    #define _ARCH_O_DIRECT (0x0)
105
106    #if TARGET_CPU_ARM
107    #define _ALIGN_MEM_ACCESS
108    #endif
109
110    #define malloc_align(addr, align, size)                     \
111        {if ( 0 != posix_memalign(&(addr), (align), (size)) ) { \
112            (addr) = NULL;                                      \
113         } }
114    #define free_align(addr) free(addr)
115
116    #ifndef spin_t
117        // spinlock
118        #include <libkern/OSAtomic.h>
119        #ifndef MAC_OS_X_VERSION_10_12
120            #define MAC_OS_X_VERSION_10_12 101200
121        #endif
122        #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
123            #include <os/lock.h>
124            #define spin_t os_unfair_lock
125            #define spin_lock(arg) os_unfair_lock_lock(arg)
126            #define spin_trylock(arg) os_unfair_lock_trylock(arg)
127            #define spin_unlock(arg) os_unfair_lock_unlock(arg)
128            #define SPIN_INITIALIZER OS_UNFAIR_LOCK_INIT
129            #define spin_init(arg) *(arg) = OS_UNFAIR_LOCK_INIT
130        #else
131            #define spin_t OSSpinLock
132            #define spin_lock(arg) OSSpinLockLock(arg)
133            #define spin_trylock(arg) OSSpinLockTry(arg)
134            #define spin_unlock(arg) OSSpinLockUnlock(arg)
135            #define SPIN_INITIALIZER (spin_t)(0)
136            #define spin_init(arg) *(arg) = (spin_t)(0)
137        #endif
138        #define spin_destroy(arg)
139    #endif
140    #ifndef mutex_t
141        // mutex
142        #include <pthread.h>
143        #define mutex_t pthread_mutex_t
144        #define mutex_init(arg) pthread_mutex_init(arg, NULL)
145        #define mutex_lock(arg) pthread_mutex_lock(arg)
146        #define mutex_trylock(arg) \
147            (pthread_mutex_trylock(arg) == 0)
148        #define mutex_unlock(arg) pthread_mutex_unlock(arg)
149        #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
150        #define mutex_destroy(arg) pthread_mutex_destroy(arg)
151    #endif
152    #ifndef thread_t
153            // thread
154        #include <pthread.h>
155        #define thread_t pthread_t
156        #define thread_cond_t pthread_cond_t
157        #define thread_create(tid, func, args) \
158            pthread_create((tid), NULL, (func), (args))
159        #define thread_join(tid, ret) pthread_join(tid, ret)
160        #define thread_cancel(tid) pthread_cancel(tid)
161        #define thread_exit(code) pthread_exit(code)
162        #define thread_cond_init(cond) pthread_cond_init(cond, NULL)
163        #define thread_cond_destroy(cond) pthread_cond_destroy(cond)
164        #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex)
165        #define thread_cond_timedwait(cond, mutex, ms) \
166            { \
167            struct timespec ts = convert_reltime_to_abstime(ms); \
168            pthread_cond_timedwait(cond, mutex, &ts); \
169            }
170        #define thread_cond_signal(cond) pthread_cond_signal(cond)
171        #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond)
172    #endif
173
174#elif __ANDROID__
175    #include <inttypes.h>
176    #include <alloca.h>
177
178    #define INLINE static __inline
179
180    #define _X64 "llx"
181    #define _F64 "lld"
182    #define _FSEC "ld"
183    #define _FUSEC "ld"
184
185    #define _ARCH_O_DIRECT (O_DIRECT)
186    #define malloc_align(addr, align, size) \
187        (addr = memalign((align), (size)))
188    #define free_align(addr) free(addr)
189
190    #ifndef spin_t
191        // spinlock
192        #include <pthread.h>
193        #define spin_t pthread_mutex_t
194        #define spin_init(arg) pthread_mutex_init(arg, NULL)
195        #define spin_lock(arg) pthread_mutex_lock(arg)
196        #define spin_trylock(arg) \
197            (pthread_mutex_trylock(arg) == 0)
198        #define spin_unlock(arg) pthread_mutex_unlock(arg)
199        #define spin_destroy(arg) pthread_mutex_destroy(arg)
200        #define SPIN_INITIALIZER ((spin_t)PTHREAD_MUTEX_INITIALIZER)
201    #endif
202    #ifndef mutex_t
203        // mutex
204        #include <pthread.h>
205        #define mutex_t pthread_mutex_t
206        #define mutex_init(arg) pthread_mutex_init(arg, NULL)
207        #define mutex_lock(arg) pthread_mutex_lock(arg)
208        #define mutex_trylock(arg) \
209            (pthread_mutex_trylock(arg) == 0)
210        #define mutex_unlock(arg) pthread_mutex_unlock(arg)
211        #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
212        #define mutex_destroy(arg) pthread_mutex_destroy(arg)
213    #endif
214    #ifndef thread_t
215        // thread
216        #include <pthread.h>
217        #define thread_t pthread_t
218        #define thread_cond_t pthread_cond_t
219        #define thread_create(tid, func, args) \
220            pthread_create((tid), NULL, (func), (args))
221        #define thread_join(tid, ret) pthread_join(tid, ret)
222        #define thread_cancel(tid) pthread_cancel(tid)
223        #define thread_exit(code) pthread_exit(code)
224        #define thread_cond_init(cond) pthread_cond_init(cond, NULL)
225        #define thread_cond_destroy(cond) pthread_cond_destroy(cond)
226        #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex)
227        #define thread_cond_timedwait(cond, mutex, ms) \
228            { \
229            struct timespec ts = convert_reltime_to_abstime(ms); \
230            pthread_cond_timedwait(cond, mutex, &ts); \
231            }
232        #define thread_cond_signal(cond) pthread_cond_signal(cond)
233        #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond)
234    #endif
235
236    #ifdef assert
237        #undef assert
238    #endif
239    #define assert(a) (a)
240
241#elif __linux__ && __arm__
242    #include <inttypes.h>
243    #include <alloca.h>
244
245    #define INLINE static __inline
246
247    #define _X64 "llx"
248    #define _F64 "lld"
249    #define _FSEC "ld"
250    #define _FUSEC "ld"
251
252    #define _ARCH_O_DIRECT (O_DIRECT)
253    #define malloc_align(addr, align, size) \
254        (addr = memalign((align), (size)))
255    #define free_align(addr) free(addr)
256
257    #ifndef spin_t
258        // spinlock
259        #include <pthread.h>
260        #define spin_t pthread_spinlock_t
261        #define spin_init(arg) pthread_spin_init(arg, PTHREAD_PROCESS_SHARED)
262        #define spin_lock(arg) pthread_spin_lock(arg)
263        #define spin_trylock(arg) \
264            (pthread_spin_trylock(arg) == 0)
265        #define spin_unlock(arg) pthread_spin_unlock(arg)
266        #define spin_destroy(arg) pthread_spin_destroy(arg)
267        #define SPIN_INITIALIZER (spin_t)(0)
268    #endif
269    #ifndef mutex_t
270        // mutex
271        #include <pthread.h>
272        #define mutex_t pthread_mutex_t
273        #define mutex_init(arg) pthread_mutex_init(arg, NULL)
274        #define mutex_lock(arg) pthread_mutex_lock(arg)
275        #define mutex_trylock(arg) \
276            (pthread_mutex_trylock(arg) == 0)
277        #define mutex_unlock(arg) pthread_mutex_unlock(arg)
278        #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
279        #define mutex_destroy(arg) pthread_mutex_destroy(arg)
280    #endif
281    #ifndef thread_t
282        // thread
283        #include <pthread.h>
284        #define thread_t pthread_t
285        #define thread_cond_t pthread_cond_t
286        #define thread_create(tid, func, args) \
287            pthread_create((tid), NULL, (func), (args))
288        #define thread_join(tid, ret) pthread_join(tid, ret)
289        #define thread_cancel(tid) pthread_cancel(tid)
290        #define thread_exit(code) pthread_exit(code)
291        #define thread_cond_init(cond) pthread_cond_init(cond, NULL)
292        #define thread_cond_destroy(cond) pthread_cond_destroy(cond)
293        #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex)
294        #define thread_cond_timedwait(cond, mutex, ms) \
295            { \
296            struct timespec ts = convert_reltime_to_abstime(ms); \
297            pthread_cond_timedwait(cond, mutex, &ts); \
298            }
299        #define thread_cond_signal(cond) pthread_cond_signal(cond)
300        #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond)
301    #endif
302
303#elif __linux__
304    #include <inttypes.h>
305    #include <alloca.h>
306
307    #define INLINE __inline
308
309    #define _X64 PRIx64
310    #define _F64 PRIu64
311    #define _FSEC "ld"
312    #define _FUSEC "ld"
313
314    #define _ARCH_O_DIRECT (O_DIRECT)
315
316    #define malloc_align(addr, align, size)                     \
317        {if ( 0 != posix_memalign(&(addr), (align), (size)) ) { \
318            (addr) = NULL;                                      \
319         } }
320    #define free_align(addr) free(addr)
321
322    #ifndef spin_t
323        // spinlock
324        #include <pthread.h>
325        #define spin_t pthread_spinlock_t
326        #define spin_init(arg) pthread_spin_init(arg, PTHREAD_PROCESS_SHARED)
327        #define spin_lock(arg) pthread_spin_lock(arg)
328        #define spin_trylock(arg) \
329            (pthread_spin_trylock(arg) == 0)
330        #define spin_unlock(arg) pthread_spin_unlock(arg)
331        #define spin_destroy(arg) pthread_spin_destroy(arg)
332        #define SPIN_INITIALIZER (spin_t)(1)
333    #endif
334    #ifndef mutex_t
335        // mutex
336        #include <pthread.h>
337        #define mutex_t pthread_mutex_t
338        #define mutex_init(arg) pthread_mutex_init(arg, NULL)
339        #define mutex_lock(arg) pthread_mutex_lock(arg)
340        #define mutex_trylock(arg) \
341            (pthread_mutex_trylock(arg) == 0)
342        #define mutex_unlock(arg) pthread_mutex_unlock(arg)
343        #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
344        #define mutex_destroy(arg) pthread_mutex_destroy(arg)
345    #endif
346    #ifndef thread_t
347        // thread
348        #include <pthread.h>
349        #define thread_t pthread_t
350        #define thread_cond_t pthread_cond_t
351        #define thread_create(tid, func, args) \
352            pthread_create((tid), NULL, (func), (args))
353        #define thread_join(tid, ret) pthread_join(tid, ret)
354        #define thread_cancel(tid) pthread_cancel(tid)
355        #define thread_exit(code) pthread_exit(code)
356        #define thread_cond_init(cond) pthread_cond_init(cond, NULL)
357        #define thread_cond_destroy(cond) pthread_cond_destroy(cond)
358        #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex)
359        #define thread_cond_timedwait(cond, mutex, ms) \
360            { \
361            struct timespec ts = convert_reltime_to_abstime(ms); \
362            pthread_cond_timedwait(cond, mutex, &ts); \
363            }
364        #define thread_cond_signal(cond) pthread_cond_signal(cond)
365        #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond)
366    #endif
367
368#elif defined(WIN32) || defined(_WIN32)
369    // mingw compatiable
370
371    #define _FSEC "ld"
372    #define _FUSEC "ld"
373
374    #define _ARCH_O_DIRECT (0x0)
375
376#ifdef _MSC_VER
377    // visual studio CL compiler
378    #include <Windows.h>
379    #include "gettimeofday_vs.h"
380    #define INLINE static inline
381    //#define alloca(size) _alloca(size)
382    #define _X64 "llx"
383    #define _F64 "llu"
384#ifndef _CRT_SECURE_NO_WARNINGS
385    #define _CRT_SECURE_NO_WARNINGS
386#endif
387    #define gettimeofday gettimeofday_vs
388    #define sleep(sec) Sleep((sec)*1000)
389    typedef unsigned long mode_t;
390    #include <BaseTsd.h>
391    typedef SSIZE_T ssize_t;
392#else
393    #include <inttypes.h>
394    #include <windows.h>
395    #define _X64 PRIx64
396    #define _F64 PRIu64
397    #define INLINE __inline
398#endif
399    #include <stdint.h>
400    #include <stdlib.h>
401    #define malloc_align(addr, align, size) \
402        (addr = (void*)_aligned_malloc((size), (align)))
403    #define free_align(addr) _aligned_free(addr)
404
405    #ifndef spin_t
406        // spinlock
407        #define spin_t CRITICAL_SECTION
408        #define spin_init(arg) InitializeCriticalSection(arg)
409        #define spin_lock(arg) EnterCriticalSection(arg)
410        #define spin_trylock(arg) TryEnterCriticalSection(arg)
411        #define spin_unlock(arg) LeaveCriticalSection(arg)
412        #define spin_destroy(arg) DeleteCriticalSection(arg)
413    #endif
414    #ifndef mutex_t
415        // mutex
416        #define mutex_t CRITICAL_SECTION
417        #define mutex_init(arg) InitializeCriticalSection(arg)
418        #define mutex_lock(arg) EnterCriticalSection(arg)
419        #define mutex_trylock(arg) TryEnterCriticalSection(arg)
420        #define mutex_unlock(arg) LeaveCriticalSection(arg)
421        #define mutex_destroy(arg) DeleteCriticalSection(arg)
422    #endif
423    #ifndef thread_t
424        // thread
425        #define thread_t HANDLE
426        #define thread_cond_t CONDITION_VARIABLE
427        #define thread_create(tid, func, args) \
428            { \
429            DWORD __dt__; \
430            *(tid) = CreateThread(NULL, 0, \
431                (LPTHREAD_START_ROUTINE)(func), (args), 0, &__dt__); \
432            }
433        #define thread_join(tid, ret) WaitForSingleObject(tid, INFINITE)
434        #define thread_cancel(tid) TerminateThread(tid, 0);
435        #define thread_exit(code) ExitThread(code)
436        #define thread_cond_init(cond) InitializeConditionVariable(cond)
437        #define thread_cond_destroy(cond) (void)cond
438        #define thread_cond_wait(cond, mutex) SleepConditionVariableCS(cond, mutex, INFINITE)
439        #define thread_cond_timedwait(cond, mutex, msec) \
440            SleepConditionVariableCS(cond, mutex, msec)
441        #define thread_cond_signal(cond) WakeConditionVariable(cond)
442        #define thread_cond_broadcast(cond) WakeAllConditionVariable(cond)
443    #endif
444
445#elif __CYGWIN__
446    // cygwin compatiable
447    #include <inttypes.h>
448    #include <alloca.h>
449
450    #define INLINE __inline
451
452    #define _X64 PRIx64
453    #define _F64 PRIu64
454    #define _FSEC "ld"
455    #define _FUSEC "ld"
456
457    #define _ARCH_O_DIRECT (0x0)
458
459    #define malloc_align(addr, align, size) \
460        (addr = (void *)memalign((align), (size)))
461    #define free_align(addr) free(addr)
462
463    #ifndef spin_t
464        // spinlock
465        #include <pthread.h>
466        #define spin_t pthread_spinlock_t
467        #define spin_init(arg) pthread_spin_init(arg, PTHREAD_PROCESS_SHARED)
468        #define spin_lock(arg) pthread_spin_lock(arg)
469        #define spin_trylock(arg) \
470            (pthread_spin_trylock(arg) == 0)
471        #define spin_unlock(arg) pthread_spin_unlock(arg)
472        #define spin_destroy(arg) pthread_spin_destroy(arg)
473        #define SPIN_INITIALIZER (spin_t)(1)
474    #endif
475    #ifndef mutex_t
476        // mutex
477        #include <pthread.h>
478        #define mutex_t pthread_mutex_t
479        #define mutex_init(arg) pthread_mutex_init(arg, NULL)
480        #define mutex_lock(arg) pthread_mutex_lock(arg)
481        #define mutex_trylock(arg) \
482            (pthread_mutex_trylock(arg) == 0)
483        #define mutex_unlock(arg) pthread_mutex_unlock(arg)
484        #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
485        #define mutex_destroy(arg) pthread_mutex_destroy(arg)
486    #endif
487    #ifndef thread_t
488        // thread
489        #include <pthread.h>
490        #define thread_t pthread_t
491        #define thread_cond_t pthread_cond_t
492        #define thread_create(tid, func, args) \
493            pthread_create((tid), NULL, (func), (args))
494        #define thread_join(tid, ret) pthread_join(tid, ret)
495        #define thread_cancel(tid) pthread_cancel(tid)
496        #define thread_exit(code) pthread_exit(code)
497        #define thread_cond_init(cond) pthread_cond_init(cond, NULL)
498        #define thread_cond_destroy(cond) pthread_cond_destroy(cond)
499        #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex)
500        #define thread_cond_timedwait(cond, mutex, ms) \
501            { \
502            struct timespec ts = convert_reltime_to_abstime(ms); \
503            pthread_cond_timedwait(cond, mutex, &ts); \
504            }
505        #define thread_cond_signal(cond) pthread_cond_signal(cond)
506        #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond)
507    #endif
508
509#elif __sun
510    #include <inttypes.h>
511    #include <alloca.h>
512
513    #define INLINE __inline
514
515    #define _X64 PRIx64
516    #define _F64 PRIu64
517    #define _FSEC "ld"
518    #define _FUSEC "ld"
519
520    /* Solaris don't have flag to open to set direct io, but
521       rather use directio() afterwards to enable it. lets look
522       into that later on.
523    */
524    #define _ARCH_O_DIRECT (0)
525
526    #define malloc_align(addr, align, size)                     \
527        {if ( 0 != posix_memalign(&(addr), (align), (size)) ) { \
528            (addr) = NULL;                                      \
529         } }
530    #define free_align(addr) free(addr)
531
532    #ifndef spin_t
533        // spinlock
534        // There isn't much point of keeping a separate
535        // spinlock datatype, because the mutexes on
536        // solaris is adaptive anyway and will spin
537        // initially.
538        #include <pthread.h>
539        #define spin_t pthread_mutex_t
540        #define spin_init(arg) pthread_mutex_init(arg, NULL)
541        #define spin_lock(arg) pthread_mutex_lock(arg)
542        #define spin_trylock(arg) \
543            (pthread_mutex_trylock(arg) == 0)
544        #define spin_unlock(arg) pthread_mutex_unlock(arg)
545        #define spin_destroy(arg) pthread_mutex_destroy(arg)
546        #define SPIN_INITIALIZER PTHREAD_MUTEX_INITIALIZER
547    #endif
548    #ifndef mutex_t
549        // mutex
550        #include <pthread.h>
551        #define mutex_t pthread_mutex_t
552        #define mutex_init(arg) pthread_mutex_init(arg, NULL)
553        #define mutex_lock(arg) pthread_mutex_lock(arg)
554        #define mutex_trylock(arg) \
555            (pthread_mutex_trylock(arg) == 0)
556        #define mutex_unlock(arg) pthread_mutex_unlock(arg)
557        #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
558        #define mutex_destroy(arg) pthread_mutex_destroy(arg)
559    #endif
560    #ifndef thread_t
561        // thread
562        #include <pthread.h>
563        #define thread_t pthread_t
564        #define thread_cond_t pthread_cond_t
565        #define thread_create(tid, func, args) \
566            pthread_create((tid), NULL, (func), (args))
567        #define thread_join(tid, ret) pthread_join(tid, ret)
568        #define thread_cancel(tid) pthread_cancel(tid)
569        #define thread_exit(code) pthread_exit(code)
570        #define thread_cond_init(cond) pthread_cond_init(cond, NULL)
571        #define thread_cond_destroy(cond) pthread_cond_destroy(cond)
572        #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex)
573        #define thread_cond_timedwait(cond, mutex, ms) \
574            { \
575            struct timespec ts = convert_reltime_to_abstime(ms); \
576            pthread_cond_timedwait(cond, mutex, &ts); \
577            }
578        #define thread_cond_signal(cond) pthread_cond_signal(cond)
579        #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond)
580    #endif
581
582#elif __FreeBSD__
583    #include <inttypes.h>
584
585    #define INLINE __inline
586
587    #define _X64 PRIx64
588    #define _F64 PRIu64
589    #define _FSEC "ld"
590    #define _FUSEC "ld"
591
592    #define _ARCH_O_DIRECT (O_DIRECT)
593
594    #define malloc_align(addr, align, size)                     \
595        {if ( 0 != posix_memalign(&(addr), (align), (size)) ) { \
596            (addr) = NULL;                                      \
597         } }
598    #define free_align(addr) free(addr)
599
600    #ifndef spin_t
601        // spinlock
602        #include <pthread.h>
603        #define spin_t pthread_mutex_t
604        #define spin_init(arg) pthread_mutex_init(arg, NULL)
605        #define spin_lock(arg) pthread_mutex_lock(arg)
606        #define spin_trylock(arg) \
607            (pthread_mutex_trylock(arg) == 0)
608        #define spin_unlock(arg) pthread_mutex_unlock(arg)
609        #define spin_destroy(arg) pthread_mutex_destroy(arg)
610        #define SPIN_INITIALIZER PTHREAD_MUTEX_INITIALIZER
611    #endif
612    #ifndef mutex_t
613        // mutex
614        #include <pthread.h>
615        #define mutex_t pthread_mutex_t
616        #define mutex_init(arg) pthread_mutex_init(arg, NULL)
617        #define mutex_lock(arg) pthread_mutex_lock(arg)
618        #define mutex_trylock(arg) \
619            (pthread_mutex_trylock(arg) == 0)
620        #define mutex_unlock(arg) pthread_mutex_unlock(arg)
621        #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
622        #define mutex_destroy(arg) pthread_mutex_destroy(arg)
623    #endif
624    #ifndef thread_t
625        // thread
626        #include <pthread.h>
627        #define thread_t pthread_t
628        #define thread_cond_t pthread_cond_t
629        #define thread_create(tid, func, args) \
630            pthread_create((tid), NULL, (func), (args))
631        #define thread_join(tid, ret) pthread_join(tid, ret)
632        #define thread_cancel(tid) pthread_cancel(tid)
633        #define thread_exit(code) pthread_exit(code)
634        #define thread_cond_init(cond) pthread_cond_init(cond, NULL)
635        #define thread_cond_destroy(cond) pthread_cond_destroy(cond)
636        #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex)
637        #define thread_cond_timedwait(cond, mutex, ms) \
638            { \
639            struct timespec ts = convert_reltime_to_abstime(ms); \
640            pthread_cond_timedwait(cond, mutex, &ts); \
641            }
642        #define thread_cond_signal(cond) pthread_cond_signal(cond)
643        #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond)
644    #endif
645
646
647#else
648#pragma error "Unknown architecture"
649    #define INLINE make_error
650#endif
651
652#endif
653