-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsm.h
71 lines (61 loc) · 1.79 KB
/
sm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef __LIB_SM__
#define __LIB_SM__
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#define RC(rc) ({ \
typeof(rc) __rc = (rc); \
printf("< rc=%d\n", __rc); \
__rc; \
})
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define container_of(e, type, field) \
((type *)(uintptr_t)((char *)(e)-offsetof(type, field)))
#define PRE(cond) assert((cond))
#define POST(cond) assert((cond))
#define IMPOSSIBLE(why) assert(false && why)
#define ERGO(a, b) (!(a) || (b))
#define BITS(state) (1ULL << (state))
enum {
SM_PREV_NONE = -1,
/* sizeof(sm_conf::allowed * 8) */
SM_STATES_MAX = 64,
/* flags */
SM_INITIAL = 1U << 0,
SM_FAILURE = 1U << 1,
SM_FINAL = 1U << 2,
};
struct sm_conf
{
uint32_t flags;
uint64_t allowed;
const char *name;
};
struct sm
{
int rc;
int state;
const char *name;
unsigned long int id;
bool (*is_locked)(const struct sm *);
bool (*invariant)(const struct sm *, int);
const struct sm_conf *conf;
};
void sm_init(struct sm *m,
bool (*invariant)(const struct sm *, int),
/* optional, set NULL if not used */
bool (*is_locked)(const struct sm *),
const struct sm_conf *conf,
int state);
void sm_fini(struct sm *m);
void sm_move(struct sm *m, int next_state);
void sm_fail(struct sm *m, int fail_state, int rc);
int sm_state(const struct sm *m);
void sm_to_sm_obs(const struct sm *from, const struct sm *to);
void sm_from_to_obs(const char *from_name, uint32_t from_pid, uint64_t from_id,
const char *to_name, uint32_t to_pid, uint64_t to_id);
void sm_attr_obs(const struct sm *m, const char *key, const char *value);
void sm_attr_obs_d(const struct sm *m, const char *key, int64_t value);
unsigned int sm_pid(void);
#endif /* __LIB_SM__ */