Remove uses of deprecated macros
[src/app-framework-binder.git] / src / jobs-fake.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "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 <stdlib.h>
21 #include <stdint.h>
22 #include <unistd.h>
23 #include <signal.h>
24 #include <time.h>
25 #include <sys/syscall.h>
26 #include <pthread.h>
27 #include <errno.h>
28 #include <assert.h>
29
30 #include <systemd/sd-event.h>
31
32 #include "jobs.h"
33 #include "sig-monitor.h"
34 #include "verbose.h"
35
36 #include "jobs.h"
37
38 struct jobloop;
39
40 struct job
41 {
42         struct job *next;
43         const void *group;
44         int timeout;
45         void (*callback)(int signum, void* arg);
46         void *closure;
47 };
48
49 static struct job *first, *last;
50 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
51
52 static int add_job(const void *group, int timeout, void (*callback)(int signum, void *closure), void *closure)
53 {
54         struct job *j;
55
56         j = malloc(sizeof*j);
57         if (!j) {
58                 errno = ENOMEM;
59                 return -1;
60         }
61
62         j->next = 0;
63         j->group = group;
64         j->timeout = timeout;
65         j->callback = callback;
66         j->closure = closure;
67
68         pthread_mutex_lock(&mutex);
69         if (first)
70                 last->next = j;
71         else
72                 first = j;
73         last = j;
74         pthread_mutex_unlock(&mutex);
75         return 0;
76 }
77
78 static void *thrrun(void *arg)
79 {
80         struct job *j;
81
82         pthread_mutex_lock(&mutex);
83         j = first;
84         if (j)
85                 first = j->next;
86         pthread_mutex_unlock(&mutex);
87         if (j) {
88                 j->callback(0, j->closure);
89                 free(j);
90         }
91         return 0;
92 }
93
94 int jobs_queue(
95         const void *group,
96         int timeout,
97         void (*callback)(int signum, void* arg),
98         void *arg)
99 {
100         pthread_t tid;
101         int rc = add_job(group, timeout, callback, arg);
102         if (!rc) {
103                 rc = pthread_create(&tid, NULL, thrrun, NULL);
104                 if (rc)
105                         rc = -1;
106         }
107         return rc;
108 }
109
110 #if 0
111 int jobs_enter(
112         const void *group,
113         int timeout,
114         void (*callback)(int signum, void *closure, struct jobloop *jobloop),
115         void *closure)
116 {
117         return 0;
118 }
119
120 int jobs_leave(struct jobloop *jobloop)
121 {
122         return 0;
123 }
124
125 int jobs_call(
126         const void *group,
127         int timeout,
128         void (*callback)(int, void*),
129         void *arg)
130 {
131         return 0;
132 }
133
134 struct sd_event *jobs_get_sd_event()
135 {
136         struct sd_event *r;
137         int rc = sd_event_default(&r);
138         return rc < 0 ? NULL : r;
139 }
140
141 void jobs_terminate()
142 {
143 }
144
145 int jobs_start(int allowed_count, int start_count, int waiter_count, void (*start)(int signum))
146 {
147         start(0);
148         return 0;
149 }
150 #endif