X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-evt.c;h=1c8798fdc39cb8858a407f91748552c19fc73ff3;hb=f40979c718fa6fe6b571e133e1bf19fc90957298;hp=f54f34e17ae2520c757735b1de06a85bc97622c6;hpb=6dfeafe7e4fa582b3db3f950136bc97f8611fc6d;p=src%2Fapp-framework-binder.git diff --git a/src/afb-evt.c b/src/afb-evt.c index f54f34e1..1c8798fd 100644 --- a/src/afb-evt.c +++ b/src/afb-evt.c @@ -56,7 +56,7 @@ struct afb_evt_listener { pthread_rwlock_t rwlock; /* count of reference to the listener */ - int refcount; + uint16_t refcount; }; /* @@ -82,10 +82,10 @@ struct afb_evtid { #endif /* refcount */ - int refcount; + uint16_t refcount; /* id of the event */ - int id; + uint16_t id; /* has client? */ int has_client; @@ -176,8 +176,8 @@ static struct afb_evt_listener *listeners = NULL; /* 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) @@ -545,6 +545,7 @@ struct afb_evtid *afb_evt_evtid_create(const char *fullname) { size_t len; struct afb_evtid *evtid, *oevt; + uint16_t id; /* allocates the event */ len = strlen(fullname); @@ -554,15 +555,20 @@ struct afb_evtid *afb_evt_evtid_create(const char *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); @@ -571,7 +577,7 @@ struct afb_evtid *afb_evt_evtid_create(const char *fullname) 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; @@ -649,8 +655,10 @@ void afb_evt_evtid_unref(struct afb_evtid *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 */ @@ -720,7 +728,7 @@ const char *afb_evt_evtid_hooked_name(struct afb_evtid *evtid) /* * 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; } @@ -886,6 +894,36 @@ int afb_evt_watch_sub_evtid(struct afb_evt_listener *listener, struct afb_evtid return -1; } +/* + * Avoids the 'listener' to watch 'eventid' + * Returns 0 in case of success or else -1. + */ +int afb_evt_watch_sub_eventid(struct afb_evt_listener *listener, uint16_t eventid) +{ + struct afb_evt_watch *watch; + struct afb_evtid *evtid; + + /* search the existing watch */ + pthread_rwlock_wrlock(&listener->rwlock); + watch = listener->watchs; + while(watch != NULL) { + evtid = watch->evtid; + if (evtid->id == eventid) { + if (watch->activity != 0) { + watch->activity--; + if (watch->activity == 0 && listener->itf->remove != NULL) + listener->itf->remove(listener->closure, evtid->fullname, evtid->id); + } + pthread_rwlock_unlock(&listener->rwlock); + return 0; + } + watch = watch->next_by_listener; + } + pthread_rwlock_unlock(&listener->rwlock); + errno = ENOENT; + return -1; +} + #if WITH_AFB_HOOK /* * update the hooks for events @@ -943,7 +981,7 @@ const char *afb_evt_event_x2_fullname(struct afb_event_x2 *eventid) /* * 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;