]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/src/include/libpmemobj/tx.h
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / include / libpmemobj / tx.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright 2014-2020, Intel Corporation */
3
4 /*
5 * libpmemobj/tx.h -- definitions of libpmemobj transactional macros
6 */
7
8 #ifndef LIBPMEMOBJ_TX_H
9 #define LIBPMEMOBJ_TX_H 1
10
11 #include <errno.h>
12 #include <string.h>
13
14 #include <libpmemobj/tx_base.h>
15 #include <libpmemobj/types.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #ifdef POBJ_TX_CRASH_ON_NO_ONABORT
22 #define TX_ONABORT_CHECK do {\
23 if (_stage == TX_STAGE_ONABORT)\
24 abort();\
25 } while (0)
26 #else
27 #define TX_ONABORT_CHECK do {} while (0)
28 #endif
29
30 #define _POBJ_TX_BEGIN(pop, ...)\
31 {\
32 jmp_buf _tx_env;\
33 enum pobj_tx_stage _stage;\
34 int _pobj_errno;\
35 if (setjmp(_tx_env)) {\
36 errno = pmemobj_tx_errno();\
37 } else {\
38 _pobj_errno = pmemobj_tx_begin(pop, _tx_env, __VA_ARGS__,\
39 TX_PARAM_NONE);\
40 if (_pobj_errno)\
41 errno = _pobj_errno;\
42 }\
43 while ((_stage = pmemobj_tx_stage()) != TX_STAGE_NONE) {\
44 switch (_stage) {\
45 case TX_STAGE_WORK:
46
47 #define TX_BEGIN_PARAM(pop, ...)\
48 _POBJ_TX_BEGIN(pop, ##__VA_ARGS__)
49
50 #define TX_BEGIN_LOCK TX_BEGIN_PARAM
51
52 /* Just to let compiler warn when incompatible function pointer is used */
53 static inline pmemobj_tx_callback
54 _pobj_validate_cb_sig(pmemobj_tx_callback cb)
55 {
56 return cb;
57 }
58
59 #define TX_BEGIN_CB(pop, cb, arg, ...) _POBJ_TX_BEGIN(pop, TX_PARAM_CB,\
60 _pobj_validate_cb_sig(cb), arg, ##__VA_ARGS__)
61
62 #define TX_BEGIN(pop) _POBJ_TX_BEGIN(pop, TX_PARAM_NONE)
63
64 #define TX_ONABORT\
65 pmemobj_tx_process();\
66 break;\
67 case TX_STAGE_ONABORT:
68
69 #define TX_ONCOMMIT\
70 pmemobj_tx_process();\
71 break;\
72 case TX_STAGE_ONCOMMIT:
73
74 #define TX_FINALLY\
75 pmemobj_tx_process();\
76 break;\
77 case TX_STAGE_FINALLY:
78
79 #define TX_END\
80 pmemobj_tx_process();\
81 break;\
82 default:\
83 TX_ONABORT_CHECK;\
84 pmemobj_tx_process();\
85 break;\
86 }\
87 }\
88 _pobj_errno = pmemobj_tx_end();\
89 if (_pobj_errno)\
90 errno = _pobj_errno;\
91 }
92
93 #define TX_ADD(o)\
94 pmemobj_tx_add_range((o).oid, 0, sizeof(*(o)._type))
95
96 #define TX_ADD_FIELD(o, field)\
97 TX_ADD_DIRECT(&(D_RO(o)->field))
98
99 #define TX_ADD_DIRECT(p)\
100 pmemobj_tx_add_range_direct(p, sizeof(*(p)))
101
102 #define TX_ADD_FIELD_DIRECT(p, field)\
103 pmemobj_tx_add_range_direct(&(p)->field, sizeof((p)->field))
104
105 #define TX_XADD(o, flags)\
106 pmemobj_tx_xadd_range((o).oid, 0, sizeof(*(o)._type), flags)
107
108 #define TX_XADD_FIELD(o, field, flags)\
109 TX_XADD_DIRECT(&(D_RO(o)->field), flags)
110
111 #define TX_XADD_DIRECT(p, flags)\
112 pmemobj_tx_xadd_range_direct(p, sizeof(*(p)), flags)
113
114 #define TX_XADD_FIELD_DIRECT(p, field, flags)\
115 pmemobj_tx_xadd_range_direct(&(p)->field, sizeof((p)->field), flags)
116
117 #define TX_NEW(t)\
118 ((TOID(t))pmemobj_tx_alloc(sizeof(t), TOID_TYPE_NUM(t)))
119
120 #define TX_ALLOC(t, size)\
121 ((TOID(t))pmemobj_tx_alloc(size, TOID_TYPE_NUM(t)))
122
123 #define TX_ZNEW(t)\
124 ((TOID(t))pmemobj_tx_zalloc(sizeof(t), TOID_TYPE_NUM(t)))
125
126 #define TX_ZALLOC(t, size)\
127 ((TOID(t))pmemobj_tx_zalloc(size, TOID_TYPE_NUM(t)))
128
129 #define TX_XALLOC(t, size, flags)\
130 ((TOID(t))pmemobj_tx_xalloc(size, TOID_TYPE_NUM(t), flags))
131
132 /* XXX - not available when compiled with VC++ as C code (/TC) */
133 #if !defined(_MSC_VER) || defined(__cplusplus)
134 #define TX_REALLOC(o, size)\
135 ((__typeof__(o))pmemobj_tx_realloc((o).oid, size, TOID_TYPE_NUM_OF(o)))
136
137 #define TX_ZREALLOC(o, size)\
138 ((__typeof__(o))pmemobj_tx_zrealloc((o).oid, size, TOID_TYPE_NUM_OF(o)))
139 #endif /* !defined(_MSC_VER) || defined(__cplusplus) */
140
141 #define TX_STRDUP(s, type_num)\
142 pmemobj_tx_strdup(s, type_num)
143
144 #define TX_XSTRDUP(s, type_num, flags)\
145 pmemobj_tx_xstrdup(s, type_num, flags)
146
147 #define TX_WCSDUP(s, type_num)\
148 pmemobj_tx_wcsdup(s, type_num)
149
150 #define TX_XWCSDUP(s, type_num, flags)\
151 pmemobj_tx_xwcsdup(s, type_num, flags)
152
153 #define TX_FREE(o)\
154 pmemobj_tx_free((o).oid)
155
156 #define TX_XFREE(o, flags)\
157 pmemobj_tx_xfree((o).oid, flags)
158
159 #define TX_SET(o, field, value) (\
160 TX_ADD_FIELD(o, field),\
161 D_RW(o)->field = (value))
162
163 #define TX_SET_DIRECT(p, field, value) (\
164 TX_ADD_FIELD_DIRECT(p, field),\
165 (p)->field = (value))
166
167 static inline void *
168 TX_MEMCPY(void *dest, const void *src, size_t num)
169 {
170 pmemobj_tx_add_range_direct(dest, num);
171 return memcpy(dest, src, num);
172 }
173
174 static inline void *
175 TX_MEMSET(void *dest, int c, size_t num)
176 {
177 pmemobj_tx_add_range_direct(dest, num);
178 return memset(dest, c, num);
179 }
180
181 #ifdef __cplusplus
182 }
183 #endif
184
185 #endif /* libpmemobj/tx.h */