summaryrefslogtreecommitdiff
path: root/coroutine/pthread/Context.c
blob: 38774cda0b409d0ba6a743dfbd5ac32cc2c90b07 (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
/*
 *  This file is part of the "Coroutine" project and released under the MIT License.
 *
 *  Created by Samuel Williams on 24/6/2021.
 *  Copyright, 2021, by Samuel Williams.
*/

#include "Context.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

static const int DEBUG = 0;

static
int check(const char * message, int result) {
    if (result) {
        switch (result) {
            case EDEADLK:
                if (DEBUG) fprintf(stderr, "deadlock detected result=%d errno=%d\n", result, errno);
                break;
            default:
                if (DEBUG) fprintf(stderr, "error detected result=%d errno=%d\n", result, errno);
                perror(message);
        }
    }

    assert(result == 0);

    return result;
}

void coroutine_initialize_main(struct coroutine_context * context) {
    context->id = pthread_self();

    check("coroutine_initialize_main:pthread_cond_init",
        pthread_cond_init(&context->schedule, NULL)
    );

    context->shared = (struct coroutine_shared*)malloc(sizeof(struct coroutine_shared));
    assert(context->shared);

    context->shared->main = context;
    context->shared->count = 1;

    if (DEBUG) {
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

        check("coroutine_initialize_main:pthread_mutex_init",
            pthread_mutex_init(&context->shared->guard, &attr)
        );
    } else {
        check("coroutine_initialize_main:pthread_mutex_init",
            pthread_mutex_init(&context->shared->guard, NULL)
        );
    }
}

static
void coroutine_release(struct coroutine_context *context) {
    if (context->shared) {
        size_t count = (context->shared->count -= 1);

        if (count == 0) {
            if (DEBUG) fprintf(stderr, "coroutine_release:pthread_mutex_destroy(%p)\n", &context->shared->guard);
            pthread_mutex_destroy(&context->shared->guard);
            free(context->shared);
        }

        context->shared = NULL;

        if (DEBUG) fprintf(stderr, "coroutine_release:pthread_cond_destroy(%p)\n", &context->schedule);
        pthread_cond_destroy(&context->schedule);
    }
}

void coroutine_initialize(
    struct coroutine_context *context,
    coroutine_start start,
    void *stack,
    size_t size
) {
    assert(start && stack && size >= 1024);

    // We will create the thread when we first transfer, but save the details now:
    context->shared = NULL;
    context->start = start;
    context->stack = stack;
    context->size = size;
}

static
int is_locked(pthread_mutex_t * mutex) {
    int result = pthread_mutex_trylock(mutex);

    // If we could successfully lock the mutex:
    if (result == 0) {
        pthread_mutex_unlock(mutex);
        // We could lock the mutex, so it wasn't locked:
        return 0;
    } else {
        // Otherwise we couldn't lock it because it's already locked:
        return 1;
    }
}

static
void coroutine_guard_unlock(void * _context)
{
    struct coroutine_context * context = _context;

    if (DEBUG) fprintf(stderr, "coroutine_guard_unlock:pthread_mutex_unlock\n");

    check("coroutine_guard_unlock:pthread_mutex_unlock",
        pthread_mutex_unlock(&context->shared->guard)
    );
}

static
void coroutine_wait(struct coroutine_context *context)
{
    if (DEBUG) fprintf(stderr, "coroutine_wait:pthread_mutex_lock(guard=%p is_locked=%d)\n", &context->shared->guard, is_locked(&context->shared->guard));
    check("coroutine_wait:pthread_mutex_lock",
        pthread_mutex_lock(&context->shared->guard)
    );

    if (DEBUG) fprintf(stderr, "coroutine_wait:pthread_mutex_unlock(guard)\n");
    pthread_mutex_unlock(&context->shared->guard);
}

static
void coroutine_trampoline_cleanup(void *_context) {
    struct coroutine_context * context = _context;
    coroutine_release(context);
}

void * coroutine_trampoline(void * _context)
{
    struct coroutine_context * context = _context;
    assert(context->shared);

    pthread_cleanup_push(coroutine_trampoline_cleanup, context);

    coroutine_wait(context);

    context->start(context->from, context);

    pthread_cleanup_pop(1);

    return NULL;
}

static
int coroutine_create_thread(struct coroutine_context *context)
{
    int result;

    pthread_attr_t attr;
    result = pthread_attr_init(&attr);
    if (result != 0) {
        return result;
    }

    result = pthread_attr_setstack(&attr, context->stack, (size_t)context->size);
    if (result != 0) {
        pthread_attr_destroy(&attr);
        return result;
    }

    result = pthread_cond_init(&context->schedule, NULL);
    if (result != 0) {
        pthread_attr_destroy(&attr);
        return result;
    }

    result = pthread_create(&context->id, &attr, coroutine_trampoline, context);
    if (result != 0) {
        pthread_attr_destroy(&attr);
        if (DEBUG) fprintf(stderr, "coroutine_create_thread:pthread_cond_destroy(%p)\n", &context->schedule);
        pthread_cond_destroy(&context->schedule);
        return result;
    }

    context->shared->count += 1;

    return result;
}

struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target)
{
    assert(current->shared);

    struct coroutine_context * previous = target->from;
    target->from = current;

    if (DEBUG) fprintf(stderr, "coroutine_transfer:pthread_mutex_lock(guard=%p is_locked=%d)\n", &current->shared->guard, is_locked(&current->shared->guard));
    pthread_mutex_lock(&current->shared->guard);
    pthread_cleanup_push(coroutine_guard_unlock, current);

    // First transfer:
    if (target->shared == NULL) {
        target->shared = current->shared;

        if (DEBUG) fprintf(stderr, "coroutine_transfer:coroutine_create_thread...\n");
        if (coroutine_create_thread(target)) {
            if (DEBUG) fprintf(stderr, "coroutine_transfer:coroutine_create_thread failed\n");
            target->shared = NULL;
            target->from = previous;
            return NULL;
        }
    } else {
        if (DEBUG) fprintf(stderr, "coroutine_transfer:pthread_cond_signal(target)\n");
        pthread_cond_signal(&target->schedule);
    }

    // A side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation cleanup handler. If cancelled, pthread_cond_wait immediately invokes cleanup handlers.
    if (DEBUG) fprintf(stderr, "coroutine_transfer:pthread_cond_wait(schedule=%p, guard=%p, is_locked=%d)\n", &current->schedule, &current->shared->guard, is_locked(&current->shared->guard));
    check("coroutine_transfer:pthread_cond_wait",
        pthread_cond_wait(&current->schedule, &current->shared->guard)
    );

    if (DEBUG) fprintf(stderr, "coroutine_transfer:pthread_cleanup_pop\n");
    pthread_cleanup_pop(1);

#ifdef __FreeBSD__
    // Apparently required for FreeBSD:
    pthread_testcancel();
#endif

    target->from = previous;

    return target;
}

static
void coroutine_join(struct coroutine_context * context) {
    if (DEBUG) fprintf(stderr, "coroutine_join:pthread_cancel\n");
    int result = pthread_cancel(context->id);
    if (result == -1 && errno == ESRCH) {
        // The thread may be dead due to fork, so it cannot be joined and this doesn't represent a real error:
        return;
    }

    check("coroutine_join:pthread_cancel", result);

    if (DEBUG) fprintf(stderr, "coroutine_join:pthread_join\n");
    check("coroutine_join:pthread_join",
        pthread_join(context->id, NULL)
    );

    if (DEBUG) fprintf(stderr, "coroutine_join:pthread_join done\n");
}

void coroutine_destroy(struct coroutine_context * context)
{
    if (DEBUG) fprintf(stderr, "coroutine_destroy\n");

    assert(context);

    // We are already destroyed or never created:
    if (context->shared == NULL) return;

    if (context == context->shared->main) {
        context->shared->main = NULL;
        coroutine_release(context);
    } else {
        coroutine_join(context);
        assert(context->shared == NULL);
    }
}