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