Changeset 5ebb2fbc for src/libcfa/concurrency/threads
- Timestamp:
- Jan 18, 2017, 12:47:15 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:
- e9e4e9ee
- Parents:
- f3b0a07 (diff), c49bf54 (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/threads
rf3b0a07 r5ebb2fbc 9 9 // 10 10 // Author : Thierry Delisle 11 // Created On : Mon Nov 2812:27:26 201611 // Created On : Tue Jan 17 12:27:26 2016 12 12 // Last Modified By : Thierry Delisle 13 // Last Modified On : Mon Nov 28 12:27:26 201613 // Last Modified On : -- 14 14 // Update Count : 0 15 15 // … … 18 18 #define THREADS_H 19 19 20 #include "assert" //20 #include "assert" 21 21 #include "invoke.h" 22 23 #include "coroutines" 22 24 23 25 //----------------------------------------------------------------------------- … … 25 27 // Anything that implements this trait can be resumed. 26 28 // Anything that is resumed is a coroutine. 27 trait is_ coroutine(dtype T) {29 trait is_thread(dtype T /*| sized(T)*/) { 28 30 void co_main(T* this); 29 coroutine* get_coroutine(T* this); 31 thread_h* get_thread(T* this); 32 /*void ?{}(T*); 33 void ^?{}(T*);*/ 30 34 }; 35 36 forall(otype T | is_thread(T) ) 37 static inline coroutine* get_coroutine(T* this) { 38 return &get_thread(this)->c; 39 } 31 40 32 41 //----------------------------------------------------------------------------- 33 42 // Ctors and dtors 34 void ?{}(coStack_t* this); 35 void ?{}(coroutine* this); 36 void ^?{}(coStack_t* this); 37 void ^?{}(coroutine* this); 43 void ?{}(thread_h* this); 44 void ^?{}(thread_h* this); 38 45 39 46 //----------------------------------------------------------------------------- 40 // Public coroutine API 41 static inline void suspend(); 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 }; 42 53 43 forall( dtype T | is_coroutine(T))44 static inline void resume(T* cor);54 forall(otype T | is_thread(T) ) 55 void ?{}( thread(T)* this ); 45 56 46 forall(dtype T | is_coroutine(T)) 47 void prime(T* cor); 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 ); 48 62 49 63 //----------------------------------------------------------------------------- 50 64 // PRIVATE exposed because of inline 51 52 // Start coroutine routines53 extern "C" {54 forall(dtype T | is_coroutine(T))55 void CtxInvokeCoroutine(T* this);56 57 forall(dtype T | is_coroutine(T))58 void CtxStart(T* this, void (*invoke)(T*));59 }60 61 // Get current coroutine62 extern coroutine* current_coroutine; //PRIVATE, never use directly63 static inline coroutine* this_coroutine(void) {64 return current_coroutine;65 }66 67 // Private wrappers for context switch and stack creation68 extern void corCxtSw(coroutine* src, coroutine* dst);69 extern void create_stack( coStack_t* this, unsigned int storageSize );70 71 // Suspend implementation inlined for performance72 static inline void suspend() {73 coroutine* src = this_coroutine(); // optimization74 75 assertf( src->last != 0,76 "Attempt to suspend coroutine %.256s (%p) that has never been resumed.\n"77 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",78 src->name, src );79 assertf( src->last->notHalted,80 "Attempt by coroutine %.256s (%p) to suspend back to terminated coroutine %.256s (%p).\n"81 "Possible cause is terminated coroutine's main routine has already returned.",82 src->name, src, src->last->name, src->last );83 84 corCxtSw( src, src->last );85 }86 87 // Resume implementation inlined for performance88 forall(dtype T | is_coroutine(T))89 static inline void resume(T* cor) {90 coroutine* src = this_coroutine(); // optimization91 coroutine* dst = get_coroutine(cor);92 93 if( unlikely(!dst->stack.base) ) {94 create_stack(&dst->stack, dst->stack.size);95 CtxStart(cor, CtxInvokeCoroutine);96 }97 98 // not resuming self ?99 if ( src != dst ) {100 assertf( dst->notHalted ,101 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"102 "Possible cause is terminated coroutine's main routine has already returned.",103 src->name, src, dst->name, dst );104 105 // set last resumer106 dst->last = src;107 } // if108 109 // always done for performance testing110 corCxtSw( src, dst );111 }112 65 113 66 #endif //THREADS_H
Note:
See TracChangeset
for help on using the changeset viewer.