Skip to content

Commit abac3d3

Browse files
committed
Change am_ao_publish() to take const struct am_event **, add comments
1 parent 0697944 commit abac3d3

File tree

4 files changed

+134
-44
lines changed

4 files changed

+134
-44
lines changed

apps/examples/dpp/philo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <string.h>
2828

29+
#include "common/compiler.h"
2930
#include "common/macros.h"
3031
#include "hsm/hsm.h"
3132
#include "event/event.h"
@@ -116,7 +117,7 @@ static int philo_eating(struct philo *me, const struct am_event *event) {
116117
EVT_DONE, sizeof(struct done), /*margin=*/0
117118
);
118119
msg->philo = me->id;
119-
am_ao_publish(&msg->event);
120+
am_ao_publish(AM_CAST(const struct am_event **, &msg));
120121
return AM_HSM_TRAN(philo_thinking);
121122
}
122123
default:

apps/examples/dpp/table.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdlib.h>
2828
#include <string.h>
2929

30+
#include "common/compiler.h"
3031
#include "common/macros.h"
3132
#include "event/event.h"
3233
#include "hsm/hsm.h"
@@ -96,7 +97,7 @@ static void table_serve(int philo) {
9697
);
9798
eat->philo = philo;
9899
am_pal_printf("table serving philo %d\n", philo);
99-
am_ao_publish(&eat->event);
100+
am_ao_publish(AM_CAST(const struct am_event **, &eat));
100101
philo_mark_eating(philo);
101102

102103
if (m_table.nsession) {

libs/ao/ao.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ bool am_ao_publish_x(const struct am_event **event, int margin) {
118118
return all_published;
119119
}
120120

121-
void am_ao_publish(const struct am_event *event) {
121+
void am_ao_publish(const struct am_event **event) {
122122
AM_ASSERT(event);
123-
AM_ASSERT(AM_EVENT_HAS_USER_ID(event));
124-
AM_ASSERT(AM_EVENT_HAS_PUBSUB_ID(event));
123+
AM_ASSERT(*event);
124+
AM_ASSERT(AM_EVENT_HAS_USER_ID(*event));
125+
AM_ASSERT(AM_EVENT_HAS_PUBSUB_ID(*event));
125126

126-
am_ao_publish_x(&event, /*margin=*/0);
127+
am_ao_publish_x(event, /*margin=*/0);
127128
}
128129

129130
void am_ao_post_fifo(struct am_ao *ao, const struct am_event *event) {

libs/ao/ao.h

Lines changed: 125 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct am_ao {
6262
bool stopped; /**< 1: AO was stopped, 0: AO wasn't stopped */
6363
};
6464

65-
/** AO state configuration. */
65+
/** Active object library state configuration. */
6666
struct am_ao_state_cfg {
6767
/** Debug callback. */
6868
void (*debug)(const struct am_ao *ao, const struct am_event *e);
@@ -97,45 +97,114 @@ struct am_ao_subscribe_list {
9797
/**
9898
* Publish event.
9999
*
100-
* @param event the event to publish
100+
* The event is delivered to event queues of all the active objects,
101+
* which are subscribed to the event ID.
102+
* The event is then handled asynchronously by the active objects.
103+
*
104+
* Use am_ao_subscribe() to subscribe an active object
105+
* to an event ID. Also use am_ao_unsubscribe() or am_ao_unsubscribe_all()
106+
* to unsubscribe it from the event ID.
107+
*
108+
* If any active object has full event queue and cannot
109+
* accommodate the event, then the function crashes with assert.
110+
*
111+
* If your application is prepared for loosing the event,
112+
* then use am_ao_publish_x() function instead.
113+
*
114+
* The event is added to active object event queue using am_event_push_back()
115+
* function.
116+
*
117+
* Tries to free the event, if no active objects are subscribed to it.
118+
* *event is set to NULL, if the event was freed.
119+
*
120+
* The library takes care of freeing the event once all
121+
* subscribed active objects handled the event.
122+
*
123+
* Statically allocated events (events for which am_event_is_static()
124+
* returns true) are never freed.
125+
*
126+
* The function is fast and thread safe and usable from
127+
* interrupt service routines (ISR).
128+
*
129+
* @param event the pointer to the event to publish
101130
*/
102-
void am_ao_publish(const struct am_event *event);
131+
void am_ao_publish(const struct am_event **event);
103132

104133
/**
105134
* Publish event.
106135
*
136+
* The event is delivered to event queues of all the active objects,
137+
* which are subscribed to the event ID.
138+
* The event is then handled asynchronously by the active objects.
139+
*
140+
* Use am_ao_subscribe() to subscribe an active object
141+
* to an event ID. Also use am_ao_unsubscribe() or am_ao_unsubscribe_all()
142+
* to unsubscribe it from the event ID.
143+
*
144+
* If any active object has full event queue and cannot
145+
* accommodate the event, then the function skips the event delivery
146+
* to the active object.
147+
*
148+
* If your application is not prepared for loosing the event,
149+
* then use am_ao_publish() function instead.
150+
*
151+
* The event is added to active object event queue using am_event_push_back()
152+
* function.
153+
*
107154
* Tries to free the event, if it was not delivered to any subscriber.
108155
* *event is set to NULL, if the event was freed.
109156
*
110-
* @param event the event to publish
111-
* @param margin free event queue slots to be available after event is pushed
157+
* The library takes care of freeing the event once all
158+
* subscribed active objects handled the event.
159+
*
160+
* Statically allocated events (events for which am_event_is_static()
161+
* returns true) are never freed.
162+
*
163+
* The function is fast and thread safe and usable from
164+
* interrupt service routines (ISR).
165+
*
166+
* @param event the pointer to the event to publish
167+
* @param margin free event queue slots to be available in each subscribed
168+
* active object after the event is pushed to their event queues
112169
*
113170
* @retval true success
114171
* @retval false at least one delivery has failed
115172
*/
116173
bool am_ao_publish_x(const struct am_event **event, int margin);
117174

118175
/**
119-
* Post event to the back of AO event queue.
176+
* Post event to the back of active object's event queue.
120177
*
121-
* If AO event queue is full the API asserts.
122-
* The event is handled asynchronously.
178+
* The event is then handled asynchronously by the active object.
123179
*
124-
* @param ao the event is posted to this AO
180+
* If the active object's event queue is full the function asserts.
181+
*
182+
* The function is fast and thread safe and usable from
183+
* interrupt service routines (ISR).
184+
*
185+
* @param ao the event is posted to this active object
125186
* @param event the event to post
126187
*/
127188
void am_ao_post_fifo(struct am_ao *ao, const struct am_event *event);
128189

129190
/**
130-
* Post event to the back of AO event queue.
191+
* Post event to the back of active object's event queue.
192+
*
193+
* The event is then handled asynchronously by the active object.
131194
*
132-
* If AO event queue is full and margin is >0 the API fails gracefully.
133-
* The event is handled asynchronously.
195+
* If the active object's event queue is full and margin is >0,
196+
* then the function fails gracefully.
134197
*
135198
* Tries to free the event, if it was not posted.
136199
* *event is set to NULL, if the event was freed.
137200
*
138-
* @param ao the event is posted to this AO
201+
* Statically allocated events (events for which am_event_is_static()
202+
* returns true) are never freed.
203+
*
204+
* The function is fast and thread safe and usable from
205+
* interrupt service routines (ISR).
206+
*
207+
* @param ao the event is posted to this active object
139208
* @param event the event to post
140209
* @param margin free event queue slots to be available after event is posted
141210
*
@@ -147,26 +216,35 @@ bool am_ao_post_fifo_x(
147216
);
148217

149218
/**
150-
* Post event to the front of AO event queue.
219+
* Post event to the front of active object's event queue.
220+
*
221+
* The event is then handled asynchronously by the active object.
151222
*
152-
* If AO event queue is full the API asserts.
153-
* The event is handled asynchronously.
223+
* If active object's event queue is full the function asserts.
154224
*
155-
* @param ao the event is posted to this AO
225+
* The function is fast and thread safe and usable from
226+
* interrupt service routines (ISR).
227+
*
228+
* @param ao the event is posted to this active object
156229
* @param event the event to post
157230
*/
158231
void am_ao_post_lifo(struct am_ao *ao, const struct am_event *event);
159232

160233
/**
161234
* Post event to the front of AO event queue.
162235
*
163-
* If AO event queue is full and margin is >0 the API fails gracefully.
164-
* The event is handled asynchronously.
236+
* The event is then handled asynchronously by the active object.
237+
*
238+
* If active object's event queue is full and margin is >0,
239+
* then the function fails gracefully.
165240
*
166241
* Tries to free the event, if it was not posted.
167242
* *event is set to NULL, if the event was freed.
168243
*
169-
* @param ao the event is posted to this AO
244+
* Statically allocated events (events for which am_event_is_static()
245+
* returns true) are never freed.
246+
*
247+
* @param ao the event is posted to this active object
170248
* @param event the event to post
171249
* @param margin free event queue slots to be available after event is posted
172250
*
@@ -190,16 +268,20 @@ void am_ao_ctor(struct am_ao *ao, const struct am_hsm_state *state);
190268
*
191269
* The safest is to start active objects in the order of their priority,
192270
* beginning from the lowest priority active objects because they tend
193-
* to have the biggest event queues.
271+
* to have the bigger event queues.
194272
*
195273
* @param ao the active object to start
196274
* @param prio priority level [0, AM_AO_PRIO_MAX]
197275
* @param queue the active object's event queue
198-
* @param queue_size the event queue size
276+
* @param queue_size the event queue size [sizeof(struct am_event*)]
199277
* @param stack active object stack
200278
* @param stack_size the stack size [bytes]
201-
* @param name human readable name of active object
202-
* @param init_event init event. Can be NULL. The event is not recycled.
279+
* @param name human readable name of active object.
280+
* Not copied. Must remain valid after the call.
281+
* Can be NULL.
282+
* @param init_event init event. Can be NULL.
283+
* The event is not freed. The caller is responsible
284+
* for freeing the event after the call.
203285
*/
204286
void am_ao_start(
205287
struct am_ao *ao,
@@ -213,12 +295,13 @@ void am_ao_start(
213295
);
214296

215297
/**
216-
* Stop an active object
298+
* Stop an active object.
217299
*
218300
* @param ao the active object to stop
219301
*/
220302
void am_ao_stop(struct am_ao *ao);
221303

304+
/** Active object library configuration. */
222305
struct am_ao_cfg {
223306
/** Debug callback. */
224307
void (*debug)(const struct am_ao *ao, const struct am_event *e);
@@ -229,45 +312,49 @@ struct am_ao_cfg {
229312
};
230313

231314
/**
232-
* AO state constructor.
315+
* Active object library state constructor.
233316
*
234-
* @param cfg AO state configuration
235-
* The AO module makes an internal copy of the configuration.
317+
* @param cfg active object library configuration
318+
* The active object module makes an internal copy of
319+
* the configuration.
236320
*/
237321
void am_ao_state_ctor(const struct am_ao_state_cfg *cfg);
238322

239-
/** Destroys the internal state of AO */
323+
/** Destroy the internal state of active object library */
240324
void am_ao_state_dtor(void);
241325

242326
/**
243-
* Subscribes active object to event.
327+
* Subscribe active object to event ID.
244328
*
245329
* @param ao active object to subscribe
246330
* @param event the event ID to subscribe to
247331
*/
248332
void am_ao_subscribe(const struct am_ao *ao, int event);
249333

250334
/**
251-
* Unsubscribes active object from the event.
335+
* Unsubscribe active object from event ID.
252336
*
253337
* @param ao active object to unsubscribe
254338
* @param event the event ID to unsubscribe from
255339
*/
256340
void am_ao_unsubscribe(const struct am_ao *ao, int event);
257341

258342
/**
259-
* Unsubscribes active object from all events.
343+
* Unsubscribe active object from all events.
260344
*
261345
* @param ao active object to unsubscribe
262346
*/
263347
void am_ao_unsubscribe_all(const struct am_ao *ao);
264348

265349
/**
266-
* Initialize the AO subscribe list.
350+
* Initialize active object global subscribe list.
267351
*
268-
* Optional. Only needed if AO pub/sub functionality is used.
352+
* Optional. Only needed if active object pub/sub functionality is used.
353+
* The pub/sub functionality is provided by
354+
* am_ao_publish(), am_ao_publish_x(),
355+
* am_ao_subscribe(), am_ao_unsubscribe() and am_ao_unsubscribe_all() APIs.
269356
*
270-
* @param sub the array of AO subscribe lists
357+
* @param sub the array of active object subscribe lists
271358
* @param nsub the number of elements in sub array
272359
*/
273360
void am_ao_init_subscribe_list(struct am_ao_subscribe_list *sub, int nsub);
@@ -284,9 +371,9 @@ void am_ao_init_subscribe_list(struct am_ao_subscribe_list *sub, int nsub);
284371
bool am_ao_run_all(void);
285372

286373
/**
287-
* Check if AO event queue is empty.
374+
* Check if active object event queue is empty.
288375
*
289-
* @param ao check the event queue of this AO
376+
* @param ao check the event queue of this active object
290377
*
291378
* @retval true the queue is empty
292379
* @retval false the queue is not empty
@@ -307,7 +394,7 @@ void am_ao_dump_event_queues(
307394
);
308395

309396
/**
310-
* Log last event of every AO.
397+
* Log last event of every active object.
311398
*
312399
* @param log the logging callback
313400
*/

0 commit comments

Comments
 (0)