pthread_rwlock_t rwlock;
/* count of reference to the listener */
- int refcount;
+ uint16_t refcount;
};
/*
#endif
/* refcount */
- int refcount;
+ uint16_t refcount;
/* id of the event */
- int id;
+ uint16_t id;
/* has client? */
int has_client;
/* handling id of events */
static pthread_rwlock_t events_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static struct afb_evtid *evtids = NULL;
-static int event_id_counter = 0;
-static int event_id_wrapped = 0;
+static uint16_t event_genid = 0;
+static uint16_t event_count = 0;
/* head of uniqueness of events */
#if !defined(EVENT_BROADCAST_HOP_MAX)
{
size_t len;
struct afb_evtid *evtid, *oevt;
+ uint16_t id;
/* allocates the event */
len = strlen(fullname);
/* allocates the id */
pthread_rwlock_wrlock(&events_rwlock);
+ if (event_count == UINT16_MAX) {
+ pthread_rwlock_unlock(&events_rwlock);
+ free(evtid);
+ ERROR("Can't create more events");
+ return NULL;
+ }
+ event_count++;
do {
- if (++event_id_counter < 0) {
- event_id_wrapped = 1;
- event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
- }
- if (!event_id_wrapped)
- break;
+ /* TODO add a guard (counting number of event created) */
+ id = ++event_genid;
+ if (!id)
+ id = event_genid = 1;
oevt = evtids;
- while(oevt != NULL && oevt->id != event_id_counter)
+ while(oevt != NULL && oevt->id != id)
oevt = oevt->next;
} while (oevt != NULL);
evtid->next = evtids;
evtid->refcount = 1;
evtid->watchs = NULL;
- evtid->id = event_id_counter;
+ evtid->id = id;
evtid->has_client = 0;
pthread_rwlock_init(&evtid->rwlock, NULL);
evtids = evtid;
prv = &evtids;
while (*prv && !(found = (*prv == evtid)))
prv = &(*prv)->next;
- if (found)
+ if (found) {
*prv = evtid->next;
+ event_count--;
+ }
pthread_rwlock_unlock(&events_rwlock);
/* destroys the event */
/*
* Returns the id of the 'event'
*/
-int afb_evt_evtid_id(struct afb_evtid *evtid)
+uint16_t afb_evt_evtid_id(struct afb_evtid *evtid)
{
return evtid->id;
}
/*
* Returns the id of the 'eventid'
*/
-int afb_evt_event_x2_id(struct afb_event_x2 *eventid)
+uint16_t afb_evt_event_x2_id(struct afb_event_x2 *eventid)
{
struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
return evtid ? evtid->id : 0;