[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 | // |
---|
| 8 | // coroutines.c -- |
---|
| 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 | |
---|
[bd98b58] | 17 | #include "coroutines" |
---|
| 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 | |
---|
[8def349] | 34 | extern processor * get_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 | |
---|
| 62 | void ?{}(coroutine* this) { |
---|
| 63 | this->name = "Anonymous Coroutine"; |
---|
| 64 | this->errno_ = 0; |
---|
| 65 | this->state = Start; |
---|
| 66 | this->notHalted = true; |
---|
| 67 | this->starter = NULL; |
---|
| 68 | this->last = NULL; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void ?{}(coroutine* this, size_t size) { |
---|
| 72 | this{}; |
---|
| 73 | (&this->stack){size}; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void ^?{}(coStack_t* this) { |
---|
| 77 | if ( ! this->userStack ) { |
---|
| 78 | LIB_DEBUG_DO( |
---|
| 79 | if ( mprotect( this->storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) { |
---|
| 80 | abortf( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", this, errno, strerror( errno ) ); |
---|
| 81 | } |
---|
| 82 | ); |
---|
| 83 | free( this->storage ); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void ^?{}(coroutine* this) {} |
---|
| 88 | |
---|
| 89 | // Part of the Public API |
---|
| 90 | // Not inline since only ever called once per coroutine |
---|
| 91 | forall(dtype T | is_coroutine(T)) |
---|
| 92 | void prime(T* cor) { |
---|
| 93 | coroutine* this = get_coroutine(cor); |
---|
| 94 | assert(this->state == Start); |
---|
| 95 | |
---|
| 96 | this->state = Primed; |
---|
| 97 | resume(cor); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | // We need to call suspend from invoke.c, so we expose this wrapper that |
---|
| 101 | // is not inline (We can't inline Cforall in C) |
---|
| 102 | void suspend_no_inline(void) { |
---|
| 103 | suspend(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void corCxtSw(coroutine* src, coroutine* dst) { |
---|
| 107 | // THREAD_GETMEM( This )->disableInterrupts(); |
---|
| 108 | |
---|
| 109 | // set state of current coroutine to inactive |
---|
| 110 | src->state = Inactive; |
---|
| 111 | |
---|
| 112 | // set new coroutine that task is executing |
---|
[8def349] | 113 | get_this_processor()->current_coroutine = dst; |
---|
[6a3d2e7] | 114 | |
---|
| 115 | // context switch to specified coroutine |
---|
| 116 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
| 117 | // when CtxSwitch returns we are back in the src coroutine |
---|
| 118 | |
---|
| 119 | // set state of new coroutine to active |
---|
| 120 | src->state = Active; |
---|
| 121 | |
---|
| 122 | // THREAD_GETMEM( This )->enableInterrupts(); |
---|
| 123 | } //ctxSwitchDirect |
---|
| 124 | |
---|
| 125 | void create_stack( coStack_t* this, unsigned int storageSize ) { |
---|
| 126 | //TEMP HACK do this on proper kernel startup |
---|
| 127 | if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE ); |
---|
| 128 | |
---|
| 129 | size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment |
---|
| 130 | |
---|
| 131 | if ( (intptr_t)this->storage == 0 ) { |
---|
| 132 | this->userStack = false; |
---|
| 133 | this->size = libCeiling( storageSize, 16 ); |
---|
| 134 | // use malloc/memalign because "new" raises an exception for out-of-memory |
---|
| 135 | |
---|
| 136 | // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment |
---|
| 137 | LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) ); |
---|
| 138 | LIB_NO_DEBUG_DO( this->storage = malloc( cxtSize + this->size + 8 ) ); |
---|
| 139 | |
---|
| 140 | LIB_DEBUG_DO( |
---|
| 141 | if ( mprotect( this->storage, pageSize, PROT_NONE ) == -1 ) { |
---|
| 142 | abortf( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) ); |
---|
| 143 | } // if |
---|
| 144 | ); |
---|
| 145 | |
---|
| 146 | if ( (intptr_t)this->storage == 0 ) { |
---|
| 147 | abortf( "Attempt to allocate %d bytes of storage for coroutine or task execution-state but insufficient memory available.", this->size ); |
---|
| 148 | } // if |
---|
| 149 | |
---|
| 150 | LIB_DEBUG_DO( this->limit = (char *)this->storage + pageSize ); |
---|
| 151 | LIB_NO_DEBUG_DO( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment |
---|
| 152 | |
---|
| 153 | } else { |
---|
| 154 | 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() ); |
---|
| 155 | this->userStack = true; |
---|
| 156 | this->size = storageSize - cxtSize; |
---|
| 157 | |
---|
| 158 | if ( this->size % 16 != 0u ) this->size -= 8; |
---|
| 159 | |
---|
| 160 | this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ); // minimum alignment |
---|
| 161 | } // if |
---|
| 162 | assertf( this->size >= MinStackSize, "Stack size %d provides less than minimum of %d bytes for a stack.", this->size, MinStackSize ); |
---|
| 163 | |
---|
| 164 | this->base = (char *)this->limit + this->size; |
---|
| 165 | this->context = this->base; |
---|
| 166 | this->top = (char *)this->context + cxtSize; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // Local Variables: // |
---|
| 170 | // mode: c // |
---|
| 171 | // tab-width: 4 // |
---|
| 172 | // End: // |
---|