afb-api: Define the notion of group for concurrency
[src/app-framework-binder.git] / src / afb-xreq.c
index 13deeb0..9c0ba16 100644 (file)
@@ -527,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)
@@ -696,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 = {
@@ -719,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 = {
@@ -743,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
 };
 
 /******************************************************************************/
@@ -1015,12 +1030,10 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
        xreq->context.api_key = api;
 
        /* check self locking */
-       if (!api->noconcurrency)
-               api = NULL;
-       else {
+       if (api->group) {
                caller = xreq->caller;
                while (caller) {
-                       if (caller->context.api_key == api) {
+                       if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
                                /* noconcurrency lock detected */
                                ERROR("self-lock detected in call stack for API %s", xreq->api);
                                early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
@@ -1032,7 +1045,7 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
 
        /* queue the request job */
        afb_xreq_unhooked_addref(xreq);
-       if (jobs_queue(api, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
+       if (jobs_queue(api->group, 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");