Remove prefix snapshot from snapshot.

See previous commit on shared pointer.
This commit is contained in:
Patrick Pelissier committed 2017-07-25 22:44:17 +02:00
1 parent 62e2024658
commit 8a716cd4f4
3 files changed
+130 -129

No files matched your search

+2 -2
View File
@@ -1698,7 +1698,7 @@ This header is for created snapshots.
#### SNAPSHOT\_DEF(name, type[, oplist]) #### SNAPSHOT\_DEF(name, type[, oplist])
Define the snapshot 'snapshot\_##name##\_t' and its associated methods as "static inline" functions. Define the snapshot 'name##\_t' and its associated methods as "static inline" functions.
A snapshot is a mechanism to allows a consumer thread and a produce thread, **working at different speeds**, to exchange messages in a reliable and thread safe way (the message is passed atomatically). A snapshot is a mechanism to allows a consumer thread and a produce thread, **working at different speeds**, to exchange messages in a reliable and thread safe way (the message is passed atomatically).
In practice, it is implemented using a triple buffer. In practice, it is implemented using a triple buffer.
@@ -1710,7 +1710,7 @@ The object oplist is expected to have the following operators (INIT, INIT\_SET,
Example: Example:
SNAPSHOT_DEF(uint, unsigned int) SNAPSHOT_DEF(snapshot_uint, unsigned int)
void f(unsigned int * i) { void f(unsigned int * i) {
snapshot_uint_t t; snapshot_uint_t t;
snapshot_uint_init(t); snapshot_uint_init(t);
+126 -125
View File
@@ -48,16 +48,16 @@
/********************************** INTERNAL ************************************/ /********************************** INTERNAL ************************************/
/* Define the oplist of a snapshot */ /* Define the oplist of a snapshot */
#define SNAPSHOTI_OPLIST(name, oplist) \ #define SNAPSHOTI_OPLIST(name, oplist) \
(INIT(M_C3(snapshot_, name, _init)), \ (INIT(M_C(name, _init)), \
INIT_SET(M_C3(snapshot_, name, _init_set)), \ INIT_SET(M_C(name, _init_set)), \
SET(M_C3(snapshot_, name, _set)), \ SET(M_C(name, _set)), \
CLEAR(M_C3(snapshot_, name, _clear)), \ CLEAR(M_C(name, _clear)), \
TYPE(M_C3(snapshot_, name, _t)), \ TYPE(M_C(name, _t)), \
SUBTYPE(M_C3(snapshot_name_, name, _t)) \ SUBTYPE(M_C(name_, type_t)) \
,OPLIST(oplist) \ ,OPLIST(oplist) \
,M_IF_METHOD(INIT_MOVE, oplist)(INIT_MOVE(M_C3(snapshot_, name, _init_move)),) \ ,M_IF_METHOD(INIT_MOVE, oplist)(INIT_MOVE(M_C(name, _init_move)),) \
,M_IF_METHOD(MOVE, oplist)(MOVE(M_C3(snapshot_, name, _move)),) \ ,M_IF_METHOD(MOVE, oplist)(MOVE(M_C(name, _move)),) \
) )
/* Flag defining the atomic state of a snapshot: /* Flag defining the atomic state of a snapshot:
@@ -66,7 +66,7 @@
* - f: Next index of the write buffer when a shot is taken Range [0..2] * - f: Next index of the write buffer when a shot is taken Range [0..2]
* - b: Boolean indicating that the read buffer shall be updated * - b: Boolean indicating that the read buffer shall be updated
*/ */
#define SNAPSHOTI_FLAG(r, w, f, b) \ #define SNAPSHOTI_FLAG(r, w, f, b) \
((unsigned char)( ( (r) << 4) | ((w) << 2) | ((f)) | ((b) << 6))) ((unsigned char)( ( (r) << 4) | ((w) << 2) | ((f)) | ((b) << 6)))
#define SNAPSHOTI_R(flags) \ #define SNAPSHOTI_R(flags) \
(((flags) >> 4) & 0x03u) (((flags) >> 4) & 0x03u)
@@ -90,127 +90,128 @@
} while (0) } while (0)
#define SNAPSHOTI_DEF2(name, type, oplist) \ #define SNAPSHOTI_DEF2(name, type, oplist) \
typedef struct M_C3(snapshot_, name, _s) { \ \
typedef struct M_C(name, _s) { \
type data[3]; \ type data[3]; \
atomic_uchar flags; \ atomic_uchar flags; \
} M_C3(snapshot_, name, _t)[1]; \ } M_C(name, _t)[1]; \
typedef type M_C3(snapshot_type_, name, _t); \ typedef type M_C(name, _type_t); \
\ \
static inline void M_C3(snapshot_, name, _init)(M_C3(snapshot_, name, _t) snap) \ static inline void M_C(name, _init)(M_C(name, _t) snap) \
{ \ { \
assert(snap != NULL); \ assert(snap != NULL); \
for(int i = 0; i < 3; i++) { \ for(int i = 0; i < 3; i++) { \
M_GET_INIT oplist(snap->data[i]); \ M_GET_INIT oplist(snap->data[i]); \
} \ } \
atomic_init (&snap->flags, SNAPSHOTI_FLAG(0, 1, 2, 0)); \ atomic_init (&snap->flags, SNAPSHOTI_FLAG(0, 1, 2, 0)); \
SNAPSHOTI_CONTRACT(snap); \ SNAPSHOTI_CONTRACT(snap); \
} \ } \
\ \
static inline void M_C3(snapshot_, name, _clear)(M_C3(snapshot_, name, _t) snap) \ static inline void M_C(name, _clear)(M_C(name, _t) snap) \
{ \ { \
SNAPSHOTI_CONTRACT(snap); \ SNAPSHOTI_CONTRACT(snap); \
for(int i = 0; i < 3; i++) { \ for(int i = 0; i < 3; i++) { \
M_GET_CLEAR oplist(snap->data[i]); \ M_GET_CLEAR oplist(snap->data[i]); \
} \ } \
} \ } \
\ \
static inline void M_C3(snapshot_, name, _init_set)(M_C3(snapshot_, name, _t) snap, \ static inline void M_C(name, _init_set)(M_C(name, _t) snap, \
M_C3(snapshot_, name, _t) org) \ M_C(name, _t) org) \
{ \ { \
SNAPSHOTI_CONTRACT(org); \ SNAPSHOTI_CONTRACT(org); \
assert(snap != NULL && snap != org); \ assert(snap != NULL && snap != org); \
for(int i = 0; i < 3; i++) { \ for(int i = 0; i < 3; i++) { \
M_GET_INIT_SET oplist(snap->data[i], org->data[i]); \ M_GET_INIT_SET oplist(snap->data[i], org->data[i]); \
} \ } \
atomic_init (&snap->flags, atomic_load(&org->flags)); \ atomic_init (&snap->flags, atomic_load(&org->flags)); \
SNAPSHOTI_CONTRACT(snap); \ SNAPSHOTI_CONTRACT(snap); \
} \ } \
\ \
static inline void M_C3(snapshot_, name, _set)(M_C3(snapshot_, name, _t) snap, \ static inline void M_C(name, _set)(M_C(name, _t) snap, \
M_C3(snapshot_, name, _t) org) \ M_C(name, _t) org) \
{ \ { \
SNAPSHOTI_CONTRACT(snap); \ SNAPSHOTI_CONTRACT(snap); \
SNAPSHOTI_CONTRACT(org); \ SNAPSHOTI_CONTRACT(org); \
for(int i = 0; i < 3; i++) { \ for(int i = 0; i < 3; i++) { \
M_GET_SET oplist(snap->data[i], org->data[i]); \ M_GET_SET oplist(snap->data[i], org->data[i]); \
} \ } \
atomic_init (&snap->flags, atomic_load(&org->flags)); \ atomic_init (&snap->flags, atomic_load(&org->flags)); \
SNAPSHOTI_CONTRACT(snap); \ SNAPSHOTI_CONTRACT(snap); \
} \ } \
\
M_IF_METHOD(INIT_MOVE, oplist)( \
static inline void M_C(name, _init_move)(M_C(name, _t) snap, \
M_C(name, _t) org) \
{ \
SNAPSHOTI_CONTRACT(org); \
assert(snap != NULL && snap != org); \
for(int i = 0; i < 3; i++) { \
M_GET_INIT_MOVE oplist(snap->data[i], org->data[i]); \
} \
snap->flags = org->flags; \
atomic_init (&org->flags, 0); \
SNAPSHOTI_CONTRACT(snap); \
} \
,) /* IF_METHOD (INIT_MOVE) */ \
\ \
M_IF_METHOD(INIT_MOVE, oplist)( \ M_IF_METHOD(MOVE, oplist)( \
static inline void M_C3(snapshot_, name, _init_move)(M_C3(snapshot_, name, _t) snap, \ static inline void M_C(name, _move)(M_C(name, _t) snap, \
M_C3(snapshot_, name, _t) org) \ M_C(name, _t) org) \
{ \ { \
SNAPSHOTI_CONTRACT(org); \ SNAPSHOTI_CONTRACT(snap); \
assert(snap != NULL && snap != org); \ SNAPSHOTI_CONTRACT(org); \
for(int i = 0; i < 3; i++) { \ assert(snap != org); \
M_GET_INIT_MOVE oplist(snap->data[i], org->data[i]); \ for(int i = 0; i < 3; i++) { \
} \ M_GET_MOVE oplist(snap->data[i], org->data[i]); \
snap->flags = org->flags; \ } \
atomic_init (&org->flags, 0); \ snap->flags = org->flags; \
SNAPSHOTI_CONTRACT(snap); \ atomic_init(&org->flags, 0); \
} \ SNAPSHOTI_CONTRACT(snap); \
,) /* IF_METHOD (INIT_MOVE) */ \ } \
\ ,) /* IF_METHOD (MOVE) */ \
M_IF_METHOD(MOVE, oplist)( \
static inline void M_C3(snapshot_, name, _move)(M_C3(snapshot_, name, _t) snap, \
M_C3(snapshot_, name, _t) org) \
{ \
SNAPSHOTI_CONTRACT(snap); \
SNAPSHOTI_CONTRACT(org); \
assert(snap != org); \
for(int i = 0; i < 3; i++) { \
M_GET_MOVE oplist(snap->data[i], org->data[i]); \
} \
snap->flags = org->flags; \
atomic_init(&org->flags, 0); \
SNAPSHOTI_CONTRACT(snap); \
} \
,) /* IF_METHOD (MOVE) */ \
\
static inline type *M_C3(snapshot_, name, _take)(M_C3(snapshot_, name, _t) snap) \
{ \
SNAPSHOTI_CONTRACT(snap); \
unsigned char nextFlags, origFlags = atomic_load (&snap->flags); \
do { \
nextFlags = SNAPSHOTI_FLAG(SNAPSHOTI_R(origFlags), \
SNAPSHOTI_F(origFlags), \
SNAPSHOTI_W(origFlags), 1); \
} while (!atomic_compare_exchange_weak (&snap->flags, &origFlags, \
nextFlags)); \
return &snap->data[SNAPSHOTI_W(nextFlags)]; \
} \
\ \
static inline const type *M_C3(snapshot_, name, _look)(M_C3(snapshot_, name, _t) snap) \ static inline type *M_C(name, _take)(M_C(name, _t) snap) \
{ \ { \
SNAPSHOTI_CONTRACT(snap); \ SNAPSHOTI_CONTRACT(snap); \
unsigned char nextFlags, origFlags = atomic_load (&snap->flags); \ unsigned char nextFlags, origFlags = atomic_load (&snap->flags); \
do { \ do { \
if (!SNAPSHOTI_B(origFlags)) \ nextFlags = SNAPSHOTI_FLAG(SNAPSHOTI_R(origFlags), \
break; \ SNAPSHOTI_F(origFlags), \
nextFlags = SNAPSHOTI_FLAG(SNAPSHOTI_F(origFlags), \ SNAPSHOTI_W(origFlags), 1); \
SNAPSHOTI_W(origFlags), \ } while (!atomic_compare_exchange_weak (&snap->flags, &origFlags, \
SNAPSHOTI_R(origFlags), 0); \ nextFlags)); \
} while (!atomic_compare_exchange_weak (&snap->flags, &origFlags, \ return &snap->data[SNAPSHOTI_W(nextFlags)]; \
nextFlags)); \ } \
nextFlags = atomic_load(&snap->flags); \ \
return M_CONST_CAST(type, &snap->data[SNAPSHOTI_R(nextFlags)]); \ static inline const type *M_C(name, _look)(M_C(name, _t) snap) \
} \ { \
\ SNAPSHOTI_CONTRACT(snap); \
static inline type *M_C3(snapshot_, name, _get_produced)(M_C3(snapshot_, name, _t) snap) \ unsigned char nextFlags, origFlags = atomic_load (&snap->flags); \
{ \ do { \
SNAPSHOTI_CONTRACT(snap); \ if (!SNAPSHOTI_B(origFlags)) \
unsigned char flags = atomic_load(&snap->flags); \ break; \
return &snap->data[SNAPSHOTI_W(flags)]; \ nextFlags = SNAPSHOTI_FLAG(SNAPSHOTI_F(origFlags), \
} \ SNAPSHOTI_W(origFlags), \
\ SNAPSHOTI_R(origFlags), 0); \
static inline const type *M_C3(snapshot_, name, _get_consummed)(M_C3(snapshot_, name, _t) snap) \ } while (!atomic_compare_exchange_weak (&snap->flags, &origFlags, \
{ \ nextFlags)); \
SNAPSHOTI_CONTRACT(snap); \ nextFlags = atomic_load(&snap->flags); \
unsigned char flags = atomic_load(&snap->flags); \ return M_CONST_CAST(type, &snap->data[SNAPSHOTI_R(nextFlags)]); \
return M_CONST_CAST(type, &snap->data[SNAPSHOTI_R(flags)]); \ } \
} \ \
static inline type *M_C(name, _get_produced)(M_C(name, _t) snap) \
{ \
SNAPSHOTI_CONTRACT(snap); \
unsigned char flags = atomic_load(&snap->flags); \
return &snap->data[SNAPSHOTI_W(flags)]; \
} \
\
static inline const type *M_C(name, _get_consummed)(M_C(name, _t) snap) \
{ \
SNAPSHOTI_CONTRACT(snap); \
unsigned char flags = atomic_load(&snap->flags); \
return M_CONST_CAST(type, &snap->data[SNAPSHOTI_R(flags)]); \
} \
// FIXME: Method SWAP ? // FIXME: Method SWAP ?
+2 -2
View File
@@ -26,8 +26,8 @@
#include "coverage.h" #include "coverage.h"
START_COVERAGE START_COVERAGE
SNAPSHOT_DEF(uint, unsigned int) SNAPSHOT_DEF(snapshot_uint, unsigned int)
SNAPSHOT_DEF(mpz, mpz_t, SNAPSHOT_DEF(snapshot_mpz, mpz_t,
(INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear))) (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear)))
END_COVERAGE END_COVERAGE