source: src/examples/thread.c @ c84e80a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c84e80a was c84e80a, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Kernel now supports [0-9] cfa threads on a single core, using round-robin scheduling and no preemption

  • 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        unsigned id;
20        unsigned count;
21};
22
23void ?{}( MyThread * this ) {
24        this->id = 0;
25        this->count = 10;
26}
27
28void ?{}( MyThread * this, unsigned id, unsigned count ) {
29        this->id = id;
30        this->count = count;
31}
32
33void ^?{}( MyThread * this ) {}
34
35void main(MyThread* this) {
36        printf("Main called with %p\n", this);
37        printf("Thread %d : Suspending %d times\n", this->id, this->count);
38
39        for(int i = 0; i < this->count; i++) {
40                printf("Thread %d : Suspend No. %d\n", this->id, i + 1);
41                printf("Back to %p\n", &this->t.c);
42                suspend();
43        }
44}
45
46thread_h* get_thread(MyThread* this) {
47        return &this->t;
48}
49
50coroutine* get_coroutine(MyThread* this) {
51        return &this->t.c;
52}
53
54int main() {
55
56        thread(MyThread) thread1;
57        thread(MyThread) thread2;
58
59        thread2.handle.id = 1;
60
61        printf("\n\nMain is %p\n", this_coroutine());
62
63        kernel_run();
64
65        printf("Kernel terminated correctly\n");
66
67        return 0;
68}
Note: See TracBrowser for help on using the repository browser.