summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/monkey/mk_core/mk_thread.c
blob: 9b5589f3615f43571aa31830b5a8267d39c3e86d (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Monkey HTTP Server (Duda I/O)
 *  -----------------------------
 *  Copyright 2017 Eduardo Silva <eduardo@monkey.io>
 *  Copyright 2014, Zeying Xie <swpdtz at gmail dot com>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <assert.h>
#include <string.h>

#if defined (__APPLE__)
#include <sys/ucontext.h>
#else
#include <ucontext.h>
#endif

#include <limits.h>

#include <mk_core/mk_pthread.h>
#include <mk_core/mk_memory.h>
#include <mk_core/mk_thread.h>

/*
 * @OBJ_NAME: dthread
 * @OBJ_MENU: Dthread
 * @OBJ_DESC: The dthread object provides a set of methods to handle user space cooperative thread, namely dthread(duda thread).
 * A dthread can be suspended when it encounters something that will block(in other
 * words, something will be available in the future), while another dthread that
 * is ready to run is awakened. Back and forth, all dthreads within the same pthread
 * work collaboratively. This means dthread is non-preemptive and requires the user
 * to explicitly give up control when necessary.
 * Dthreads communicate with each other by using channel, a channel is like a pipe,
 * one dthread feeds data to the channel while another cosumes from it.
 *
 */

#ifdef USE_VALGRIND
#include <valgrind/valgrind.h>
#endif

#define MK_THREAD_STACK_SIZE (3 * (PTHREAD_STACK_MIN) / 2)
#define DEFAULT_MK_THREAD_NUM    16

struct mk_thread {
    mk_thread_func func;
    void *data;
    ucontext_t context;
    struct mk_thread_scheduler *sch;
    int status;
    int parent_id;
#ifdef USE_VALGRIND
    unsigned int valgrind_stack_id;
#endif
    struct mk_list chan_list;
    char stack[MK_THREAD_STACK_SIZE];
} mk_thread_t;

struct mk_thread_scheduler {
    ucontext_t main;
    int n_dthread;
    int cap;
    int running_id;
    struct mk_thread **dt;
};

static void _mk_thread_release(struct mk_thread *dt);

static void _mk_thread_entry_point(struct mk_thread_scheduler *sch)
{
    int id;
    struct mk_thread *dt;
    struct mk_list *head;
    struct mk_thread_channel *chan;

    assert(sch);
    id = sch->running_id;
    dt = sch->dt[id];
    dt->func(dt->data);
    dt->status = MK_THREAD_DEAD;

    mk_list_foreach(head, &dt->chan_list) {
        chan = mk_list_entry(head, struct mk_thread_channel, _head);
        chan->receiver = -1;
    }
    sch->n_dthread--;
    sch->running_id = dt->parent_id;
}

struct mk_thread_scheduler *mk_thread_open()
{
    struct mk_thread_scheduler *sch;

    sch = mk_mem_alloc(sizeof(*sch));
    if (!sch) {
        return NULL;
    }

    sch->n_dthread = 0;
    sch->cap = DEFAULT_MK_THREAD_NUM;
    sch->running_id = -1;
    sch->dt = mk_mem_alloc_z(sizeof(struct mk_thread *) * sch->cap);
    if (!sch->dt) {
        mk_mem_free(sch);
        return NULL;
    }

    return sch;
}

void mk_thread_close(struct mk_thread_scheduler *sch)
{
    struct mk_thread *dt;

    int i;
    for (i = 0; i < sch->cap; ++i) {
        dt = sch->dt[i];
        if (dt) {
            _mk_thread_release(dt);
        }
    }
    mk_mem_free(sch->dt);
    sch->dt = NULL;
    mk_mem_free(sch);
}

/*
 * @METHOD_NAME: create
 * @METHOD_DESC: create a new dthread.
 * @METHOD_PROTO: int create(mk_thread_func func, void *data)
 * @METHOD_PARAM: func the function to be executed when the newly created dthread
 * is started.
 * @METHOD_PARAM: data user specific data that will be passed to func.
 * @METHOD_RETURN: the dthread id associated with the new dthread.
 */
int mk_thread_create(mk_thread_func func, void *data)
{
    int i;
    int id;
    void *p;
    struct mk_thread_scheduler *sch;
    struct mk_thread *dt;

    sch = pthread_getspecific(mk_thread_scheduler);
    if (!sch) {
        sch = mk_thread_open();
        assert(sch);
        pthread_setspecific(mk_thread_scheduler, (void *) sch);
    }

    if (sch->n_dthread >= sch->cap) {
        id = sch->cap;

        p = mk_mem_realloc(sch->dt, sch->cap * 2 * sizeof(struct mk_thread *));
        if (!p) {
            return -1;
        }
        sch->dt = p;
        memset(sch->dt + sch->cap, 0, sizeof(struct mk_thread *) * sch->cap);
        sch->cap *= 2;
    }
    else {
        for (i = 0; i < sch->cap; ++i) {
            id = (i + sch->cap) % sch->cap;
            if (sch->dt[id] == NULL || sch->dt[id]->status == MK_THREAD_DEAD) {
                break;
            }
        }
    }

    /* may use dthread pooling instead of release and realloc */
    if (sch->dt[id] && sch->dt[id]->status == MK_THREAD_DEAD) {
        _mk_thread_release(sch->dt[id]);
        sch->dt[id] = NULL;
    }

    dt = mk_mem_alloc(sizeof(*dt));
    if (!dt) {
        return -1;
    }

    dt->func = func;
    dt->data = data;
    dt->sch = sch;
    dt->status = MK_THREAD_READY;
    dt->parent_id = -1;
#ifdef USE_VALGRIND
    dt->valgrind_stack_id = VALGRIND_STACK_REGISTER(dt->stack, dt->stack + MK_THREAD_STACK_SIZE);
#endif
    mk_list_init(&dt->chan_list);
    sch->dt[id] = dt;
    sch->n_dthread++;
    return id;
}

static void _mk_thread_release(struct mk_thread *dt)
{
    assert(dt);
#ifdef USE_VALGRIND
    VALGRIND_STACK_DEREGISTER(dt->valgrind_stack_id);
#endif
    mk_mem_free(dt);
}

/*
 * @METHOD_NAME: status
 * @METHOD_DESC: get the status of a given dthread.
 * @METHOD_PROTO: int status(int id)
 * @METHOD_PARAM: id the dthread id of the target dthread.
 * @METHOD_RETURN: it returns one of the following status: MK_THREAD_DEAD, MK_THREAD_READY,
 * MK_THREAD_RUNNING, MK_THREAD_SUSPEND.
 */
int mk_thread_status(int id)
{
    struct mk_thread_scheduler *sch;

    sch = pthread_getspecific(mk_thread_scheduler);
    assert(sch);
    assert(id >= 0 && id < sch->cap);
    if (!sch->dt[id]) return MK_THREAD_DEAD;
    return sch->dt[id]->status;
}

/*
 * @METHOD_NAME: yield
 * @METHOD_DESC: require the currently running dthread explicitly to give up control
 * back to the dthread scheduler.
 * @METHOD_PROTO: void yield()
 * @METHOD_RETURN: this method do not return any value.
 */
void mk_thread_yield()
{
    int id;
    struct mk_thread *dt;
    struct mk_thread_scheduler *sch;

    sch = pthread_getspecific(mk_thread_scheduler);
    assert(sch);

    id = sch->running_id;
    assert(id >= 0);

    dt = sch->dt[id];
    dt->status = MK_THREAD_SUSPEND;
    sch->running_id = -1;
    swapcontext(&dt->context, &sch->main);
}

/*
 * @METHOD_NAME: resume
 * @METHOD_DESC: resume a given dthread and suspend the currently running dthread.
 * @METHOD_PROTO: void resume(int id)
 * @METHOD_PARAM: id the dthread id of the target dthread.
 * @METHOD_RETURN: this method do not return any value.
 */
void mk_thread_resume(int id)
{
    struct mk_thread *dt;
    struct mk_thread *running_dt;
    struct mk_thread_scheduler *sch;

    sch = pthread_getspecific(mk_thread_scheduler);
    assert(sch);
    assert(id >= 0 && id < sch->cap);

    running_dt = NULL;
    if (sch->running_id != -1) {
        running_dt = sch->dt[sch->running_id];
    }

    dt = sch->dt[id];
    if (!dt) return;
    switch (dt->status) {
    case MK_THREAD_READY:
        getcontext(&dt->context);
        dt->context.uc_stack.ss_sp = dt->stack;
        dt->context.uc_stack.ss_size = MK_THREAD_STACK_SIZE;
        if (running_dt) {
            dt->context.uc_link = &running_dt->context;
            dt->parent_id = sch->running_id;
            running_dt->status = MK_THREAD_SUSPEND;
        } else {
            dt->context.uc_link = &sch->main;
        }
        sch->running_id = id;
        dt->status = MK_THREAD_RUNNING;
        makecontext(&dt->context, (void (*)(void))_mk_thread_entry_point, 1, sch);
        if (running_dt) {
            swapcontext(&running_dt->context, &dt->context);
        } else {
            swapcontext(&sch->main, &dt->context);
        }
        break;
    case MK_THREAD_SUSPEND:
        sch->running_id = id;
        dt->status = MK_THREAD_RUNNING;
        if (running_dt) {
            running_dt->status = MK_THREAD_SUSPEND;
            swapcontext(&running_dt->context, &dt->context);
        } else {
            swapcontext(&sch->main, &dt->context);
        }
        break;
    default:
        assert(0);
    }
}

/*
 * @METHOD_NAME: running
 * @METHOD_DESC: get the id of the currently running dthread.
 * @METHOD_PROTO: int running()
 * @METHOD_RETURN: the dthread id associated with the currently running dthread.
 */
int mk_thread_running()
{
    struct mk_thread_scheduler *sch;

    sch = pthread_getspecific(mk_thread_scheduler);
    assert(sch);
    return sch->running_id;
}

void mk_thread_add_channel(int id, struct mk_thread_channel *chan)
{
    struct mk_thread_scheduler *sch;
    struct mk_thread *dt;

    assert(chan);
    sch = pthread_getspecific(mk_thread_scheduler);
    assert(sch);
    assert(id >= 0 && id < sch->cap);
    dt = sch->dt[id];
    mk_list_add(&chan->_head, &dt->chan_list);
}