- Timestamp:
- Jan 17, 2017, 5:13:47 PM (8 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:
- c49bf54
- Parents:
- 7350ff97
- Location:
- src/libcfa/concurrency
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/coroutines
r7350ff97 r8118303 1 // - *- Mode: CFA -*-1 // - *- Mode: CFA - *- 2 2 // 3 3 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo … … 18 18 #define COROUTINES_H 19 19 20 #include "assert" //20 #include "assert" 21 21 #include "invoke.h" 22 22 … … 26 26 // Anything that is resumed is a coroutine. 27 27 trait is_coroutine(dtype T) { 28 void co_main(T * this);29 coroutine * get_coroutine(T* this);28 void co_main(T * this); 29 coroutine * get_coroutine(T * this); 30 30 }; 31 31 32 32 //----------------------------------------------------------------------------- 33 33 // Ctors and dtors 34 void ?{}(coStack_t * this);35 void ?{}(coroutine * this);36 void ^?{}(coStack_t * this);37 void ^?{}(coroutine * this);34 void ?{}(coStack_t * this); 35 void ?{}(coroutine * this); 36 void ^?{}(coStack_t * this); 37 void ^?{}(coroutine * this); 38 38 39 39 //----------------------------------------------------------------------------- … … 42 42 43 43 forall(dtype T | is_coroutine(T)) 44 static inline void resume(T * cor);44 static inline void resume(T * cor); 45 45 46 46 forall(dtype T | is_coroutine(T)) 47 void prime(T * cor);47 void prime(T * cor); 48 48 49 49 //----------------------------------------------------------------------------- … … 53 53 extern "C" { 54 54 forall(dtype T | is_coroutine(T)) 55 void CtxInvokeCoroutine(T * this);55 void CtxInvokeCoroutine(T * this); 56 56 57 57 forall(dtype T | is_coroutine(T)) 58 void CtxStart(T * this, void (*invoke)(T*));58 void CtxStart(T * this, void ( *invoke)(T *)); 59 59 } 60 60 61 61 // Get current coroutine 62 extern coroutine * current_coroutine; //PRIVATE, never use directly63 static inline coroutine * this_coroutine(void) {62 extern coroutine * current_coroutine; //PRIVATE, never use directly 63 static inline coroutine * this_coroutine(void) { 64 64 return current_coroutine; 65 65 } 66 66 67 67 // Private wrappers for context switch and stack creation 68 extern void corCxtSw(coroutine * src, coroutine* dst);69 extern void create_stack( coStack_t * this, unsigned int storageSize );68 extern void corCxtSw(coroutine * src, coroutine * dst); 69 extern void create_stack( coStack_t * this, unsigned int storageSize ); 70 70 71 71 // Suspend implementation inlined for performance 72 72 static inline void suspend() { 73 coroutine * src = this_coroutine(); // optimization73 coroutine * src = this_coroutine(); // optimization 74 74 75 75 assertf( src->last != 0, … … 87 87 // Resume implementation inlined for performance 88 88 forall(dtype T | is_coroutine(T)) 89 static inline void resume(T * cor) {90 coroutine * src = this_coroutine(); // optimization91 coroutine * dst = get_coroutine(cor);89 static inline void resume(T * cor) { 90 coroutine * src = this_coroutine(); // optimization 91 coroutine * dst = get_coroutine(cor); 92 92 93 93 if( unlikely(!dst->stack.base) ) { -
src/libcfa/concurrency/coroutines.c
r7350ff97 r8118303 111 111 // is not inline (We can't inline Cforall in C) 112 112 void suspend_no_inline(void) { 113 LIB_DEBUG_PRINTF("Suspending back : to %p from %p\n", this_coroutine(), this_coroutine() ? this_coroutine()->last : (void*)-1); 114 113 115 suspend(); 114 116 } -
src/libcfa/concurrency/invoke.c
r7350ff97 r8118303 20 20 void *this 21 21 ) { 22 LIB_DEBUG_PRINTF("Invoke : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);22 LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine); 23 23 24 24 struct coroutine* cor = get_coroutine( this ); … … 31 31 32 32 main( this ); 33 34 //Final suspend, should never return 35 __suspend_no_inline__F___1(); 36 assertf(false, "Resumed dead coroutine"); 37 } 38 39 void CtxInvokeThread( 40 void (*main)(void *), 41 struct thread_h *(*get_thread)(void *), 42 void *this 43 ) { 44 LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread); 45 46 __suspend_no_inline__F___1(); 47 48 struct coroutine* cor = &get_thread( this )->c; 49 cor->state = Active; 50 51 LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this); 52 main( this ); 53 54 //Final suspend, should never return 55 __suspend_no_inline__F___1(); 56 assertf(false, "Resumed dead thread"); 33 57 } 34 58 … … 40 64 void (*invoke)(void *) 41 65 ) { 42 LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p , get_c %p) to %p\n", this, main, get_coroutine, invoke);66 LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart); 43 67 44 68 struct coStack_t* stack = &get_coroutine( this )->stack; -
src/libcfa/concurrency/invoke.h
r7350ff97 r8118303 35 35 }; 36 36 37 struct thread_h { 38 struct coroutine c; 39 }; 40 37 41 #endif //_INVOKE_H_ 38 42 #else //! defined(__CFA_INVOKE_PRIVATE__) -
src/libcfa/concurrency/kernel
r7350ff97 r8118303 1 // -*- Mode: CFA -*- 2 // 3 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 4 // 5 // The contents of this file are covered under the licence agreement in the 6 // file "LICENCE" distributed with Cforall. 7 // 8 // threads -- 9 // 10 // Author : Thierry Delisle 11 // Created On : Tue Jan 17 12:27:26 2016 12 // Last Modified By : Thierry Delisle 13 // Last Modified On : -- 14 // Update Count : 0 15 // 16 17 #ifndef KERNEL_H 18 #define KERNEL_H 19 20 extern struct thread_h * the_thread; 21 22 void kernel_run( void ); 23 24 #endif //KERNEL_H 25 26 // Local Variables: // 27 // mode: c // 28 // tab-width: 4 // 29 // End: // -
src/libcfa/concurrency/kernel.c
r7350ff97 r8118303 1 // -*- Mode: CFA -*- 2 // 3 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 4 // 5 // The contents of this file are covered under the licence agreement in the 6 // file "LICENCE" distributed with Cforall. 7 // 8 // kernel.c -- 9 // 10 // Author : Thierry Delisle 11 // Created On : Tue Jan 17 12:27:26 2016 12 // Last Modified By : Thierry Delisle 13 // Last Modified On : -- 14 // Update Count : 0 15 // 16 17 //Header 18 #include "kernel" 19 20 //C Includes 21 #include <stdbool.h> 22 23 //CFA Includes 24 #include "libhdr.h" 25 #include "threads" 26 27 //Private includes 28 #define __CFA_INVOKE_PRIVATE__ 29 #include "invoke.h" 30 31 thread_h * the_thread = 0; 32 33 void kernel_run( void ) { 34 35 bool done = true; 36 coroutine* processor_cor = this_coroutine(); 37 LIB_DEBUG_PRINTF("Kernel : processor cor is %p\n", processor_cor); 38 39 do { 40 thread_h * dst = the_thread; 41 42 LIB_DEBUG_PRINTF("Kernel : picked thread %p\n", dst); 43 44 // set new coroutine that task is executing 45 current_coroutine = &dst->c; 46 47 // context switch to specified coroutine 48 LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p)\n", current_coroutine, processor_cor); 49 CtxSwitch( processor_cor->stack.context, current_coroutine->stack.context ); 50 // when CtxSwitch returns we are back in the processor coroutine 51 } while( ! done ); 52 } 53 54 // Local Variables: // 55 // mode: c // 56 // tab-width: 4 // 57 // End: // -
src/libcfa/concurrency/threads
r7350ff97 r8118303 18 18 #define THREADS_H 19 19 20 #include "assert" 21 #include "invoke.h" 20 22 23 #include "coroutines" 24 25 //----------------------------------------------------------------------------- 26 // Coroutine trait 27 // Anything that implements this trait can be resumed. 28 // Anything that is resumed is a coroutine. 29 trait is_thread(dtype T /*| sized(T)*/) { 30 void co_main(T* this); 31 thread_h* get_thread(T* this); 32 /*void ?{}(T*); 33 void ^?{}(T*);*/ 34 }; 35 36 forall(otype T | is_thread(T) ) 37 static inline coroutine* get_coroutine(T* this) { 38 return &get_thread(this)->c; 39 } 40 41 //----------------------------------------------------------------------------- 42 // Ctors and dtors 43 void ?{}(thread_h* this); 44 void ^?{}(thread_h* this); 45 46 //----------------------------------------------------------------------------- 47 // thread runner 48 // Structure that actually start and stop threads 49 forall(otype T | is_thread(T) ) 50 struct thread { 51 T handle; 52 }; 53 54 forall(otype T | is_thread(T) ) 55 void ?{}( thread(T)* this ); 56 57 forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } ) 58 void ?{}( thread(T)* this, P params ); 59 60 forall(otype T | is_thread(T) ) 61 void ^?{}( thread(T)* this ); 62 63 //----------------------------------------------------------------------------- 64 // PRIVATE exposed because of inline 21 65 22 66 #endif //THREADS_H -
src/libcfa/concurrency/threads.c
r7350ff97 r8118303 6 6 // file "LICENCE" distributed with Cforall. 7 7 // 8 // threads --8 // threads.c -- 9 9 // 10 10 // Author : Thierry Delisle … … 15 15 // 16 16 17 #include "threads" 17 18 19 #include "kernel" 20 #include "libhdr.h" 21 22 #define __CFA_INVOKE_PRIVATE__ 23 #include "invoke.h" 24 25 #include <stdlib> 26 27 //----------------------------------------------------------------------------- 28 // Forward declarations 29 forall(otype T | is_thread(T) ) 30 void start( thread(T)* this ); 31 32 forall(otype T | is_thread(T) ) 33 void stop( thread(T)* this ); 34 35 //----------------------------------------------------------------------------- 36 // Thread ctors and dtors 37 38 void ?{}(thread_h* this) { 39 (&this->c){}; 40 } 41 42 void ^?{}(thread_h* this) { 43 ^(&this->c){}; 44 } 45 46 forall(otype T | is_thread(T) ) 47 void ?{}( thread(T)* this ) { 48 printf("thread() ctor\n"); 49 (&this->handle){}; 50 start(this); 51 } 52 53 forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } ) 54 void ?{}( thread(T)* this, P params ) { 55 (&this->handle){ params }; 56 start(this); 57 } 58 59 forall(otype T | is_thread(T) ) 60 void ^?{}( thread(T)* this ) { 61 stop(this); 62 ^(&this->handle){}; 63 } 64 65 //----------------------------------------------------------------------------- 66 // Starting and stopping threads 67 extern "C" { 68 forall(dtype T | is_thread(T)) 69 void CtxInvokeThread(T * this); 70 } 71 72 forall(otype T | is_thread(T)) 73 void start( thread(T)* this ) { 74 T* handle = &this->handle; 75 coroutine* thrd_c = get_coroutine(handle); 76 thread_h* thrd_h = get_thread (handle); 77 thrd_c->last = this_coroutine(); 78 current_coroutine = thrd_c; 79 80 LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h); 81 82 create_stack(&thrd_c->stack, thrd_c->stack.size); 83 CtxStart(handle, CtxInvokeThread); 84 CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); 85 86 the_thread = thrd_h; 87 } 88 89 forall(otype T | is_thread(T) ) 90 void stop( thread(T)* this ) { 91 92 } 18 93 19 94 // Local Variables: //
Note: See TracChangeset
for help on using the changeset viewer.