Changeset c84e80a for src/libcfa/concurrency
- Timestamp:
- Jan 18, 2017, 6:12:13 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:
- 68e6031
- Parents:
- 7fbe450
- Location:
- src/libcfa/concurrency
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/coroutines
r7fbe450 rc84e80a 29 29 coroutine * get_coroutine(T * this); 30 30 }; 31 32 #define DECL_COROUTINE(X) static inline coroutine* get_coroutine(X* this) { return &this->c; } void main(X* this); 31 33 32 34 //----------------------------------------------------------------------------- -
src/libcfa/concurrency/invoke.c
r7fbe450 rc84e80a 14 14 15 15 extern void __suspend_no_inline__F___1(void); 16 extern void __scheduler_remove__F_P9sthread_h__1(struct thread_h*); 16 17 17 18 void CtxInvokeCoroutine( … … 20 21 void *this 21 22 ) { 22 LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);23 // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine); 23 24 24 25 struct coroutine* cor = get_coroutine( this ); … … 42 43 void *this 43 44 ) { 44 LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread);45 // LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread); 45 46 46 47 __suspend_no_inline__F___1(); 47 48 48 struct coroutine* cor = &get_thread( this )->c; 49 struct thread_h* thrd = get_thread( this ); 50 struct coroutine* cor = &thrd->c; 49 51 cor->state = Active; 50 52 51 LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);53 // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this); 52 54 main( this ); 55 56 __scheduler_remove__F_P9sthread_h__1(thrd); 53 57 54 58 //Final suspend, should never return … … 64 68 void (*invoke)(void *) 65 69 ) { 66 LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);70 // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart); 67 71 68 72 struct coStack_t* stack = &get_coroutine( this )->stack; -
src/libcfa/concurrency/kernel
r7fbe450 rc84e80a 18 18 #define KERNEL_H 19 19 20 extern struct thread_h * the_thread; 20 #include <stdbool.h> 21 21 22 struct processor { 23 struct proc_coroutine * cor; 24 unsigned int thread_index; 25 unsigned int thread_count; 26 struct thread_h * threads[10]; 27 bool terminated; 28 }; 29 30 void ?{}(processor * this); 31 void ^?{}(processor * this); 32 33 void scheduler_add( struct thread_h * thrd ); 34 void scheduler_remove( struct thread_h * thrd ); 22 35 void kernel_run( void ); 23 36 -
src/libcfa/concurrency/kernel.c
r7fbe450 rc84e80a 19 19 20 20 //C Includes 21 #include <std bool.h>21 #include <stddef.h> 22 22 23 23 //CFA Includes … … 29 29 #include "invoke.h" 30 30 31 thread_h * the_thread = 0; 31 processor systemProcessorStorage = {}; 32 processor * systemProcessor = &systemProcessorStorage; 33 34 void ?{}(processor * this) { 35 this->cor = NULL; 36 this->thread_index = 0; 37 this->thread_count = 10; 38 this->terminated = false; 39 40 for(int i = 0; i < 10; i++) { 41 this->threads[i] = NULL; 42 } 43 44 LIB_DEBUG_PRINTF("Processor : ctor for core %p (core spots %d)\n", this, this->thread_count); 45 } 46 47 void ^?{}(processor * this) { 48 49 } 50 51 //----------------------------------------------------------------------------- 52 // Processor coroutine 53 struct proc_coroutine { 54 processor * proc; 55 coroutine c; 56 }; 57 58 void ?{}(coroutine * this, processor * proc) { 59 this{}; 60 } 61 62 DECL_COROUTINE(proc_coroutine) 63 64 void ?{}(proc_coroutine * this, processor * proc) { 65 (&this->c){proc}; 66 this->proc = proc; 67 proc->cor = this; 68 } 69 70 void ^?{}(proc_coroutine * this) { 71 ^(&this->c){}; 72 } 73 74 void CtxInvokeProcessor(processor * proc) { 75 proc_coroutine proc_cor_storage = {proc}; 76 resume( &proc_cor_storage ); 77 } 78 79 //----------------------------------------------------------------------------- 80 // Processor running routines 81 void main(proc_coroutine * cor); 82 thread_h * nextThread(processor * this); 83 void runThread(processor * this, thread_h * dst); 84 void spin(processor * this, unsigned int * spin_count); 85 86 void main(proc_coroutine * cor) { 87 processor * this; 88 this = cor->proc; 89 90 thread_h * readyThread = NULL; 91 for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) { 92 93 readyThread = nextThread(this); 94 95 if(readyThread) { 96 runThread(this, readyThread); 97 spin_count = 0; 98 } else { 99 spin(this, &spin_count); 100 } 101 } 102 103 LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this); 104 } 105 106 thread_h * nextThread(processor * this) { 107 for(int i = 0; i < this->thread_count; i++) { 108 this->thread_index = (this->thread_index + 1) % this->thread_count; 109 110 thread_h * thrd = this->threads[this->thread_index]; 111 if(thrd) return thrd; 112 } 113 114 return NULL; 115 } 116 117 void runThread(processor * this, thread_h * dst) { 118 coroutine * proc_ctx = get_coroutine(this->cor); 119 coroutine * thrd_ctx = get_coroutine(dst); 120 thrd_ctx->last = proc_ctx; 121 122 // context switch to specified coroutine 123 // Which is now the current_coroutine 124 LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine); 125 current_coroutine = thrd_ctx; 126 CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context ); 127 current_coroutine = proc_ctx; 128 LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine); 129 130 // when CtxSwitch returns we are back in the processor coroutine 131 } 132 133 void spin(processor * this, unsigned int * spin_count) { 134 (*spin_count)++; 135 } 136 137 //----------------------------------------------------------------------------- 138 // Kernel runner (Temporary) 139 140 void scheduler_add( struct thread_h * thrd ) { 141 LIB_DEBUG_PRINTF("Kernel : scheduling %p on core %p (%d spots)\n", thrd, systemProcessor, systemProcessor->thread_count); 142 for(int i = 0; i < systemProcessor->thread_count; i++) { 143 if(systemProcessor->threads[i] == NULL) { 144 systemProcessor->threads[i] = thrd; 145 return; 146 } 147 } 148 assert(false); 149 } 150 151 void scheduler_remove( struct thread_h * thrd ) { 152 LIB_DEBUG_PRINTF("Kernel : unscheduling %p from core %p\n", thrd, systemProcessor); 153 for(int i = 0; i < systemProcessor->thread_count; i++) { 154 if(systemProcessor->threads[i] == thrd) { 155 systemProcessor->threads[i] = NULL; 156 break; 157 } 158 } 159 for(int i = 0; i < systemProcessor->thread_count; i++) { 160 if(systemProcessor->threads[i] != NULL) { 161 return; 162 } 163 } 164 LIB_DEBUG_PRINTF("Kernel : terminating core %p\n\n\n", systemProcessor); 165 systemProcessor->terminated = true; 166 } 32 167 33 168 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 ); 169 CtxInvokeProcessor(systemProcessor); 52 170 } 53 171 -
src/libcfa/concurrency/threads
r7fbe450 rc84e80a 39 39 } 40 40 41 static inline coroutine* get_coroutine(thread_h* this) { 42 return &this->c; 43 } 44 41 45 //----------------------------------------------------------------------------- 42 46 // Ctors and dtors -
src/libcfa/concurrency/threads.c
r7fbe450 rc84e80a 78 78 current_coroutine = thrd_c; 79 79 80 LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);80 // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h); 81 81 82 82 create_stack(&thrd_c->stack, thrd_c->stack.size); … … 84 84 CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); 85 85 86 the_thread = thrd_h;86 scheduler_add(thrd_h); 87 87 } 88 88
Note: See TracChangeset
for help on using the changeset viewer.