123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #ifndef ZEPHYR_INCLUDE_SYS_NOTIFY_H_
- #define ZEPHYR_INCLUDE_SYS_NOTIFY_H_
- #include <kernel.h>
- #include <zephyr/types.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- struct sys_notify;
- #define SYS_NOTIFY_METHOD_COMPLETED 0
- #define SYS_NOTIFY_METHOD_SPINWAIT 1
- #define SYS_NOTIFY_METHOD_SIGNAL 2
- #define SYS_NOTIFY_METHOD_CALLBACK 3
- #define SYS_NOTIFY_METHOD_MASK 0x03U
- #define SYS_NOTIFY_METHOD_POS 0
- #define SYS_NOTIFY_EXTENSION_POS 2
- #define SYS_NOTIFY_EXTENSION_MASK (~BIT_MASK(SYS_NOTIFY_EXTENSION_POS))
- typedef void (*sys_notify_generic_callback)();
- struct sys_notify {
- union method {
-
- struct k_poll_signal *signal;
-
- sys_notify_generic_callback callback;
- } method;
-
- uint32_t volatile flags;
-
- int volatile result;
- };
- static inline uint32_t sys_notify_get_method(const struct sys_notify *notify)
- {
- uint32_t method = notify->flags >> SYS_NOTIFY_METHOD_POS;
- return method & SYS_NOTIFY_METHOD_MASK;
- }
- int sys_notify_validate(struct sys_notify *notify);
- sys_notify_generic_callback sys_notify_finalize(struct sys_notify *notify,
- int res);
- static inline int sys_notify_fetch_result(const struct sys_notify *notify,
- int *result)
- {
- __ASSERT_NO_MSG(notify != NULL);
- __ASSERT_NO_MSG(result != NULL);
- int rv = -EAGAIN;
- if (sys_notify_get_method(notify) == SYS_NOTIFY_METHOD_COMPLETED) {
- rv = 0;
- *result = notify->result;
- }
- return rv;
- }
- static inline void sys_notify_init_spinwait(struct sys_notify *notify)
- {
- __ASSERT_NO_MSG(notify != NULL);
- *notify = (struct sys_notify){
- .flags = SYS_NOTIFY_METHOD_SPINWAIT,
- };
- }
- static inline void sys_notify_init_signal(struct sys_notify *notify,
- struct k_poll_signal *sigp)
- {
- __ASSERT_NO_MSG(notify != NULL);
- __ASSERT_NO_MSG(sigp != NULL);
- *notify = (struct sys_notify){
- .method = {
- .signal = sigp,
- },
- .flags = SYS_NOTIFY_METHOD_SIGNAL,
- };
- }
- static inline void sys_notify_init_callback(struct sys_notify *notify,
- sys_notify_generic_callback handler)
- {
- __ASSERT_NO_MSG(notify != NULL);
- __ASSERT_NO_MSG(handler != NULL);
- *notify = (struct sys_notify){
- .method = {
- .callback = handler,
- },
- .flags = SYS_NOTIFY_METHOD_CALLBACK,
- };
- }
- static inline bool sys_notify_uses_callback(const struct sys_notify *notify)
- {
- __ASSERT_NO_MSG(notify != NULL);
- return sys_notify_get_method(notify) == SYS_NOTIFY_METHOD_CALLBACK;
- }
- #ifdef __cplusplus
- }
- #endif
- #endif
|