67870ce6b051168ce66de31522df33114db9346e
[src/app-framework-binder.git] / src / afb-thread.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <string.h>
21
22 #include <afb/afb-req-itf.h>
23
24 #include "afb-thread.h"
25 #include "jobs.h"
26 #include "sig-monitor.h"
27 #include "verbose.h"
28
29 static void req_call(int signum, void *arg1, void *arg2, void *arg3)
30 {
31         struct afb_req req = { .itf = arg1, .closure = arg2 };
32         void (*callback)(struct afb_req) = arg3;
33
34         if (signum != 0)
35                 afb_req_fail_f(req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
36         else
37                 callback(req);
38         afb_req_unref(req);
39 }
40
41 void afb_thread_req_call(struct afb_req req, void (*callback)(struct afb_req req), int timeout, void *group)
42 {
43         int rc;
44
45         afb_req_addref(req);
46         if (0) {
47                 /* no threading */
48                 sig_monitor3(timeout, req_call, (void*)req.itf, req.closure, callback);
49         } else {
50                 /* threading */
51                 rc = jobs_queue3(group, timeout, req_call, (void*)req.itf, req.closure, callback);
52                 if (rc < 0) {
53                         /* TODO: allows or not to proccess it directly as when no threading? (see above) */
54                         ERROR("can't process job with threads: %m");
55                         afb_req_fail_f(req, "cancelled", "not able to pipe a job for the task");
56                         afb_req_unref(req);
57                 }
58         }
59 }
60