source.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2022 ML!PA Consulting GmbH
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include "event.h"
22 #include "list.h"
23 #include "irq.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
32 typedef struct {
35 
39 typedef struct {
44 
48 #define EVENT_SOURCE_INIT { 0 }
49 
56 #define EVENT_SOURCE_SUBSCRIBER_INIT(e, q) { .event = (event_t *)e, .queue = q }
57 
64 static inline void event_source_attach(event_source_t *source,
66 {
67  unsigned state = irq_disable();
68 
69  list_add(&source->list, &event->node);
70 
71  irq_restore(state);
72 }
73 
80 static inline void event_source_detach(event_source_t *source,
82 {
83  unsigned state = irq_disable();
84 
85  list_remove(&source->list, &event->node);
86 
87  irq_restore(state);
88 }
89 
95 static inline void event_source_trigger(event_source_t *source)
96 {
97  unsigned state = irq_disable();
98 
101  node);
102  while (event) {
103  event_post(event->queue, event->event);
104  event = container_of(event->node.next, event_source_subscriber_t, node);
105  }
106 
107  irq_restore(state);
108 }
109 
110 #ifdef __cplusplus
111 }
112 #endif
#define container_of(PTR, TYPE, MEMBER)
Returns the container of a pointer to a member.
Definition: container.h:58
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
IRQ driver interface.
Intrusive linked list.
static list_node_t * list_remove(list_node_t *list, list_node_t *node)
Removes the node from the list.
Definition: list.h:82
static void list_add(list_node_t *node, list_node_t *new_node)
Insert object into list.
Definition: list.h:49
static void event_source_attach(event_source_t *source, event_source_subscriber_t *event)
Attach an event to an event source.
Definition: source.h:64
static void event_source_trigger(event_source_t *source)
Trigger all events registered on the event source.
Definition: source.h:95
static void event_source_detach(event_source_t *source, event_source_subscriber_t *event)
Remove a subscription from the event source.
Definition: source.h:80
event queue structure
Definition: event.h:150
Subscriber of an event source.
Definition: source.h:39
list_node_t node
event subscriber list node
Definition: source.h:40
event_t * event
pointer to event that should be triggered
Definition: source.h:41
event_queue_t * queue
event queue to be used for the event
Definition: source.h:42
Event source struct.
Definition: source.h:32
list_node_t list
event source list node
Definition: source.h:33
event structure
Definition: event.h:142
List node structure.
Definition: list.h:36
struct list_node * next
pointer to next list entry
Definition: list.h:37