aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-session.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-11-06 23:01:42 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2017-11-06 23:01:42 +0100
commitf26355dd547b32124ce6439ad48487cb71278c76 (patch)
tree57e081178ca609ee2f75e71752254e5d698e204f /src/afb-session.c
parent5bffa91c6c719b828a317adfb913ce57dbb2c6f0 (diff)
afb-session: fix reference counting
Change-Id: If36c9210f0982ba35299de52f0c8fd96e7c836e2 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/afb-session.c')
-rw-r--r--src/afb-session.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/afb-session.c b/src/afb-session.c
index 3befb92e..8f807615 100644
--- a/src/afb-session.c
+++ b/src/afb-session.c
@@ -161,6 +161,7 @@ static void destroy (struct afb_session *session)
assert (session != NULL);
+ remove_all_cookies(session);
pthread_mutex_lock(&sessions.mutex);
prv = &sessions.heads[(int)session->idx];
while (*prv)
@@ -293,6 +294,8 @@ struct afb_session *afb_session_search (const char *uuid)
pthread_mutex_lock(&sessions.mutex);
cleanup();
session = search(uuid, pearson4(uuid));
+ if (session)
+ __atomic_add_fetch(&session->refcount, 1, __ATOMIC_RELAXED);
pthread_mutex_unlock(&sessions.mutex);
return session;
@@ -315,7 +318,7 @@ struct afb_session *afb_session_get (const char *uuid, int timeout, int *created
else {
idx = pearson4(uuid);
session = search(uuid, idx);
- if (session != NULL) {
+ if (session) {
__atomic_add_fetch(&session->refcount, 1, __ATOMIC_RELAXED);
pthread_mutex_unlock(&sessions.mutex);
if (created)
@@ -346,8 +349,11 @@ void afb_session_unref(struct afb_session *session)
if (session != NULL) {
assert(session->refcount != 0);
if (!__atomic_sub_fetch(&session->refcount, 1, __ATOMIC_RELAXED)) {
+ pthread_mutex_lock(&session->mutex);
if (session->uuid[0] == 0)
destroy (session);
+ else
+ pthread_mutex_unlock(&session->mutex);
}
}
}
@@ -356,12 +362,16 @@ void afb_session_unref(struct afb_session *session)
void afb_session_close (struct afb_session *session)
{
assert(session != NULL);
+ pthread_mutex_lock(&session->mutex);
if (session->uuid[0] != 0) {
session->uuid[0] = 0;
- remove_all_cookies(session);
- if (session->refcount == 0)
+ remove_all_cookies(session);
+ if (session->refcount == 0) {
destroy (session);
+ return;
+ }
}
+ pthread_mutex_unlock(&session->mutex);
}
// Sample Generic Ping Debug API