]> pd.if.org Git - nbds/blob - include/common.h
d8811a9d0d5fa5bb1d9553b047e48dbd1e0ee5fa
[nbds] / include / common.h
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  */
5 #ifndef COMMON_H
6 #define COMMON_H
7
8 #include <stdlib.h>
9 #include <assert.h>
10 #include <limits.h>
11 #include <string.h>
12 #include <sys/types.h>
13
14 #define MAX_NUM_THREADS  4 // make this whatever you want, but make it a power of 2
15
16 #define CACHE_LINE_SIZE  64 // 64 byte cache line on x86 and x86-64
17 #define CACHE_LINE_SCALE 6  // log base 2 of the cache line size
18
19 #define EXPECT_TRUE(x)      __builtin_expect(!!(x), 1)
20 #define EXPECT_FALSE(x)     __builtin_expect(!!(x), 0)
21
22 #define SYNC_SWAP           __sync_lock_test_and_set
23 #define SYNC_CAS            __sync_val_compare_and_swap
24 #define SYNC_ADD            __sync_add_and_fetch
25 #define SYNC_FETCH_AND_OR   __sync_fetch_and_or
26
27 #define COUNT_TRAILING_ZEROS __builtin_ctz
28
29 #define MASK(n)     ((1ULL << (n)) - 1)
30
31 #define TRUE  1
32 #define FALSE 0
33
34 #ifdef NBD32
35 #define TAG1         (1U << 31)
36 #define TAG2         (1U << 30)
37 #else
38 #define TAG1         (1ULL << 63)
39 #define TAG2         (1ULL << 62)
40 #endif
41 #define TAG_VALUE(v, tag) ((v) |  tag)
42 #define IS_TAGGED(v, tag) ((v) &  tag)
43 #define STRIP_TAG(v, tag) ((v) & ~tag)
44
45 #define DOES_NOT_EXIST 0
46 #define ERROR_INVALID_OPTION      (-1)
47 #define ERROR_INVALID_ARGUMENT    (-2)
48 #define ERROR_UNSUPPORTED_FEATURE (-3)
49 #define ERROR_TXN_NOT_RUNNING     (-4)
50
51 #define VOLATILE_DEREF(x) (*((volatile typeof(x))(x)))
52
53 typedef unsigned long long uint64_t;
54 typedef unsigned int       uint32_t;
55 typedef unsigned short     uint16_t;
56 typedef unsigned char      uint8_t;
57
58 typedef size_t markable_t;
59
60 static inline uint64_t rdtsc (void) {
61     unsigned l, u;
62     __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (u));
63     return ((uint64_t)u << 32) | l;
64 }
65
66 #include "lwt.h"
67 #endif //COMMON_H