/** * \file * * * \brief Simple notifier for the observer/subject pattern (implementation) * * \author Bernie Innocenti */ #include "observer.h" #include // IRQ_DISABLE/IRQ_ENABLE void observer_SetEvent(Observer *observer, void (*event)(int event_id, void *param)) { observer->event = event; } void observer_InitSubject(Subject *subject) { LIST_INIT(&subject->observers); } void observer_Subscribe(Subject *subject, Observer *observer) { ATOMIC(ADDHEAD(&subject->observers, &observer->link)); } void observer_Unsubscribe(UNUSED_ARG(Subject *,subject), Observer *observer) { ATOMIC(REMOVE(&observer->link)); } void observer_notify(Subject *subject, int event_id, void *param) { Observer *observer; cpu_flags_t irqstate; IRQ_SAVE_DISABLE(irqstate); /* * Run over list with protection against other * threads, but re-enable irqs in callbacks. */ FOREACH_NODE(observer, &subject->observers) { IRQ_RESTORE(irqstate); observer->event(event_id, param); IRQ_SAVE_DISABLE(irqstate); } IRQ_RESTORE(irqstate); }