source: src/examples/thread.c @ c49bf54

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since c49bf54 was c49bf54, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

First thread example

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include <kernel>
2#include <stdlib>
3#include <threads>
4
5// Start coroutine routines
6extern "C" {
7      forall(dtype T | is_coroutine(T))
8      void CtxInvokeCoroutine(T * this);
9
10      forall(dtype T | is_coroutine(T))
11      void CtxStart(T * this, void ( *invoke)(T *));
12
13        forall(dtype T | is_coroutine(T))
14      void CtxInvokeThread(T * this);
15}
16
17struct MyThread {
18        thread_h t;
19        int value;
20};
21
22void ?{}( MyThread * this, int value ) {
23        this->value = value;
24}
25
26void ^?{}( MyThread * this ) {}
27
28void co_main(MyThread* this) {
29        printf("Main called with %p\n", this);
30        printf("Thread value is %d\n", this->value);
31}
32
33thread_h* get_thread(MyThread* this) {
34        return &this->t;
35}
36
37coroutine* get_coroutine(MyThread* this) {
38        return &this->t.c;
39}
40
41void ?{}( MyThread * this ) {
42        this->value = 1;
43        printf("MyThread created\n");
44        printf("Address %p\n", this);
45        printf("handle %p\n", get_thread(this));
46        printf("main %p\n", co_main);
47        printf("get_t %p\n", get_thread);
48        printf("invoke %p\n", CtxInvokeThread);
49}
50
51int main() {
52
53        printf("Main is %p\n", this_coroutine());
54
55        printf("Creating thread\n");
56
57        thread(MyThread) thread1;
58
59        printf("Main is %p\n", this_coroutine());       
60
61        printf("First thread created\n");
62
63        kernel_run();
64
65        return 0;
66}
Note: See TracBrowser for help on using the repository browser.