aboutsummaryrefslogtreecommitdiff
path: root/examples/coroutine.tm
blob: 1d3d4dcf68a5f1517a31148c75ca1ea1893dc564 (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
# This is a coroutine library that uses libaco. If you don't have it installed,
# you can get it from the website: https://libaco.org
#
# Lua programmers will recognize this as similar to Lua's stackful coroutines.
#
# Async/Await programmers will weep at its beauty and gnash their teeth and
# rend their garments in despair at what they could have had.

use libaco.so
use <aco.h>

func main():
    !! Example usage:
    co := new(func():
        say("I'm in the coroutine!")
        yield()
        say("I'm back in the coroutine!")
    )
    >> co
    say("I'm in the main func")
    >> co:resume()
    say("I'm back in the main func")
    >> co:resume()
    say("I'm back in the main func again")
    >> co:resume()

_main_co := !@Memory
_shared_stack := !@Memory

struct Coroutine(co:@Memory):
    func is_finished(co:Coroutine; inline)->Bool:
        return inline C (
            ((aco_t*)$co.$co)->is_finished
        ):Bool

    func resume(co:Coroutine)->Bool:
        if co:is_finished():
            return no
        inline C {
            aco_resume($co.$co);
        }
        return yes

func _init():
    inline C {
        aco_set_allocator(GC_malloc, NULL);
        aco_thread_init(aco_exit_fn);
    }
    _main_co = inline C(
        aco_create(NULL, NULL, 0, NULL, NULL)
    ):@Memory

    _shared_stack = inline C(
        aco_shared_stack_new(0);
    ):@Memory

func new(co:func())->Coroutine:
    if not _main_co:
        _init()

    main_co := _main_co
    shared_stack := _shared_stack
    aco_ptr := inline C (
        aco_create($main_co, $shared_stack, 0, (void*)$co.fn, $co.userdata)
    ):@Memory
    return Coroutine(aco_ptr)
            
func yield(; inline):
    inline C {
        aco_yield();
    }