Changeset a362f97 for src/examples
- Timestamp:
- Jan 27, 2017, 3:27:34 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- c0aa336
- Parents:
- 6acb935 (diff), 0a86a30 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src/examples
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/thread.c
r6acb935 ra362f97 1 #include <fstream> 1 2 #include <kernel> 2 3 #include <stdlib> … … 4 5 5 6 // Start coroutine routines 6 extern "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 17 7 struct MyThread { 18 thread _ht;8 thread t; 19 9 unsigned id; 20 10 unsigned count; 21 11 }; 22 12 13 DECL_THREAD(MyThread) 14 23 15 void ?{}( MyThread * this ) { 24 this->id = 0;25 this->count = 10;26 16 } 27 17 … … 31 21 } 32 22 33 void ^?{}( MyThread * this ) {}34 35 23 void main(MyThread* this) { 36 printf("Main called with %p\n", this);37 printf("Thread %d : Suspending %d times\n", this->id, this->count);24 sout | "Thread" | this->id | " : Suspending" | this->count | "times" | endl; 25 yield(); 38 26 39 27 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(); 28 sout | "Thread" | this->id | " : Suspend No." | i + 1 | endl; 29 yield(); 43 30 } 44 31 } 45 32 46 thread_h* get_thread(MyThread* this) { 47 return &this->t; 48 } 33 int main(int argc, char* argv[]) { 49 34 50 coroutine* get_coroutine(MyThread* this) { 51 return &this->t.c; 52 } 35 unsigned itterations = 10u; 36 if(argc == 2) { 37 int val = ato(argv[1]); 38 assert(val >= 0); 39 itterations = val; 40 } 53 41 54 int main() { 42 sout | "User main begin" | endl; 55 43 56 thread(MyThread) thread1; 57 thread(MyThread) thread2; 44 { 45 processor p; 46 { 47 scoped(MyThread) thread1 = { 1u, itterations }; 48 scoped(MyThread) thread2 = { 2u, itterations }; 49 } 50 } 58 51 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"); 52 sout | "User main end" | endl; 66 53 67 54 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.