38 #ifndef EM_EVENT_GROUP_H_
39 #define EM_EVENT_GROUP_H_
45 #define invalid_egrp(event_group) \
46 ((unsigned int)egrp_hdl2idx((event_group)) >= EM_MAX_EVENT_GROUPS)
53 event_group_alloc(
void);
56 event_group_free(em_event_group_t event_group);
65 egrp_hdl2idx(
const em_event_group_t event_group)
67 return (
int)((uintptr_t)event_group - 1);
70 static inline em_event_group_t
71 egrp_idx2hdl(
const int event_group_idx)
73 return (em_event_group_t)(uintptr_t)(event_group_idx + 1);
77 event_group_elem_get(
const em_event_group_t event_group)
79 const int egrp_idx = egrp_hdl2idx(event_group);
85 egrp_elem = &
em_shm->event_group_tbl.egrp_elem[egrp_idx];
90 static inline uint64_t
95 egrp_count.all = EM_ATOMIC_GET(&egrp_elem->post.atomic);
97 return egrp_count.
gen;
105 set_local_safe(
const em_event_group_t egrp,
const int32_t egrp_gen,
106 const unsigned int decr)
109 uint64_t current_count;
114 current_count = EM_ATOMIC_GET(&egrp_elem->pre.atomic);
115 new_count.all = current_count;
116 new_count.
count -= decr;
118 if (unlikely(new_count.
count < 0 ||
119 new_count.
gen != egrp_gen)) {
122 "Expired event group event received!");
126 }
while (!EM_ATOMIC_CMPSET(&egrp_elem->pre.atomic,
127 current_count, new_count.all));
142 event_group_set_local(
const em_event_group_t egrp,
const int32_t egrp_gen,
143 const unsigned int decr)
151 set_local_safe(egrp, egrp_gen, decr);
163 static inline int64_t
165 const unsigned int decr)
167 uint64_t current_count;
171 current_count = EM_ATOMIC_GET(&egrp_elem->post.atomic);
172 new_count.all = current_count;
173 new_count.
count -= decr;
175 if (unlikely(new_count.
count < 0 ||
178 if (!egrp_elem->
ready)
181 "Expired grp event in post cnt!"
185 }
while (!EM_ATOMIC_CMPSET(&egrp_elem->post.atomic, current_count,
187 return new_count.
count;
196 event_group_count_decrement(
const unsigned int decr)
203 count = count_decrement_safe(egrp_elem, decr);
205 count = EM_ATOMIC_SUB_RETURN(&egrp_elem->post.atomic, decr);
207 if (unlikely(count < 0)) {
208 if (egrp_elem->
ready) {
210 egrp_elem->post.
all = 0;
216 "Group count already 0!");
226 egrp_elem->pre.count = 0;
228 const int num_notif = egrp_elem->
num_notif;
234 for (
int i = 0; i < num_notif; i++) {
235 notif_tbl[i].
event = egrp_elem->notif_tbl[i].event;
236 notif_tbl[i].
queue = egrp_elem->notif_tbl[i].queue;
237 notif_tbl[i].
egroup = egrp_elem->notif_tbl[i].egroup;
240 egrp_elem->
ready =
true;
242 if (unlikely(ret !=
EM_OK))
244 "send notifs failed");
249 save_current_evgrp(em_event_group_t *save_egrp ,
251 int32_t *save_egrp_gen )
261 restore_current_evgrp(
const em_event_group_t saved_egrp,
263 const int32_t saved_egrp_gen)
273 event_group_count(
void);