Changeset 75f3522 for src/libcfa
- Timestamp:
- Feb 13, 2017, 2:39:26 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:
- db6f06a
- Parents:
- fb7dca0
- Location:
- src/libcfa/concurrency
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/kernel
rfb7dca0 r75f3522 9 9 // 10 10 // Author : Thierry Delisle 11 // Created On : Tue Jan 17 12:27:26 201 611 // Created On : Tue Jan 17 12:27:26 2017 12 12 // Last Modified By : Thierry Delisle 13 13 // Last Modified On : -- … … 58 58 void ^?{}(processor * this); 59 59 60 61 60 //----------------------------------------------------------------------------- 62 61 // Locks -
src/libcfa/concurrency/kernel.c
rfb7dca0 r75f3522 9 9 // 10 10 // Author : Thierry Delisle 11 // Created On : Tue Jan 17 12:27:26 201 611 // Created On : Tue Jan 17 12:27:26 2017 12 12 // Last Modified By : Thierry Delisle 13 13 // Last Modified On : -- … … 20 20 21 21 //Header 22 #include "kernel "22 #include "kernel_private.h" 23 23 24 24 //C Includes … … 31 31 //CFA Includes 32 32 #include "libhdr.h" 33 #include "threads"34 33 35 34 //Private includes … … 51 50 //----------------------------------------------------------------------------- 52 51 // Kernel storage 53 struct processorCtx_t {54 processor * proc;55 coroutine c;56 };57 58 DECL_COROUTINE(processorCtx_t);59 60 52 #define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)] 61 53 … … 194 186 } 195 187 196 //----------------------------------------------------------------------------- 197 // Processor running routines 198 void main(processorCtx_t *); 199 thread * nextThread(cluster * this); 200 void scheduleInternal(processor * this, thread * dst); 201 void spin(processor * this, unsigned int * spin_count); 202 void thread_schedule( thread * thrd ); 203 188 //============================================================================================= 189 // Kernel Scheduling logic 190 //============================================================================================= 204 191 //Main of the processor contexts 205 192 void main(processorCtx_t * runner) { … … 212 199 213 200 thread * readyThread = NULL; 214 for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) {215 201 for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) 202 { 216 203 readyThread = nextThread( this->cltr ); 217 204 218 if(readyThread) { 219 scheduleInternal(this, readyThread); 205 if(readyThread) 206 { 207 runThread(this, readyThread); 208 209 //Some actions need to be taken from the kernel 210 finishRunning(this, readyThread); 211 220 212 spin_count = 0; 221 } else { 213 } 214 else 215 { 222 216 spin(this, &spin_count); 223 217 } … … 229 223 } 230 224 231 //Declarations for scheduleInternal 232 extern void ThreadCtxSwitch(coroutine * src, coroutine * dst); 233 234 // scheduleInternal runs a thread by context switching 225 // runThread runs a thread by context switching 235 226 // from the processor coroutine to the target thread 236 void scheduleInternal(processor * this, thread * dst) { 227 void runThread(processor * this, thread * dst) { 228 coroutine * proc_cor = get_coroutine(this->runner); 229 coroutine * thrd_cor = get_coroutine(dst); 230 231 //Reset the terminating actions here 237 232 this->thread_action = NoAction; 238 233 239 // coroutine * proc_ctx = get_coroutine(this->ctx); 240 // coroutine * thrd_ctx = get_coroutine(dst); 241 242 // //Update global state 243 // this->current_thread = dst; 244 245 // // Context Switch to the thread 246 // ThreadCtxSwitch(proc_ctx, thrd_ctx); 247 // // when ThreadCtxSwitch returns we are back in the processor coroutine 248 249 coroutine * proc_ctx = get_coroutine(this->runner); 250 coroutine * thrd_ctx = get_coroutine(dst); 251 thrd_ctx->last = proc_ctx; 252 253 // context switch to specified coroutine 254 // Which is now the current_coroutine 255 // LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, this->current_coroutine); 256 this->current_thread = dst; 257 this->current_coroutine = thrd_ctx; 258 CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context ); 259 this->current_coroutine = proc_ctx; 260 // LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, this->current_coroutine); 261 262 // when CtxSwitch returns we are back in the processor coroutine 234 //Update global state 235 this->current_thread = dst; 236 237 // Context Switch to the thread 238 ThreadCtxSwitch(proc_cor, thrd_cor); 239 // when ThreadCtxSwitch returns we are back in the processor coroutine 240 } 241 242 // Once a thread has finished running, some of 243 // its final actions must be executed from the kernel 244 void finishRunning(processor * this, thread * thrd) { 263 245 if(this->thread_action == Reschedule) { 264 thread_schedule( dst);246 ScheduleThread( thrd ); 265 247 } 266 248 } … … 325 307 //----------------------------------------------------------------------------- 326 308 // Scheduler routines 327 void thread_schedule( thread * thrd ) {309 void ScheduleThread( thread * thrd ) { 328 310 assertf( thrd->next == NULL, "Expected null got %p", thrd->next ); 329 311 … … 331 313 append( &systemProcessor->cltr->ready_queue, thrd ); 332 314 spin_unlock( &lock ); 315 } 316 317 void ScheduleInternal() { 318 get_this_processor()->thread_action = Reschedule; 319 suspend(); 333 320 } 334 321 … … 363 350 // Add the main thread to the ready queue 364 351 // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread 365 thread_schedule(mainThread);352 ScheduleThread(mainThread); 366 353 367 354 //initialize the global state variables … … 426 413 thread * it; 427 414 while( it = pop_head( &this->blocked) ) { 428 thread_schedule( it );415 ScheduleThread( it ); 429 416 } 430 417 } -
src/libcfa/concurrency/threads.c
rfb7dca0 r75f3522 17 17 #include "threads" 18 18 19 #include "kernel "19 #include "kernel_private.h" 20 20 #include "libhdr.h" 21 21 … … 72 72 //----------------------------------------------------------------------------- 73 73 // Starting and stopping threads 74 extern "C" {75 forall(dtype T | is_thread(T))76 void CtxInvokeThread(T * this);77 }78 79 extern void thread_schedule( thread * );80 81 74 forall( dtype T | is_thread(T) ) 82 75 void start( T* this ) { … … 92 85 CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); 93 86 94 fenv_t envp; 95 fegetenv( &envp ); 96 LIB_DEBUG_PRINTF("Thread : mxcsr %x\n", envp.__mxcsr); 97 LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h); 98 99 thread_schedule(thrd_h); 87 ScheduleThread(thrd_h); 100 88 } 101 89 … … 109 97 110 98 void yield( void ) { 111 get_this_processor()->thread_action = Reschedule; 112 suspend(); 99 ScheduleInternal(); 113 100 } 114 101 115 102 void ThreadCtxSwitch(coroutine* src, coroutine* dst) { 103 // set state of current coroutine to inactive 104 src->state = Inactive; 105 dst->state = Active; 106 107 //update the last resumer 116 108 dst->last = src; 117 109 118 // set state of current coroutine to inactive 119 src->state = Inactive; 120 121 // set new coroutine that task is executing 110 // set new coroutine that the processor is executing 111 // and context switch to it 122 112 get_this_processor()->current_coroutine = dst; 123 124 // context switch to specified coroutine125 113 CtxSwitch( src->stack.context, dst->stack.context ); 126 // when CtxSwitch returns we are back in the src coroutine114 get_this_processor()->current_coroutine = src; 127 115 128 116 // set state of new coroutine to active 117 dst->state = Inactive; 129 118 src->state = Active; 130 119 }
Note: See TracChangeset
for help on using the changeset viewer.