4 #include "util/debug.h"
8 * Use trigger to model operations which need to be executed when
9 * an event (a signal, for example) is observed.
11 * States and transits:
14 * OFF--(on)--> READY --(hit)--> HIT
20 * is_hit and is_ready are two key functions to query the state of
21 * a trigger. is_hit means the event already happen; is_ready means the
22 * trigger is waiting for the event.
35 #define TRIGGER_WARN_ONCE(t, exp) \
36 WARN_ONCE(t->state != exp, "trigger '%s' state transist error: %d in %s()\n", \
37 t->name, t->state, __func__)
39 static inline bool trigger_is_available(struct trigger *t)
44 static inline bool trigger_is_error(struct trigger *t)
46 return t->state <= TRIGGER_ERROR;
49 static inline void trigger_on(struct trigger *t)
51 TRIGGER_WARN_ONCE(t, TRIGGER_OFF);
52 t->state = TRIGGER_READY;
55 static inline void trigger_ready(struct trigger *t)
57 if (!trigger_is_available(t))
59 t->state = TRIGGER_READY;
62 static inline void trigger_hit(struct trigger *t)
64 if (!trigger_is_available(t))
66 TRIGGER_WARN_ONCE(t, TRIGGER_READY);
67 t->state = TRIGGER_HIT;
70 static inline void trigger_off(struct trigger *t)
72 if (!trigger_is_available(t))
74 t->state = TRIGGER_OFF;
77 static inline void trigger_error(struct trigger *t)
79 t->state = TRIGGER_ERROR;
82 static inline bool trigger_is_ready(struct trigger *t)
84 return t->state == TRIGGER_READY;
87 static inline bool trigger_is_hit(struct trigger *t)
89 return t->state == TRIGGER_HIT;
92 #define DEFINE_TRIGGER(n) \
93 struct trigger n = {.state = TRIGGER_OFF, .name = #n}