diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-09-20 15:34:11 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-10-09 14:08:31 +0200 |
commit | 7b42076a77b81e205ecc704c3bb7538716567487 (patch) | |
tree | f1158e4ea14177f01ebf55355aa995257c7b79c8 /src/afb-session.c | |
parent | 9ece61d2cbda88878b9f1e6a6e62d3d42cbdef00 (diff) |
Atomic context initialisation for bindings
Change-Id: I3e81b64d57c917da1fba9b3a9387d0f4d7f3e6b7
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/afb-session.c')
-rw-r--r-- | src/afb-session.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/afb-session.c b/src/afb-session.c index db7a99a3..61bce09a 100644 --- a/src/afb-session.c +++ b/src/afb-session.c @@ -433,7 +433,7 @@ static struct cookie *cookie_add(struct afb_session *session, int idx, const voi return cookie; } -void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void), void (*freecb)(void*)) +void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void *closure), void (*freecb)(void *item), void *closure, int replace) { int idx; void *value; @@ -441,11 +441,19 @@ void *afb_session_cookie(struct afb_session *session, const void *key, void *(*m lock(session); cookie = cookie_search(session, key, &idx); - if (cookie) - value = cookie->value; - else { - value = makecb ? makecb() : NULL; - if (makecb || freecb) { + if (cookie) { + if (!replace) + value = cookie->value; + else { + value = makecb ? makecb(closure) : closure; + if (cookie->value != value && cookie->freecb) + cookie->freecb(cookie->value); + cookie->value = value; + cookie->freecb = freecb; + } + } else { + value = makecb ? makecb(closure) : closure; + if (replace || makecb || freecb) { cookie = cookie_add(session, idx, key, value, freecb); if (!cookie) { if (makecb && freecb) |