X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-session.c;h=d3e658dd76dee4fcbaec3a81687e57cd28206bef;hb=6cd94fad12ca4e038aac409e8e1cb9a332a80a39;hp=523cbf0444c9e298bd86e30efe79a7b306d3af66;hpb=ce8de8236b96dc771d3af3094e04e797e75767af;p=src%2Fapp-framework-binder.git diff --git a/src/afb-session.c b/src/afb-session.c index 523cbf04..d3e658dd 100644 --- a/src/afb-session.c +++ b/src/afb-session.c @@ -120,7 +120,7 @@ static void free_data (struct afb_session *session) void afb_session_init (int max_session_count, int timeout, const char *initok) { // let's create as store as hashtable does not have any - sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct afb_session)); + sessions.store = calloc (1 + (unsigned)max_session_count, sizeof *sessions.store); pthread_mutex_init(&sessions.mutex, NULL); sessions.max = max_session_count; sessions.timeout = timeout; @@ -309,6 +309,8 @@ struct afb_session *afb_session_get (const char *uuid, int *created) /* search for an existing one not too old */ if (uuid != NULL) { session = search(uuid); + if (!created) + return session; if (session != NULL) { *created = 0; session->access = now; @@ -317,14 +319,16 @@ struct afb_session *afb_session_get (const char *uuid, int *created) } } - *created = 1; + if (created) + *created = 1; + return make_session(uuid, sessions.timeout, now); } struct afb_session *afb_session_addref(struct afb_session *session) { if (session != NULL) - session->refcount++; + __atomic_add_fetch(&session->refcount, 1, __ATOMIC_RELAXED); return session; } @@ -332,11 +336,12 @@ void afb_session_unref(struct afb_session *session) { if (session != NULL) { assert(session->refcount != 0); - --session->refcount; - if (session->refcount == 0 && session->uuid[0] == 0) { - destroy (session); - pthread_mutex_destroy(&session->mutex); - free(session); + if (!__atomic_sub_fetch(&session->refcount, 1, __ATOMIC_RELAXED)) { + if (session->uuid[0] == 0) { + destroy (session); + pthread_mutex_destroy(&session->mutex); + free(session); + } } } }