1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include "util/debug.h"
9 * Use trigger to model operations which need to be executed when
10 * an event (a signal, for example) is observed.
12 * States and transits:
15 * OFF--> ON --> READY --(hit)--> HIT
21 * is_hit and is_ready are two key functions to query the state of
22 * a trigger. is_hit means the event already happen; is_ready means the
23 * trigger is waiting for the event.
37 #define TRIGGER_WARN_ONCE(t, exp) \
38 WARN_ONCE(t->state != exp, "trigger '%s' state transist error: %d in %s()\n", \
39 t->name, t->state, __func__)
41 static inline bool trigger_is_available(struct trigger
*t
)
46 static inline bool trigger_is_error(struct trigger
*t
)
48 return t
->state
<= TRIGGER_ERROR
;
51 static inline void trigger_on(struct trigger
*t
)
53 TRIGGER_WARN_ONCE(t
, TRIGGER_OFF
);
54 t
->state
= TRIGGER_ON
;
57 static inline void trigger_ready(struct trigger
*t
)
59 if (!trigger_is_available(t
))
61 t
->state
= TRIGGER_READY
;
64 static inline void trigger_hit(struct trigger
*t
)
66 if (!trigger_is_available(t
))
68 TRIGGER_WARN_ONCE(t
, TRIGGER_READY
);
69 t
->state
= TRIGGER_HIT
;
72 static inline void trigger_off(struct trigger
*t
)
74 if (!trigger_is_available(t
))
76 t
->state
= TRIGGER_OFF
;
79 static inline void trigger_error(struct trigger
*t
)
81 t
->state
= TRIGGER_ERROR
;
84 static inline bool trigger_is_ready(struct trigger
*t
)
86 return t
->state
== TRIGGER_READY
;
89 static inline bool trigger_is_hit(struct trigger
*t
)
91 return t
->state
== TRIGGER_HIT
;
94 #define DEFINE_TRIGGER(n) \
95 struct trigger n = {.state = TRIGGER_OFF, .name = #n}