summaryrefslogtreecommitdiff
path: root/mm/src/code/sys_slowly.c
blob: 5a26ba6bd20947fae487c73cc23c366b5cd3879d (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
/**
 * @file sys_slowly.c
 *
 * This file implements a manager for running an asynchronous task on a thread with the lowest priority.
 *
 * The task callback is expected to have up to 2 void* arguments and have a void return. Setting `argCount` will adjust
 * how many args the callback gets called with, but defaults to 2 and using the 2 argument callback.
 *
 * @note: `argCount` must be set manually, as this file implements no way to configure it.
 */

#include "slowly.h"
#include "global.h"
#include "stackcheck.h"
#include "z64thread.h"
#include <string.h>

void Slowly_Main(SlowlyMgr* slowly) {
    slowly->status |= SLOWLY_STATUS_STARTED;

    switch (slowly->argCount) {
        case 0:
            slowly->callback.zero();
            break;

        case 1:
            slowly->callback.one(slowly->arg0);
            break;

        case 2:
            slowly->callback.two(slowly->arg0, slowly->arg1);
            break;

        default:
            break;
    }

    slowly->status |= SLOWLY_STATUS_DONE;
}

void Slowly_ThreadEntry(void* arg) {
    SlowlyMgr* slowly = (SlowlyMgr*)arg;

    Slowly_Main(slowly);
}

void Slowly_Init(SlowlyMgr* slowly, void* stack, SlowlyCallbackTwo callback, void* arg0, void* arg1) {
    memset(slowly, 0, sizeof(SlowlyMgr));

    slowly->argCount = 2;
    slowly->status = 0;
    slowly->callback.two = callback;
    slowly->arg0 = arg0;
    slowly->arg1 = arg1;

    osCreateThread(&slowly->thread, Z_THREAD_ID_SLOWLY, Slowly_ThreadEntry, slowly, stack, Z_PRIORITY_SLOWLY);
    osStartThread(&slowly->thread);
}

void Slowly_Destroy(SlowlyMgr* slowly) {
    osDestroyThread(&slowly->thread);
}