[8118303] | 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 |
---|
[c84e80a] | 21 | #include <stddef.h> |
---|
[8118303] | 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 | |
---|
[c84e80a] | 31 | processor systemProcessorStorage = {}; |
---|
| 32 | processor * systemProcessor = &systemProcessorStorage; |
---|
[8118303] | 33 | |
---|
[c84e80a] | 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); |
---|
[8118303] | 85 | |
---|
[c84e80a] | 86 | void main(proc_coroutine * cor) { |
---|
| 87 | processor * this; |
---|
| 88 | this = cor->proc; |
---|
[8118303] | 89 | |
---|
[c84e80a] | 90 | thread_h * readyThread = NULL; |
---|
| 91 | for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) { |
---|
| 92 | |
---|
| 93 | readyThread = nextThread(this); |
---|
[8118303] | 94 | |
---|
[c84e80a] | 95 | if(readyThread) { |
---|
| 96 | runThread(this, readyThread); |
---|
| 97 | spin_count = 0; |
---|
| 98 | } else { |
---|
| 99 | spin(this, &spin_count); |
---|
| 100 | } |
---|
| 101 | } |
---|
[8118303] | 102 | |
---|
[c84e80a] | 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 | } |
---|
| 167 | |
---|
| 168 | void kernel_run( void ) { |
---|
| 169 | CtxInvokeProcessor(systemProcessor); |
---|
[8118303] | 170 | } |
---|
| 171 | |
---|
| 172 | // Local Variables: // |
---|
| 173 | // mode: c // |
---|
| 174 | // tab-width: 4 // |
---|
| 175 | // End: // |
---|