Atomic context initialisation for bindings
[src/app-framework-binder.git] / src / afb-xreq.c
index bc63e0d..696bf9d 100644 (file)
 
 /******************************************************************************/
 
-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);
 }
 
 /******************************************************************************/
 
-extern const struct afb_req_itf xreq_itf;
-extern const struct afb_req_itf xreq_hooked_itf;
-
 static inline struct afb_req to_req(struct afb_xreq *xreq)
 {
-       return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
+       return (struct afb_req){ .itf = xreq->itf, .closure = xreq };
 }
 
 /******************************************************************************/
@@ -112,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)
@@ -162,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;
 }
@@ -211,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);
        }
 }
@@ -277,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;
 }
 
@@ -387,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)
@@ -526,6 +527,12 @@ static char *xreq_get_application_id_cb(void*closure)
        return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
 }
 
+static void *xreq_context_make_cb(void *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
+{
+       struct afb_xreq *xreq = closure;
+       return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
+}
+
 /******************************************************************************/
 
 static struct json_object *xreq_hooked_json_cb(void *closure)
@@ -695,6 +702,13 @@ static char *xreq_hooked_get_application_id_cb(void*closure)
        return afb_hook_xreq_get_application_id(xreq, r);
 }
 
+static void *xreq_hooked_context_make_cb(void *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
+{
+       struct afb_xreq *xreq = closure;
+       void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
+       return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
+}
+
 /******************************************************************************/
 
 const struct afb_req_itf xreq_itf = {
@@ -718,7 +732,8 @@ const struct afb_req_itf xreq_itf = {
        .store = xreq_store_cb,
        .subcall_req = xreq_subcall_req_cb,
        .has_permission = xreq_has_permission_cb,
-       .get_application_id = xreq_get_application_id_cb
+       .get_application_id = xreq_get_application_id_cb,
+       .context_make = xreq_context_make_cb
 };
 
 const struct afb_req_itf xreq_hooked_itf = {
@@ -742,7 +757,8 @@ const struct afb_req_itf xreq_hooked_itf = {
        .store = xreq_hooked_store_cb,
        .subcall_req = xreq_hooked_subcall_req_cb,
        .has_permission = xreq_hooked_has_permission_cb,
-       .get_application_id = xreq_hooked_get_application_id_cb
+       .get_application_id = xreq_hooked_get_application_id_cb,
+       .context_make = xreq_hooked_context_make_cb
 };
 
 /******************************************************************************/
@@ -927,6 +943,7 @@ void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb
 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
 {
        memset(xreq, 0, sizeof *xreq);
+       xreq->itf = &xreq_hooked_itf; /* hook by default */
        xreq->refcount = 1;
        xreq->queryitf = queryitf;
 }
@@ -946,6 +963,8 @@ static void init_hooking(struct afb_xreq *xreq)
        afb_hook_init_xreq(xreq);
        if (xreq->hookflags)
                afb_hook_xreq_begin(xreq);
+       else
+               xreq->itf = &xreq_itf; /* unhook the interface */
 }
 
 /**
@@ -967,7 +986,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 +1046,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);
 }