[8def349] | 1 | // -*- Mode: CFA -*- |
---|
[6a3d2e7] | 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 | // |
---|
[75a17f1] | 8 | // coroutine.c -- |
---|
[6a3d2e7] | 9 | // |
---|
| 10 | // Author : Thierry Delisle |
---|
| 11 | // Created On : Mon Nov 28 12:27:26 2016 |
---|
| 12 | // Last Modified By : Thierry Delisle |
---|
| 13 | // Last Modified On : Mon Nov 28 12:27:26 2016 |
---|
| 14 | // Update Count : 0 |
---|
| 15 | // |
---|
| 16 | |
---|
[75a17f1] | 17 | #include "coroutine" |
---|
[bd98b58] | 18 | |
---|
[6a3d2e7] | 19 | extern "C" { |
---|
| 20 | #include <stddef.h> |
---|
| 21 | #include <malloc.h> |
---|
| 22 | #include <errno.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include <unistd.h> |
---|
| 25 | #include <sys/mman.h> |
---|
| 26 | } |
---|
| 27 | |
---|
[bd98b58] | 28 | #include "kernel" |
---|
[6a3d2e7] | 29 | #include "libhdr.h" |
---|
| 30 | |
---|
| 31 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 32 | #include "invoke.h" |
---|
| 33 | |
---|
[89a3df5] | 34 | extern thread_local processor * this_processor; |
---|
[bd98b58] | 35 | |
---|
[6a3d2e7] | 36 | //----------------------------------------------------------------------------- |
---|
| 37 | // Global state variables |
---|
| 38 | |
---|
| 39 | // minimum feasible stack size in bytes |
---|
| 40 | #define MinStackSize 1000 |
---|
| 41 | static size_t pageSize = 0; // architecture pagesize HACK, should go in proper runtime singleton |
---|
| 42 | |
---|
| 43 | //----------------------------------------------------------------------------- |
---|
| 44 | // Coroutine ctors and dtors |
---|
| 45 | void ?{}(coStack_t* this) { |
---|
| 46 | this->size = 10240; // size of stack |
---|
| 47 | this->storage = NULL; // pointer to stack |
---|
| 48 | this->limit = NULL; // stack grows towards stack limit |
---|
| 49 | this->base = NULL; // base of stack |
---|
| 50 | this->context = NULL; // address of cfa_context_t |
---|
| 51 | this->top = NULL; // address of top of storage |
---|
| 52 | this->userStack = false; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void ?{}(coStack_t* this, size_t size) { |
---|
| 56 | this{}; |
---|
| 57 | this->size = size; |
---|
| 58 | |
---|
| 59 | create_stack(this, this->size); |
---|
| 60 | } |
---|
| 61 | |
---|
[c3acb841] | 62 | void ?{}(coroutine_desc* this) { |
---|
[db6f06a] | 63 | this{ "Anonymous Coroutine" }; |
---|
[6a3d2e7] | 64 | } |
---|
| 65 | |
---|
[c3acb841] | 66 | void ?{}(coroutine_desc* this, const char * name) { |
---|
[84e2523] | 67 | this->name = name; |
---|
| 68 | this->errno_ = 0; |
---|
| 69 | this->state = Start; |
---|
| 70 | this->starter = NULL; |
---|
| 71 | this->last = NULL; |
---|
| 72 | } |
---|
| 73 | |
---|
[c3acb841] | 74 | void ?{}(coroutine_desc* this, size_t size) { |
---|
[6a3d2e7] | 75 | this{}; |
---|
| 76 | (&this->stack){size}; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void ^?{}(coStack_t* this) { |
---|
| 80 | if ( ! this->userStack ) { |
---|
| 81 | LIB_DEBUG_DO( |
---|
| 82 | if ( mprotect( this->storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) { |
---|
| 83 | abortf( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", this, errno, strerror( errno ) ); |
---|
| 84 | } |
---|
| 85 | ); |
---|
| 86 | free( this->storage ); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
[c3acb841] | 90 | void ^?{}(coroutine_desc* this) {} |
---|
[6a3d2e7] | 91 | |
---|
| 92 | // Part of the Public API |
---|
| 93 | // Not inline since only ever called once per coroutine |
---|
| 94 | forall(dtype T | is_coroutine(T)) |
---|
| 95 | void prime(T* cor) { |
---|
[c3acb841] | 96 | coroutine_desc* this = get_coroutine(cor); |
---|
[6a3d2e7] | 97 | assert(this->state == Start); |
---|
| 98 | |
---|
| 99 | this->state = Primed; |
---|
| 100 | resume(cor); |
---|
| 101 | } |
---|
| 102 | |
---|
[0c92c9f] | 103 | // Wrapper for co |
---|
[c3acb841] | 104 | void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { |
---|
[6a3d2e7] | 105 | // THREAD_GETMEM( This )->disableInterrupts(); |
---|
| 106 | |
---|
| 107 | // set state of current coroutine to inactive |
---|
| 108 | src->state = Inactive; |
---|
| 109 | |
---|
| 110 | // set new coroutine that task is executing |
---|
[89a3df5] | 111 | this_processor->current_coroutine = dst; |
---|
[6a3d2e7] | 112 | |
---|
| 113 | // context switch to specified coroutine |
---|
| 114 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
| 115 | // when CtxSwitch returns we are back in the src coroutine |
---|
| 116 | |
---|
| 117 | // set state of new coroutine to active |
---|
| 118 | src->state = Active; |
---|
| 119 | |
---|
| 120 | // THREAD_GETMEM( This )->enableInterrupts(); |
---|
| 121 | } //ctxSwitchDirect |
---|
| 122 | |
---|
| 123 | void create_stack( coStack_t* this, unsigned int storageSize ) { |
---|
| 124 | //TEMP HACK do this on proper kernel startup |
---|
| 125 | if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE ); |
---|
| 126 | |
---|
| 127 | size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment |
---|
| 128 | |
---|
| 129 | if ( (intptr_t)this->storage == 0 ) { |
---|
| 130 | this->userStack = false; |
---|
| 131 | this->size = libCeiling( storageSize, 16 ); |
---|
| 132 | // use malloc/memalign because "new" raises an exception for out-of-memory |
---|
| 133 | |
---|
| 134 | // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment |
---|
| 135 | LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) ); |
---|
| 136 | LIB_NO_DEBUG_DO( this->storage = malloc( cxtSize + this->size + 8 ) ); |
---|
| 137 | |
---|
| 138 | LIB_DEBUG_DO( |
---|
| 139 | if ( mprotect( this->storage, pageSize, PROT_NONE ) == -1 ) { |
---|
| 140 | abortf( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) ); |
---|
| 141 | } // if |
---|
| 142 | ); |
---|
| 143 | |
---|
| 144 | if ( (intptr_t)this->storage == 0 ) { |
---|
| 145 | abortf( "Attempt to allocate %d bytes of storage for coroutine or task execution-state but insufficient memory available.", this->size ); |
---|
| 146 | } // if |
---|
| 147 | |
---|
| 148 | LIB_DEBUG_DO( this->limit = (char *)this->storage + pageSize ); |
---|
| 149 | LIB_NO_DEBUG_DO( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment |
---|
| 150 | |
---|
| 151 | } else { |
---|
| 152 | assertf( ((size_t)this->storage & (libAlign() - 1)) != 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", this->storage, (int)libAlign() ); |
---|
| 153 | this->userStack = true; |
---|
| 154 | this->size = storageSize - cxtSize; |
---|
| 155 | |
---|
| 156 | if ( this->size % 16 != 0u ) this->size -= 8; |
---|
| 157 | |
---|
| 158 | this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ); // minimum alignment |
---|
| 159 | } // if |
---|
| 160 | assertf( this->size >= MinStackSize, "Stack size %d provides less than minimum of %d bytes for a stack.", this->size, MinStackSize ); |
---|
| 161 | |
---|
| 162 | this->base = (char *)this->limit + this->size; |
---|
| 163 | this->context = this->base; |
---|
| 164 | this->top = (char *)this->context + cxtSize; |
---|
| 165 | } |
---|
| 166 | |
---|
[0c92c9f] | 167 | // We need to call suspend from invoke.c, so we expose this wrapper that |
---|
| 168 | // is not inline (We can't inline Cforall in C) |
---|
| 169 | extern "C" { |
---|
| 170 | void __suspend_internal(void) { |
---|
| 171 | suspend(); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
[6a3d2e7] | 175 | // Local Variables: // |
---|
| 176 | // mode: c // |
---|
| 177 | // tab-width: 4 // |
---|
| 178 | // End: // |
---|