Changeset eb2e723 for src/libcfa
- Timestamp:
- Jan 19, 2017, 2:42:49 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:
- 35dd180f
- Parents:
- 2175062
- Location:
- src/libcfa/concurrency
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/libcfa/concurrency/CtxSwitch-x86_64.S ¶
r2175062 reb2e723 84 84 jmp *%r12 85 85 86 .text 87 .align 2 88 .globl CtxGet 89 CtxGet: 90 movq %rsp,SP_OFFSET(%rdi) 91 movq %rbp,FP_OFFSET(%rdi) 92 93 ret 94 86 95 // Local Variables: // 87 96 // mode: c // -
TabularUnified src/libcfa/concurrency/coroutines.c ¶
r2175062 reb2e723 36 36 static size_t pageSize = 0; // architecture pagesize HACK, should go in proper runtime singleton 37 37 38 //Extra private desctructor for the main39 //FIXME the main should not actually allocate a stack40 //Since the main is never resumed the extra stack does not cause41 //any problem but it is wasted memory42 void ?{}(coStack_t* this, size_t size);43 void ?{}(coroutine* this, size_t size);44 45 //Main coroutine46 //FIXME do not construct a stack for the main47 coroutine main_coroutine = { 1000 };48 49 38 //Current coroutine 50 39 //Will need to be in TLS when multi-threading is added 51 coroutine* current_coroutine = &main_coroutine;40 coroutine* current_coroutine; 52 41 53 42 //----------------------------------------------------------------------------- -
TabularUnified src/libcfa/concurrency/invoke.h ¶
r2175062 reb2e723 52 52 // assembler routines that performs the context switch 53 53 extern void CtxInvokeStub( void ); 54 void CtxSwitch( void *from, void *to ) asm ("CtxSwitch"); 54 void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); 55 void CtxGet( void * this ) asm ("CtxGet"); 55 56 56 57 #endif //_INVOKE_PRIVATE_H_ -
TabularUnified src/libcfa/concurrency/kernel ¶
r2175062 reb2e723 21 21 22 22 struct processor { 23 struct proc _coroutine * cor;23 struct processorCtx_t * ctx; 24 24 unsigned int thread_index; 25 25 unsigned int thread_count; -
TabularUnified src/libcfa/concurrency/kernel.c ¶
r2175062 reb2e723 20 20 //C Includes 21 21 #include <stddef.h> 22 extern "C" { 23 #include <sys/resource.h> 24 } 22 25 23 26 //CFA Includes … … 29 32 #include "invoke.h" 30 33 31 processor systemProcessorStorage = {}; 32 processor * systemProcessor = &systemProcessorStorage; 34 processor * systemProcessor; 35 thread_h * mainThread; 36 37 void kernel_startup(void) __attribute__((constructor(101))); 38 void kernel_shutdown(void) __attribute__((destructor(101))); 33 39 34 40 void ?{}(processor * this) { 35 this->c or= NULL;41 this->ctx = NULL; 36 42 this->thread_index = 0; 37 43 this->thread_count = 10; … … 51 57 //----------------------------------------------------------------------------- 52 58 // Processor coroutine 53 struct proc _coroutine{59 struct processorCtx_t { 54 60 processor * proc; 55 61 coroutine c; 56 62 }; 57 63 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}; 64 DECL_COROUTINE(processorCtx_t) 65 66 void ?{}(processorCtx_t * this, processor * proc) { 67 (&this->c){}; 66 68 this->proc = proc; 67 proc->cor = this;68 }69 70 void ^?{}(proc_coroutine * this) {71 ^(&this->c){};72 69 } 73 70 74 71 void CtxInvokeProcessor(processor * proc) { 75 proc _coroutineproc_cor_storage = {proc};72 processorCtx_t proc_cor_storage = {proc}; 76 73 resume( &proc_cor_storage ); 77 74 } … … 79 76 //----------------------------------------------------------------------------- 80 77 // Processor running routines 81 void main(proc _coroutine * cor);78 void main(processorCtx_t * ctx); 82 79 thread_h * nextThread(processor * this); 83 80 void runThread(processor * this, thread_h * dst); 84 81 void spin(processor * this, unsigned int * spin_count); 85 82 86 void main(proc _coroutine * cor) {87 processor * this ;88 this = cor->proc;83 void main(processorCtx_t * ctx) { 84 processor * this = ctx->proc; 85 LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this); 89 86 90 87 thread_h * readyThread = NULL; … … 116 113 117 114 void runThread(processor * this, thread_h * dst) { 118 coroutine * proc_ctx = get_coroutine(this->c or);115 coroutine * proc_ctx = get_coroutine(this->ctx); 119 116 coroutine * thrd_ctx = get_coroutine(dst); 120 117 thrd_ctx->last = proc_ctx; … … 138 135 // Kernel runner (Temporary) 139 136 140 void scheduler_add( structthread_h * thrd ) {137 void scheduler_add( thread_h * thrd ) { 141 138 LIB_DEBUG_PRINTF("Kernel : scheduling %p on core %p (%d spots)\n", thrd, systemProcessor, systemProcessor->thread_count); 142 139 for(int i = 0; i < systemProcessor->thread_count; i++) { … … 149 146 } 150 147 151 void scheduler_remove( structthread_h * thrd ) {148 void scheduler_remove( thread_h * thrd ) { 152 149 LIB_DEBUG_PRINTF("Kernel : unscheduling %p from core %p\n", thrd, systemProcessor); 153 150 for(int i = 0; i < systemProcessor->thread_count; i++) { … … 162 159 } 163 160 } 164 LIB_DEBUG_PRINTF("Kernel : terminating core %p\n \n\n", systemProcessor);161 LIB_DEBUG_PRINTF("Kernel : terminating core %p\n", systemProcessor); 165 162 systemProcessor->terminated = true; 166 163 } 167 164 168 void kernel_run( void ) { 169 CtxInvokeProcessor(systemProcessor); 165 //----------------------------------------------------------------------------- 166 // Kernel storage 167 #define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)] 168 169 KERNEL_STORAGE(processorCtx_t, systemProcessorCtx); 170 KERNEL_STORAGE(processor, systemProcessor); 171 KERNEL_STORAGE(thread_h, mainThread); 172 KERNEL_STORAGE(machine_context_t, mainThread_context); 173 174 //----------------------------------------------------------------------------- 175 // Main thread construction 176 struct mainThread_info_t { 177 machine_context_t ctx; 178 unsigned int size; // size of stack 179 void *base; // base of stack 180 void *storage; // pointer to stack 181 void *limit; // stack grows towards stack limit 182 void *context; // address of cfa_context_t 183 void *top; // address of top of storage 184 }; 185 186 void ?{}( mainThread_info_t * this ) { 187 CtxGet( &this->ctx ); 188 this->base = this->ctx.FP; 189 this->storage = this->ctx.SP; 190 191 rlimit r; 192 int ret = getrlimit( RLIMIT_STACK, &r); 193 this->size = r.rlim_cur; 194 195 this->limit = (void *)(((intptr_t)this->base) - this->size); 196 this->context = &mainThread_context_storage; 197 this->top = this->base; 198 } 199 200 void ?{}( coStack_t * this, mainThread_info_t * info) { 201 this->size = info->size; 202 this->storage = info->storage; 203 this->limit = info->limit; 204 this->base = info->base; 205 this->context = info->context; 206 this->top = info->top; 207 this->userStack = true; 208 } 209 210 void ?{}( coroutine * this, mainThread_info_t * info) { 211 (&this->stack){ info }; 212 this->name = "Main Thread"; 213 this->errno_ = 0; 214 this->state = Inactive; 215 this->notHalted = true; 216 } 217 218 void ?{}( thread_h * this, mainThread_info_t * info) { 219 (&this->c){ info }; 220 } 221 222 //----------------------------------------------------------------------------- 223 // Kernel boot procedures 224 void kernel_startup(void) { 225 226 // SKULLDUGGERY: the mainThread steals the process main thread 227 // which will then be scheduled by the systemProcessor normally 228 LIB_DEBUG_PRINTF("Kernel : Starting\n"); 229 230 mainThread_info_t ctx; 231 LIB_DEBUG_PRINTF("Kernel : base : %p\n", ctx.base ); 232 LIB_DEBUG_PRINTF("Kernel : top : %p\n", ctx.top ); 233 LIB_DEBUG_PRINTF("Kernel : limit : %p\n", ctx.limit ); 234 LIB_DEBUG_PRINTF("Kernel : size : %x\n", ctx.size ); 235 LIB_DEBUG_PRINTF("Kernel : storage : %p\n", ctx.storage ); 236 LIB_DEBUG_PRINTF("Kernel : context : %p\n", ctx.context ); 237 238 // Start by initializing the main thread 239 mainThread = (thread_h *)&mainThread_storage; 240 LIB_DEBUG_PRINTF("Kernel : Main thread : %p\n", mainThread ); 241 mainThread{ &ctx }; 242 243 // // Initialize the system processor 244 systemProcessor = (processor *)&systemProcessor_storage; 245 systemProcessor{}; 246 247 // Initialize the system processor ctx 248 // (the coroutine that contains the processing control flow) 249 systemProcessor->ctx = (processorCtx_t *)&systemProcessorCtx_storage; 250 systemProcessor->ctx{ systemProcessor }; 251 252 scheduler_add(mainThread); 253 254 current_coroutine = &mainThread->c; 255 256 LIB_DEBUG_PRINTF("Kernel : Starting system processor\n"); 257 resume(systemProcessor->ctx); 258 259 LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n"); 260 } 261 void kernel_shutdown(void) { 262 LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down"); 263 264 LIB_DEBUG_PRINTF("Unscheduling main thread\n"); 265 scheduler_remove(mainThread); 266 267 LIB_DEBUG_PRINTF("Suspending main\n"); 268 suspend(); 269 270 LIB_DEBUG_PRINTF("Kernel : Control return to initial process thread\n"); 271 272 ^(systemProcessor->ctx){}; 273 ^(systemProcessor){}; 274 275 ^(mainThread){}; 276 277 LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n"); 170 278 } 171 279
Note: See TracChangeset
for help on using the changeset viewer.