summaryrefslogtreecommitdiff
path: root/src/lib/tpm.c
blob: ff0a22df64b4df676a3e40a8f599be2bd5c9396e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Ouroboros - Copyright (C) 2016 - 2020
 *
 * Threadpool management
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@ugent.be>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#define _POSIX_C_SOURCE 200112L

#include "config.h"

#include <ouroboros/errno.h>
#include <ouroboros/list.h>
#include <ouroboros/time_utils.h>
#include <ouroboros/tpm.h>

#include <pthread.h>
#include <stdlib.h>
#include <assert.h>

#define TPM_TIMEOUT 1000

struct pthr_el {
        struct list_head next;

        bool             kill;
        bool             busy;

        pthread_t        thr;
};

enum tpm_state {
        TPM_NULL = 0,
        TPM_INIT,
        TPM_RUNNING
};

struct tpm {
        size_t           min;
        size_t           inc;
        size_t           cur;
        size_t           wrk;

        void * (* func)(void *);
        void *           o;

        struct list_head pool;

        enum tpm_state   state;
        pthread_cond_t   cond;
        pthread_mutex_t  lock;

        pthread_t        mgr;
};

static void tpm_join(struct tpm * tpm)
{
        struct list_head * p;
        struct list_head * h;

        list_for_each_safe(p, h, &tpm->pool) {
                struct pthr_el * e = list_entry(p, struct pthr_el, next);
                if (tpm->state != TPM_RUNNING) {
                        if (!e->kill) {
                                e->kill = true;
                                pthread_cancel(e->thr);
                                --tpm->cur;
                        }
                }
        }

        list_for_each_safe(p, h, &tpm->pool) {
                struct pthr_el * e = list_entry(p, struct pthr_el, next);
                if (e->kill) {
                        pthread_t thr = e->thr;
                        list_del(&e->next);
                        free(e);
                        pthread_mutex_unlock(&tpm->lock);

                        pthread_join(thr, NULL);

                        pthread_mutex_lock(&tpm->lock);
                }
        }
}

static void tpm_kill(struct tpm * tpm)
{
        struct list_head * p;

        list_for_each(p, &tpm->pool) {
                struct pthr_el * e = list_entry(p, struct pthr_el, next);
                if (!e->busy && !e->kill) {
                        e->kill = true;
                        pthread_cancel(e->thr);
                        --tpm->cur;
                        return;
                }
        }
}

static void * tpmgr(void * o)
{
        struct timespec dl;
        struct timespec to = {(TPM_TIMEOUT / 1000),
                              (TPM_TIMEOUT % 1000) * MILLION};
        struct tpm * tpm = (struct tpm *) o;

        while (true) {
                clock_gettime(PTHREAD_COND_CLOCK, &dl);
                ts_add(&dl, &to, &dl);

                pthread_mutex_lock(&tpm->lock);

                if (tpm->state != TPM_RUNNING) {
                        tpm_join(tpm);
                        pthread_mutex_unlock(&tpm->lock);
                        break;
                }

                tpm_join(tpm);

                if (tpm->cur - tpm->wrk < tpm->min) {
                        size_t i;
                        for (i = 0; i < tpm->inc; ++i) {
                                struct pthr_el * e = malloc(sizeof(*e));
                                if (e == NULL)
                                        break;

                                e->kill = false;
                                e->busy = false;

                                if (pthread_create(&e->thr, NULL,
                                                   tpm->func, tpm->o)) {
                                        free(e);
                                        break;
                                }

                                list_add(&e->next, &tpm->pool);
                        }

                        tpm->cur += i;
                }

                if (pthread_cond_timedwait(&tpm->cond, &tpm->lock, &dl)
                    == ETIMEDOUT)
                        if (tpm->cur - tpm->wrk > tpm->min)
                                tpm_kill(tpm);

                pthread_mutex_unlock(&tpm->lock);
        }

        return (void *) 0;
}

struct tpm * tpm_create(size_t min,
                        size_t inc,
                        void * (* func)(void *),
                        void * o)
{
        struct tpm *       tpm;
        pthread_condattr_t cattr;

        tpm = malloc(sizeof(*tpm));
        if (tpm == NULL)
                goto fail_malloc;

        if (pthread_mutex_init(&tpm->lock, NULL))
                goto fail_lock;

        if (pthread_condattr_init(&cattr))
                goto fail_cattr;

#ifndef __APPLE__
        pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
#endif
        if (pthread_cond_init(&tpm->cond, &cattr))
                goto fail_cond;

        list_head_init(&tpm->pool);

        pthread_condattr_destroy(&cattr);

        tpm->state = TPM_INIT;
        tpm->func  = func;
        tpm->o     = o;
        tpm->min   = min;
        tpm->inc   = inc;
        tpm->cur   = 0;
        tpm->wrk   = 0;

        return tpm;

 fail_cond:
        pthread_condattr_destroy(&cattr);
 fail_cattr:
        pthread_mutex_destroy(&tpm->lock);
 fail_lock:
        free(tpm);
 fail_malloc:
        return NULL;
}

int tpm_start(struct tpm * tpm)
{
        pthread_mutex_lock(&tpm->lock);

        if (pthread_create(&tpm->mgr, NULL, tpmgr, tpm)) {
                pthread_mutex_unlock(&tpm->lock);
                return -1;
        }

        tpm->state = TPM_RUNNING;

        pthread_mutex_unlock(&tpm->lock);

        return 0;
}

void tpm_stop(struct tpm * tpm)
{
        pthread_mutex_lock(&tpm->lock);

        tpm->state = TPM_NULL;

        pthread_mutex_unlock(&tpm->lock);
}

void tpm_destroy(struct tpm * tpm)
{
        pthread_join(tpm->mgr, NULL);

        pthread_mutex_destroy(&tpm->lock);
        pthread_cond_destroy(&tpm->cond);

        free(tpm);
}

static struct pthr_el * tpm_pthr_el(struct tpm * tpm,
                                    pthread_t    thr)
{
        struct list_head * p;
        struct pthr_el *   e;

        list_for_each(p, &tpm->pool) {
                e = list_entry(p, struct pthr_el, next);
                if (e->thr == thr)
                        return e;

        }

        return NULL;
}

void tpm_inc(struct tpm * tpm)
{
        struct pthr_el * e;

        pthread_mutex_lock(&tpm->lock);

        e = tpm_pthr_el(tpm, pthread_self());
        if (e != NULL) {
                e->busy = false;
                --tpm->wrk;
        }

        pthread_mutex_unlock(&tpm->lock);
}

void tpm_dec(struct tpm * tpm)
{
        struct pthr_el * e;

        pthread_mutex_lock(&tpm->lock);

        e = tpm_pthr_el(tpm, pthread_self());
        if (e != NULL) {
                e->busy = true;
                ++tpm->wrk;
        }

        pthread_cond_signal(&tpm->cond);

        pthread_mutex_unlock(&tpm->lock);
}