[6a3d2e7] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[75a17f1] | 7 | // coroutine.c --
|
---|
[6a3d2e7] | 8 | //
|
---|
| 9 | // Author : Thierry Delisle
|
---|
| 10 | // Created On : Mon Nov 28 12:27:26 2016
|
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[b10affd] | 12 | // Last Modified On : Fri Mar 30 17:20:57 2018
|
---|
| 13 | // Update Count : 9
|
---|
[6a3d2e7] | 14 | //
|
---|
| 15 |
|
---|
[58b6d1b] | 16 | #include "coroutine.hfa"
|
---|
[bd98b58] | 17 |
|
---|
[6a3d2e7] | 18 | extern "C" {
|
---|
| 19 | #include <stddef.h>
|
---|
| 20 | #include <malloc.h>
|
---|
| 21 | #include <errno.h>
|
---|
| 22 | #include <string.h>
|
---|
| 23 | #include <unistd.h>
|
---|
[76e069f] | 24 | // use this define to make unwind.h play nice, definetely a hack
|
---|
| 25 | #define HIDE_EXPORTS
|
---|
| 26 | #include <unwind.h>
|
---|
| 27 | #undef HIDE_EXPORTS
|
---|
[6a3d2e7] | 28 | #include <sys/mman.h>
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[73abe95] | 31 | #include "kernel_private.hfa"
|
---|
[6a3d2e7] | 32 |
|
---|
| 33 | #define __CFA_INVOKE_PRIVATE__
|
---|
| 34 | #include "invoke.h"
|
---|
| 35 |
|
---|
[76e069f] | 36 | extern "C" {
|
---|
| 37 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage) __attribute__ ((__noreturn__));
|
---|
| 38 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__));
|
---|
| 39 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) {
|
---|
| 40 | abort();
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[6a3d2e7] | 44 | //-----------------------------------------------------------------------------
|
---|
| 45 | // Global state variables
|
---|
| 46 |
|
---|
| 47 | // minimum feasible stack size in bytes
|
---|
| 48 | #define MinStackSize 1000
|
---|
| 49 | static size_t pageSize = 0; // architecture pagesize HACK, should go in proper runtime singleton
|
---|
| 50 |
|
---|
| 51 | //-----------------------------------------------------------------------------
|
---|
| 52 | // Coroutine ctors and dtors
|
---|
[de6319f] | 53 | void ?{}( coStack_t & this, void * storage, size_t storageSize ) with( this ) {
|
---|
| 54 | size = storageSize == 0 ? 65000 : storageSize; // size of stack
|
---|
| 55 | this.storage = storage; // pointer to stack
|
---|
| 56 | limit = NULL; // stack grows towards stack limit
|
---|
| 57 | base = NULL; // base of stack
|
---|
| 58 | context = NULL; // address of cfa_context_t
|
---|
| 59 | top = NULL; // address of top of storage
|
---|
| 60 | userStack = storage != NULL;
|
---|
[6a3d2e7] | 61 | }
|
---|
| 62 |
|
---|
[de6319f] | 63 | void ^?{}(coStack_t & this) {
|
---|
| 64 | if ( ! this.userStack && this.storage ) {
|
---|
| 65 | __cfaabi_dbg_debug_do(
|
---|
| 66 | if ( mprotect( this.storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
|
---|
| 67 | abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
|
---|
| 68 | }
|
---|
| 69 | );
|
---|
| 70 | free( this.storage );
|
---|
| 71 | }
|
---|
[6a3d2e7] | 72 | }
|
---|
| 73 |
|
---|
[de6319f] | 74 | void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ) with( this ) {
|
---|
| 75 | (this.stack){storage, storageSize};
|
---|
| 76 | this.name = name;
|
---|
| 77 | errno_ = 0;
|
---|
| 78 | state = Start;
|
---|
| 79 | starter = NULL;
|
---|
| 80 | last = NULL;
|
---|
[76e069f] | 81 | cancellation = NULL;
|
---|
[6a3d2e7] | 82 | }
|
---|
| 83 |
|
---|
[76e069f] | 84 | void ^?{}(coroutine_desc& this) {
|
---|
[604d77b] | 85 | if(this.state != Halted && this.state != Start) {
|
---|
[76e069f] | 86 | coroutine_desc * src = TL_GET( this_coroutine );
|
---|
| 87 | coroutine_desc * dst = &this;
|
---|
| 88 |
|
---|
| 89 | struct _Unwind_Exception storage;
|
---|
| 90 | storage.exception_class = -1;
|
---|
| 91 | storage.exception_cleanup = _CtxCoroutine_UnwindCleanup;
|
---|
| 92 | this.cancellation = &storage;
|
---|
| 93 | this.last = src;
|
---|
| 94 |
|
---|
| 95 | // not resuming self ?
|
---|
| 96 | if ( src == dst ) {
|
---|
| 97 | abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src );
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | CoroutineCtxSwitch( src, dst );
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[6a3d2e7] | 103 |
|
---|
| 104 | // Part of the Public API
|
---|
| 105 | // Not inline since only ever called once per coroutine
|
---|
| 106 | forall(dtype T | is_coroutine(T))
|
---|
[83a071f9] | 107 | void prime(T& cor) {
|
---|
[de6319f] | 108 | coroutine_desc* this = get_coroutine(cor);
|
---|
| 109 | assert(this->state == Start);
|
---|
[6a3d2e7] | 110 |
|
---|
[de6319f] | 111 | this->state = Primed;
|
---|
| 112 | resume(cor);
|
---|
[6a3d2e7] | 113 | }
|
---|
| 114 |
|
---|
[0c92c9f] | 115 | // Wrapper for co
|
---|
[c3acb841] | 116 | void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
---|
[14a61b5] | 117 | // Safety note : This could cause some false positives due to preemption
|
---|
[afd550c] | 118 | verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
|
---|
[de6319f] | 119 | disable_interrupts();
|
---|
[6a3d2e7] | 120 |
|
---|
[de6319f] | 121 | // set state of current coroutine to inactive
|
---|
| 122 | src->state = src->state == Halted ? Halted : Inactive;
|
---|
[6a3d2e7] | 123 |
|
---|
[de6319f] | 124 | // set new coroutine that task is executing
|
---|
[14a61b5] | 125 | kernelTLS.this_coroutine = dst;
|
---|
[6a3d2e7] | 126 |
|
---|
[de6319f] | 127 | // context switch to specified coroutine
|
---|
| 128 | assert( src->stack.context );
|
---|
| 129 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
| 130 | // when CtxSwitch returns we are back in the src coroutine
|
---|
[6a3d2e7] | 131 |
|
---|
[de6319f] | 132 | // set state of new coroutine to active
|
---|
| 133 | src->state = Active;
|
---|
[6a3d2e7] | 134 |
|
---|
[de6319f] | 135 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[14a61b5] | 136 | // Safety note : This could cause some false positives due to preemption
|
---|
[afd550c] | 137 | verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
|
---|
[76e069f] | 138 |
|
---|
| 139 | if( unlikely(src->cancellation != NULL) ) {
|
---|
| 140 | _CtxCoroutine_Unwind(src->cancellation);
|
---|
| 141 | }
|
---|
[6a3d2e7] | 142 | } //ctxSwitchDirect
|
---|
| 143 |
|
---|
[65deb18] | 144 | void create_stack( coStack_t* this, unsigned int storageSize ) with( *this ) {
|
---|
[de6319f] | 145 | //TEMP HACK do this on proper kernel startup
|
---|
| 146 | if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE );
|
---|
| 147 |
|
---|
| 148 | size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment
|
---|
| 149 |
|
---|
| 150 | if ( !storage ) {
|
---|
| 151 | __cfaabi_dbg_print_safe("Kernel : Creating stack of size %zu for stack obj %p\n", cxtSize + size + 8, this);
|
---|
[6a3d2e7] | 152 |
|
---|
[de6319f] | 153 | userStack = false;
|
---|
| 154 | size = libCeiling( storageSize, 16 );
|
---|
| 155 | // use malloc/memalign because "new" raises an exception for out-of-memory
|
---|
[6a3d2e7] | 156 |
|
---|
[de6319f] | 157 | // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
|
---|
| 158 | __cfaabi_dbg_debug_do( storage = memalign( pageSize, cxtSize + size + pageSize ) );
|
---|
| 159 | __cfaabi_dbg_no_debug_do( storage = malloc( cxtSize + size + 8 ) );
|
---|
[47ecf2b] | 160 |
|
---|
[de6319f] | 161 | __cfaabi_dbg_debug_do(
|
---|
| 162 | if ( mprotect( storage, pageSize, PROT_NONE ) == -1 ) {
|
---|
| 163 | abort( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
|
---|
| 164 | } // if
|
---|
| 165 | );
|
---|
[6a3d2e7] | 166 |
|
---|
[de6319f] | 167 | if ( (intptr_t)storage == 0 ) {
|
---|
| 168 | abort( "Attempt to allocate %zd bytes of storage for coroutine or task execution-state but insufficient memory available.", size );
|
---|
| 169 | } // if
|
---|
[6a3d2e7] | 170 |
|
---|
[de6319f] | 171 | __cfaabi_dbg_debug_do( limit = (char *)storage + pageSize );
|
---|
| 172 | __cfaabi_dbg_no_debug_do( limit = (char *)libCeiling( (unsigned long)storage, 16 ) ); // minimum alignment
|
---|
[6a3d2e7] | 173 |
|
---|
[de6319f] | 174 | } else {
|
---|
| 175 | __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%u bytes)\n", this, storage, storageSize);
|
---|
[6a3d2e7] | 176 |
|
---|
[de6319f] | 177 | assertf( ((size_t)storage & (libAlign() - 1)) == 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", storage, (int)libAlign() );
|
---|
| 178 | userStack = true;
|
---|
| 179 | size = storageSize - cxtSize;
|
---|
[6a3d2e7] | 180 |
|
---|
[de6319f] | 181 | if ( size % 16 != 0u ) size -= 8;
|
---|
[6a3d2e7] | 182 |
|
---|
[de6319f] | 183 | limit = (char *)libCeiling( (unsigned long)storage, 16 ); // minimum alignment
|
---|
| 184 | } // if
|
---|
| 185 | assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize );
|
---|
[6a3d2e7] | 186 |
|
---|
[de6319f] | 187 | base = (char *)limit + size;
|
---|
| 188 | context = base;
|
---|
| 189 | top = (char *)context + cxtSize;
|
---|
[6a3d2e7] | 190 | }
|
---|
| 191 |
|
---|
[0c92c9f] | 192 | // We need to call suspend from invoke.c, so we expose this wrapper that
|
---|
| 193 | // is not inline (We can't inline Cforall in C)
|
---|
| 194 | extern "C" {
|
---|
[de6319f] | 195 | void __suspend_internal(void) {
|
---|
| 196 | suspend();
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[76e069f] | 199 | void __leave_coroutine() {
|
---|
[de6319f] | 200 | coroutine_desc * src = TL_GET( this_coroutine ); // optimization
|
---|
[76e069f] | 201 | coroutine_desc * starter = src->cancellation != 0 ? src->last : src->starter;
|
---|
[de6319f] | 202 |
|
---|
[f23b685] | 203 | src->state = Halted;
|
---|
| 204 |
|
---|
[76e069f] | 205 | assertf( starter != 0,
|
---|
[de6319f] | 206 | "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
---|
| 207 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
|
---|
| 208 | src->name, src );
|
---|
[76e069f] | 209 | assertf( starter->state != Halted,
|
---|
[de6319f] | 210 | "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
|
---|
| 211 | "Possible cause is terminated coroutine's main routine has already returned.",
|
---|
[76e069f] | 212 | src->name, src, starter->name, starter );
|
---|
[de6319f] | 213 |
|
---|
[76e069f] | 214 | CoroutineCtxSwitch( src, starter );
|
---|
[de6319f] | 215 | }
|
---|
[0c92c9f] | 216 | }
|
---|
| 217 |
|
---|
[6a3d2e7] | 218 | // Local Variables: //
|
---|
| 219 | // mode: c //
|
---|
| 220 | // tab-width: 4 //
|
---|
[4aa2fb2] | 221 | // End: //
|
---|