summaryrefslogtreecommitdiffstats
path: root/src/afb-session.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-06-07 13:59:08 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-06-07 13:59:08 +0200
commitb192d4cc46d8bda166116432ee28042e95750052 (patch)
treef5261c6f5790d8d7dc21d36cc7aeea0f12fb1138 /src/afb-session.c
parent142f890314a2279a383a70251e1579f277c408e3 (diff)
Fix concurrency issue in handling references
Change-Id: Iaae331fbdadb88f26057a64193a026950dcb56e4 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/afb-session.c')
-rw-r--r--src/afb-session.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/afb-session.c b/src/afb-session.c
index 523cbf04..536cdbcc 100644
--- a/src/afb-session.c
+++ b/src/afb-session.c
@@ -324,7 +324,7 @@ struct afb_session *afb_session_get (const char *uuid, int *created)
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 +332,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);
+ }
}
}
}