summaryrefslogtreecommitdiff
path: root/docs/application/thread.md
blob: b3f8d21fd31e28092386674a6d45c334bf3df05e (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Threads

---

## Object Definitions

```c
typedef struct _kpl_task kpl_task;

typedef void kpl_task_fn(kpl_task *t);

typedef struct _kpl_task {
    _Atomic bool join_ready;
    int32_t thread_id;
    kpl_group *state;
    kpl_result ret;
    kpl_task *_Atomic next, *join;
    task_fn *fn;
} kpl_task;

#define KPL_TASK_SLAB_SIZE 50

typedef struct _kpl_task_slab {
    size_t array_index;
    struct _task_slab *next;
    kpl_task array[KPL_TASK_SLAB_SIZE];
} kpl_task_slab;

#define KPL_QUEUE_ASYNC -1

#define KPL_MAIN_THREAD 0

typedef struct {
    kpl_task *_Atomic head, *_Atomic tail, dummy;
} kpl_atomic_queue;

typedef struct {
    kpl_atomic_queue queue;
    _Atomic ssize_t priority;
    kpl_task_slab *slab;
    kpl_task *pool;
    sem_t counter;
    pthread_t thread;
} thread;
```

## Initialization

On the start of each thread, set the cpu affinity to its thread id

## Task State

```text
0 -> length
native : arguments
process : arguments, locals, parent
closure : arguments, locals, closure function
iterator : arguments, locals, iterator functions, iterator function index
```

## Per Process Task Queue

## Asynchronous Task Queue

# Queuing

## Sync

## Async

# Running

## Joining

# Example

```c
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
#include <semaphore.h>
#include <sched.h>
#include <assert.h>

#define I 50

#define FIB 25

typedef struct _task task;

typedef void task_fn(task *t);

#define TASK_STATE_SIZE 55

typedef struct _task {
    _Atomic bool join_ready;
    int32_t thread_id;
    void *state[TASK_STATE_SIZE];
    void *return_value;
    task *_Atomic next, *join;
    task_fn *fn;
} task;

#define TASK_SLAB_SIZE 50

typedef struct _task_slab {
    size_t array_index;
    struct _task_slab *next;
    task array[TASK_SLAB_SIZE];
} task_slab;

#define QUEUE_ASYNC -1

#define MAIN_THREAD 0

typedef struct {
    task *_Atomic head, *_Atomic tail, dummy;
} atomic_queue;

typedef struct {
    atomic_queue queue;
    _Atomic ssize_t priority;
    task_slab *slab;
    task *pool;
    sem_t counter;
    pthread_t thread;
} thread;

int32_t avaiable_threads = {};

#define MAX_THREADS 64

thread threads[MAX_THREADS];

_Atomic ssize_t total_priority = {};

void priority_increment(int32_t thread_id) {
    threads[thread_id].priority++;
    total_priority++;
}

void priority_decrement(int32_t thread_id) {
    threads[thread_id].priority--;
    total_priority--;
}

void task_slab_init(int32_t thread_id) {
    task_slab *slab = calloc(1, sizeof(task_slab));
    slab->next = threads[thread_id].slab;
    threads[thread_id].slab = slab;
}

task *task_slab_get(int32_t thread_id) {
    if (threads[thread_id].slab->array_index < TASK_SLAB_SIZE)
        return &threads[thread_id].slab->array[threads[thread_id].slab->array_index++];
    task_slab_init(thread_id);
    return task_slab_get(thread_id);
}

void task_slab_free(int32_t thread_id) {
    task_slab *slab = threads[thread_id].slab;
    while (slab) {
        task_slab *tmp = slab;
        slab = slab->next;
        free(tmp);
    }
}

void task_queue_init(int32_t thread_id) {
    threads[thread_id].queue.head = &threads[thread_id].queue.dummy;
    threads[thread_id].queue.tail = &threads[thread_id].queue.dummy;
    threads[thread_id].queue.dummy.next = NULL;
    threads[thread_id].priority = 1;
    threads[thread_id].pool = NULL;
}

void task_queue_add(int32_t thread_id, task *t) {
    t->next = NULL;
    task *head = __atomic_exchange_n(&threads[thread_id].queue.head, t, __ATOMIC_SEQ_CST);
    head->next = t;
    if (t != &threads[thread_id].queue.dummy)
        priority_increment(thread_id);
}

task *task_queue_next(int32_t thread_id) {
    task *t = NULL;
    for (;;) {
        task *tail = threads[thread_id].queue.tail, *next = tail->next;
        if (tail == &threads[thread_id].queue.dummy) {
            if (!next)
                break;
            threads[thread_id].queue.tail = next;
            tail = next;
            next = tail->next;
        }
        if (next) {
            threads[thread_id].queue.tail = next;
            t = tail;
            break;
        }
        task *head = threads[thread_id].queue.head;
        if (tail != head)
            continue;
        task_queue_add(thread_id, &threads[thread_id].queue.dummy);
        next = tail->next;
        if (next) {
            threads[thread_id].queue.tail = next;
            t = tail;
            break;
        }
    }
    if (t) {
        t->next = NULL;
        priority_decrement(thread_id);
    }
    return t;
}

task *task_init(task_fn *fn, int32_t thread_id) {
    task *t = NULL;
    if (threads[thread_id].pool) {
        t = threads[thread_id].pool;
        threads[thread_id].pool = threads[thread_id].pool->join;
    }
    if (!t)
        t = task_slab_get(thread_id);
    __atomic_clear(&t->join_ready, __ATOMIC_SEQ_CST);
    t->thread_id = QUEUE_ASYNC;
    for (ssize_t state_id = 0; state_id < TASK_STATE_SIZE; state_id++)
        t->state[state_id] = NULL;
    t->return_value = NULL;
    t->next = NULL;
    t->join = NULL;
    t->fn = fn;
    return t;
}

void task_free(task *t) {
    t->join = threads[t->thread_id].pool;
    threads[t->thread_id].pool = t;
}

void task_queue_async(task *t) {
    int32_t queue_thread_id = 0;
    ssize_t queue_priority = threads[0].priority;
    for (int32_t thread_id = 1; thread_id < avaiable_threads; thread_id++) {
        if (threads[thread_id].priority < queue_priority) {
            queue_priority = threads[thread_id].priority;
            queue_thread_id = thread_id;
        }
    }
    task_queue_add(queue_thread_id, t);
    sem_post(&threads[queue_thread_id].counter);
}

void task_done(task *t) {
    if (!__atomic_test_and_set(&t->join_ready, __ATOMIC_SEQ_CST) || !t->join)
        return;
    task *join = t->join;
    t->join = NULL;
    __atomic_clear(&t->join_ready, __ATOMIC_SEQ_CST);
    if (join->thread_id == t->thread_id)
        task_queue_add(t->thread_id, join);
    else
        task_queue_async(join);
}

void task_join(task *restrict t, task *restrict join, task_fn *fn) {
    join->fn = fn;
    t->join = join;
    task_done(t);
}

void *task_loop(void *arg) {
    const int32_t thread_id = (int32_t) (intptr_t) arg;
    cpu_set_t cpus;
    CPU_ZERO(&cpus);
    CPU_SET(thread_id, &cpus);
    if (pthread_setaffinity_np(pthread_self(), sizeof(cpus), &cpus))
        exit(thread_id + 1);
    for (;;) {
        task *t = task_queue_next(thread_id);
        if (t) {
            t->thread_id = thread_id;
            t->fn(t);
            continue;
        }
        priority_decrement(thread_id);
        if (!total_priority) {
            for (int32_t thread_id = 0; thread_id < avaiable_threads; thread_id++)
                sem_post(&threads[thread_id].counter);
            break;
        }
        sem_wait(&threads[thread_id].counter);
        if (!total_priority)
            break;
        priority_increment(thread_id);
    }
    return NULL;
}

void task_run() {
    for (int32_t thread_id = 1; thread_id < avaiable_threads; thread_id++)
        pthread_create(&threads[thread_id].thread, NULL, task_loop, (void*) (intptr_t) thread_id);
    task_loop(0);
    for (int32_t thread_id = 1; thread_id < avaiable_threads; thread_id++)
        pthread_join(threads[thread_id].thread, NULL);
}

void task_constructor(void) {
    cpu_set_t cpus;
    CPU_ZERO(&cpus);
    sched_getaffinity(0, sizeof(cpus), &cpus);
    total_priority = avaiable_threads = CPU_COUNT(&cpus);
    for (int32_t thread_id = 0; thread_id < avaiable_threads; thread_id++) {
        task_queue_init(thread_id);
        task_slab_init(thread_id);
        sem_init(&threads[thread_id].counter, 0, 1);
    }
}

void task_destructor(void) {
    for (int32_t thread_id = 0; thread_id < avaiable_threads; thread_id++) {
        sem_destroy(&threads[thread_id].counter);
        task_slab_free(thread_id);
    }
}

void fib_c(task *t) {
    task *child_a = t->state[2], *child_b = t->state[3];
    t->return_value = (void*) ((intptr_t) child_a->return_value + (intptr_t) child_b->return_value);
    task_free(child_a);
    task_free(child_b);
    task_done(t);
}

void fib_a(task *t);

void fib_b(task *t) {
    intptr_t arg = (intptr_t) t->state[1];
    task *child_b = t->state[3] = task_init(fib_a, t->thread_id);
    child_b->state[1] = (void*) (arg - 2);
    task_queue_add(t->thread_id, child_b);
    task_join(child_b, t, fib_c);
}

void fib_a(task *t) {
    intptr_t arg = (intptr_t) t->state[1];
    if (arg <= 0) {
        t->return_value = (void*) 0;
        task_done(t);
        return;
    }
    if (arg < 2) {
        t->return_value = (void*) 1;
        task_done(t);
        return;
    }
    task *child_a = t->state[2] = task_init(fib_a, t->thread_id);
    child_a->state[1] = (void*) (arg - 1);
    task_queue_add(t->thread_id, child_a);
    task_join(child_a, t, fib_b);
}

_Atomic size_t counter = 0;

void start_b(task *t) {
    counter++;
    task *f = t->state[(intptr_t) t->state[0]];
    intptr_t i = (intptr_t) f->state[0], arg = (intptr_t) f->state[1], ret = (intptr_t) f->return_value;
    printf("i: %ld, fib(%ld) = %ld, thread: %d\n", i, arg, ret, f->thread_id);
    task_free(f);
    t->state[0]++;
    if ((intptr_t) t->state[0] == I + 1) {
        printf("Complete\n");
        task_done(t);
        task_free(t);
        return;
    }
    task_join(t->state[(intptr_t) t->state[0]], t, start_b);
}

void start_a(task *t) {
    t->state[0] = (void*) 1;
    for (intptr_t i = 1; i <= I; i++) {
        task *f = task_init(fib_a, t->thread_id);
        f->state[0] = (void*) i;
        f->state[1] = (void*) FIB;
        t->state[i] = f;
        task_queue_async(f);
    }
    task_join(t->state[(intptr_t) t->state[0]], t, start_b);
}

int main(void) {
    task_constructor();
    task *t = task_init(start_a, MAIN_THREAD);
    task_queue_add(MAIN_THREAD, t);
    task_run();
    task_destructor();
    assert(counter == I);
    return 0;
}
```