From 770ca7e254bba07bb7d1ade4fa95afed7a7f7693 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Wed, 20 Sep 2017 12:19:24 +0200 Subject: afb-xreq: hide internal addref/unref to xreqs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ie175ec1e508c7bd3bcdc25d7e0b26e7a9da3fafb Signed-off-by: José Bollo --- src/afb-api-dbus.c | 4 ++-- src/afb-export.c | 4 ++-- src/afb-hreq.c | 6 +++--- src/afb-stub-ws.c | 6 +++--- src/afb-xreq.c | 48 ++++++++++++++++++++++++++---------------------- src/afb-xreq.h | 2 ++ 6 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index 3884b60f..2d03269f 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -238,7 +238,7 @@ static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct memo = malloc(sizeof *memo); if (memo != NULL) { - afb_xreq_addref(xreq); + afb_xreq_unhooked_addref(xreq); memo->xreq = xreq; memo->msgid = 0; memo->api = api; @@ -262,7 +262,7 @@ static void api_dbus_client_memo_destroy(struct dbus_memo *memo) prv = &(*prv)->next; } - afb_xreq_unref(memo->xreq); + afb_xreq_unhooked_unref(memo->xreq); free(memo); } diff --git a/src/afb-export.c b/src/afb-export.c index 70a63774..dc9cbd96 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -551,12 +551,12 @@ static int svc_call_sync(void *closure, const char *api, const char *verb, struc callreq->result = NULL; callreq->status = 0; callreq->async = 0; - afb_xreq_addref(&callreq->xreq); + afb_xreq_unhooked_addref(&callreq->xreq); /* avoid early callreq destruction */ rc = jobs_enter(NULL, 0, callreq_sync_enter, callreq); if (rc >= 0) rc = callreq->status; resu = (rc >= 0 || callreq->result) ? callreq->result : afb_msg_json_internal_error(); - afb_xreq_unref(&callreq->xreq); + afb_xreq_unhooked_unref(&callreq->xreq); } if (result) *result = resu; diff --git a/src/afb-hreq.c b/src/afb-hreq.c index dc02e6fe..ba11e741 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -325,14 +325,14 @@ static void req_destroy(struct afb_xreq *xreq) void afb_hreq_addref(struct afb_hreq *hreq) { - afb_xreq_addref(&hreq->xreq); + afb_xreq_unhooked_addref(&hreq->xreq); } void afb_hreq_unref(struct afb_hreq *hreq) { if (hreq->replied) hreq->xreq.replied = 1; - afb_xreq_unref(&hreq->xreq); + afb_xreq_unhooked_unref(&hreq->xreq); } /* @@ -923,7 +923,7 @@ void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char } else if (afb_hreq_init_context(hreq) < 0) { afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); } else { - afb_xreq_addref(&hreq->xreq); /* TODO check if needed */ + afb_xreq_unhooked_addref(&hreq->xreq); afb_xreq_process(&hreq->xreq, apiset); } } diff --git a/src/afb-stub-ws.c b/src/afb-stub-ws.c index 083f72b4..83f07cac 100644 --- a/src/afb-stub-ws.c +++ b/src/afb-stub-ws.c @@ -260,7 +260,7 @@ static void client_call_cb(void * closure, struct afb_xreq *xreq) struct afb_stub_ws *stubws = closure; afb_proto_ws_client_call(stubws->proto, xreq->verb, afb_xreq_json(xreq), afb_session_uuid(xreq->context.session), xreq); - afb_xreq_addref(xreq); + afb_xreq_unhooked_addref(xreq); } static void client_on_description_cb(void *closure, struct json_object *data) @@ -334,7 +334,7 @@ static void on_reply_success(void *closure, void *request, struct json_object *r struct afb_xreq *xreq = request; afb_xreq_success(xreq, result, *info ? info : NULL); - afb_xreq_unref(xreq); + afb_xreq_unhooked_unref(xreq); } static void on_reply_fail(void *closure, void *request, const char *status, const char *info) @@ -342,7 +342,7 @@ static void on_reply_fail(void *closure, void *request, const char *status, cons struct afb_xreq *xreq = request; afb_xreq_fail(xreq, status, *info ? info : NULL); - afb_xreq_unref(xreq); + afb_xreq_unhooked_unref(xreq); } static void on_event_create(void *closure, const char *event_name, int event_id) diff --git a/src/afb-xreq.c b/src/afb-xreq.c index 842ff199..13deeb06 100644 --- a/src/afb-xreq.c +++ b/src/afb-xreq.c @@ -40,22 +40,26 @@ /******************************************************************************/ -static inline void xreq_addref(struct afb_xreq *xreq) +static void xreq_finalize(struct afb_xreq *xreq) +{ + if (!xreq->replied) + afb_xreq_fail(xreq, "error", "no reply"); + if (xreq->hookflags) + afb_hook_xreq_end(xreq); + if (xreq->caller) + afb_xreq_unhooked_unref(xreq->caller); + xreq->queryitf->unref(xreq); +} + +inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq) { __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED); } -static inline void xreq_unref(struct afb_xreq *xreq) +inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq) { - if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) { - if (!xreq->replied) - afb_xreq_fail(xreq, "error", "no reply"); - if (xreq->hookflags) - afb_hook_xreq_end(xreq); - if (xreq->caller) - xreq_unref(xreq->caller); - xreq->queryitf->unref(xreq); - } + if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) + xreq_finalize(xreq); } /******************************************************************************/ @@ -109,7 +113,7 @@ static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_obje subcall->completion(subcall, status, result); json_object_put(result); - afb_xreq_unref(&subcall->xreq); + afb_xreq_unhooked_unref(&subcall->xreq); } static void subcall_destroy_cb(struct afb_xreq *xreq) @@ -159,7 +163,7 @@ static struct subcall *subcall_alloc( subcall->xreq.api = api; subcall->xreq.verb = verb; subcall->xreq.caller = caller; - xreq_addref(caller); + afb_xreq_unhooked_addref(caller); } return subcall; } @@ -208,7 +212,7 @@ static void subcall_process(struct subcall *subcall, void (*completion)(struct s subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb, subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq); } else { - afb_xreq_addref(&subcall->xreq); + afb_xreq_unhooked_addref(&subcall->xreq); afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset); } } @@ -274,14 +278,14 @@ static int subcallsync(struct subcall *subcall, struct json_object **result) { int rc; - afb_xreq_addref(&subcall->xreq); + afb_xreq_unhooked_addref(&subcall->xreq); rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall); *result = subcall->result; if (rc < 0 || subcall->status < 0) { *result = *result ?: afb_msg_json_internal_error(); rc = -1; } - afb_xreq_unref(&subcall->xreq); + afb_xreq_unhooked_unref(&subcall->xreq); return rc; } @@ -384,13 +388,13 @@ static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(v static void xreq_addref_cb(void *closure) { struct afb_xreq *xreq = closure; - xreq_addref(xreq); + afb_xreq_unhooked_addref(xreq); } static void xreq_unref_cb(void *closure) { struct afb_xreq *xreq = closure; - xreq_unref(xreq); + afb_xreq_unhooked_unref(xreq); } static void xreq_session_close_cb(void *closure) @@ -967,7 +971,7 @@ static void process_async(int signum, void *arg) api->itf->call(api->closure, xreq); } /* release the request */ - xreq_unref(xreq); + afb_xreq_unhooked_unref(xreq); } /** @@ -1027,14 +1031,14 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset) } /* queue the request job */ - xreq_addref(xreq); + afb_xreq_unhooked_addref(xreq); if (jobs_queue(api, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) { /* TODO: allows or not to proccess it directly as when no threading? (see above) */ ERROR("can't process job with threads: %m"); early_failure(xreq, "cancelled", "not able to create a job for the task"); - xreq_unref(xreq); + afb_xreq_unhooked_unref(xreq); } end: - xreq_unref(xreq); + afb_xreq_unhooked_unref(xreq); } diff --git a/src/afb-xreq.h b/src/afb-xreq.h index f7ca03b5..4978f0a5 100644 --- a/src/afb-xreq.h +++ b/src/afb-xreq.h @@ -94,6 +94,8 @@ struct afb_xreq extern struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq); extern void afb_xreq_addref(struct afb_xreq *xreq); extern void afb_xreq_unref(struct afb_xreq *xreq); +extern void afb_xreq_unhooked_addref(struct afb_xreq *xreq); +extern void afb_xreq_unhooked_unref(struct afb_xreq *xreq); extern struct json_object *afb_xreq_json(struct afb_xreq *xreq); -- cgit 1.2.3-korg