[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 |
|
---|
[75a17f1] | 16 | #include "coroutine"
|
---|
[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>
|
---|
| 24 | #include <sys/mman.h>
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[9cc0472] | 27 | #include "kernel_private.h"
|
---|
[6a3d2e7] | 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
|
---|
[c40e7c5] | 41 | void ?{}(coStack_t& this) with( this ) {
|
---|
| 42 | size = 65000; // size of stack
|
---|
| 43 | storage = NULL; // pointer to stack
|
---|
| 44 | limit = NULL; // stack grows towards stack limit
|
---|
| 45 | base = NULL; // base of stack
|
---|
| 46 | context = NULL; // address of cfa_context_t
|
---|
| 47 | top = NULL; // address of top of storage
|
---|
| 48 | userStack = false;
|
---|
[6a3d2e7] | 49 | }
|
---|
| 50 |
|
---|
[242a902] | 51 | void ?{}(coStack_t& this, size_t size) {
|
---|
[6a3d2e7] | 52 | this{};
|
---|
[242a902] | 53 | this.size = size;
|
---|
[6a3d2e7] | 54 |
|
---|
[242a902] | 55 | create_stack(&this, this.size);
|
---|
[6a3d2e7] | 56 | }
|
---|
| 57 |
|
---|
[242a902] | 58 | void ?{}(coroutine_desc& this) {
|
---|
[db6f06a] | 59 | this{ "Anonymous Coroutine" };
|
---|
[6a3d2e7] | 60 | }
|
---|
| 61 |
|
---|
[c40e7c5] | 62 | void ?{}(coroutine_desc& this, const char * name) with( this ) {
|
---|
[242a902] | 63 | this.name = name;
|
---|
[c40e7c5] | 64 | errno_ = 0;
|
---|
| 65 | state = Start;
|
---|
| 66 | starter = NULL;
|
---|
| 67 | last = NULL;
|
---|
[84e2523] | 68 | }
|
---|
| 69 |
|
---|
[242a902] | 70 | void ?{}(coroutine_desc& this, size_t size) {
|
---|
[6a3d2e7] | 71 | this{};
|
---|
[242a902] | 72 | (this.stack){size};
|
---|
[6a3d2e7] | 73 | }
|
---|
| 74 |
|
---|
[ed235b6] | 75 | void ^?{}(coStack_t & this) {
|
---|
| 76 | if ( ! this.userStack && this.storage ) {
|
---|
[36982fc] | 77 | __cfaabi_dbg_debug_do(
|
---|
[242a902] | 78 | if ( mprotect( this.storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
|
---|
[169d944] | 79 | abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
|
---|
[6a3d2e7] | 80 | }
|
---|
| 81 | );
|
---|
[242a902] | 82 | free( this.storage );
|
---|
[6a3d2e7] | 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[242a902] | 86 | void ^?{}(coroutine_desc& this) {}
|
---|
[6a3d2e7] | 87 |
|
---|
| 88 | // Part of the Public API
|
---|
| 89 | // Not inline since only ever called once per coroutine
|
---|
| 90 | forall(dtype T | is_coroutine(T))
|
---|
[83a071f9] | 91 | void prime(T& cor) {
|
---|
[c3acb841] | 92 | coroutine_desc* this = get_coroutine(cor);
|
---|
[6a3d2e7] | 93 | assert(this->state == Start);
|
---|
| 94 |
|
---|
| 95 | this->state = Primed;
|
---|
| 96 | resume(cor);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[0c92c9f] | 99 | // Wrapper for co
|
---|
[c3acb841] | 100 | void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
---|
[b10affd] | 101 | verify( TL_GET( preemption_state ).enabled || TL_GET( this_processor )->do_terminate );
|
---|
[827a190] | 102 | disable_interrupts();
|
---|
[6a3d2e7] | 103 |
|
---|
| 104 | // set state of current coroutine to inactive
|
---|
[1c273d0] | 105 | src->state = src->state == Halted ? Halted : Inactive;
|
---|
[6a3d2e7] | 106 |
|
---|
| 107 | // set new coroutine that task is executing
|
---|
[b10affd] | 108 | TL_SET( this_coroutine, dst );
|
---|
[6a3d2e7] | 109 |
|
---|
| 110 | // context switch to specified coroutine
|
---|
[1c273d0] | 111 | assert( src->stack.context );
|
---|
[6a3d2e7] | 112 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
[47ecf2b] | 113 | // when CtxSwitch returns we are back in the src coroutine
|
---|
[6a3d2e7] | 114 |
|
---|
| 115 | // set state of new coroutine to active
|
---|
| 116 | src->state = Active;
|
---|
| 117 |
|
---|
[827a190] | 118 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[b10affd] | 119 | verify( TL_GET( preemption_state ).enabled || TL_GET( this_processor )->do_terminate );
|
---|
[6a3d2e7] | 120 | } //ctxSwitchDirect
|
---|
| 121 |
|
---|
[65deb18] | 122 | void create_stack( coStack_t* this, unsigned int storageSize ) with( *this ) {
|
---|
[6a3d2e7] | 123 | //TEMP HACK do this on proper kernel startup
|
---|
| 124 | if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE );
|
---|
| 125 |
|
---|
| 126 | size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment
|
---|
| 127 |
|
---|
[65deb18] | 128 | if ( (intptr_t)storage == 0 ) {
|
---|
| 129 | userStack = false;
|
---|
| 130 | size = libCeiling( storageSize, 16 );
|
---|
[6a3d2e7] | 131 | // use malloc/memalign because "new" raises an exception for out-of-memory
|
---|
[47ecf2b] | 132 |
|
---|
[6a3d2e7] | 133 | // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
|
---|
[65deb18] | 134 | __cfaabi_dbg_debug_do( storage = memalign( pageSize, cxtSize + size + pageSize ) );
|
---|
| 135 | __cfaabi_dbg_no_debug_do( storage = malloc( cxtSize + size + 8 ) );
|
---|
[6a3d2e7] | 136 |
|
---|
[36982fc] | 137 | __cfaabi_dbg_debug_do(
|
---|
[65deb18] | 138 | if ( mprotect( storage, pageSize, PROT_NONE ) == -1 ) {
|
---|
[169d944] | 139 | abort( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
|
---|
[6a3d2e7] | 140 | } // if
|
---|
| 141 | );
|
---|
| 142 |
|
---|
[65deb18] | 143 | if ( (intptr_t)storage == 0 ) {
|
---|
[169d944] | 144 | abort( "Attempt to allocate %zd bytes of storage for coroutine or task execution-state but insufficient memory available.", size );
|
---|
[6a3d2e7] | 145 | } // if
|
---|
| 146 |
|
---|
[65deb18] | 147 | __cfaabi_dbg_debug_do( limit = (char *)storage + pageSize );
|
---|
| 148 | __cfaabi_dbg_no_debug_do( limit = (char *)libCeiling( (unsigned long)storage, 16 ) ); // minimum alignment
|
---|
[6a3d2e7] | 149 |
|
---|
| 150 | } else {
|
---|
[65deb18] | 151 | assertf( ((size_t)storage & (libAlign() - 1)) != 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", storage, (int)libAlign() );
|
---|
| 152 | userStack = true;
|
---|
| 153 | size = storageSize - cxtSize;
|
---|
[6a3d2e7] | 154 |
|
---|
[65deb18] | 155 | if ( size % 16 != 0u ) size -= 8;
|
---|
[6a3d2e7] | 156 |
|
---|
[65deb18] | 157 | limit = (char *)libCeiling( (unsigned long)storage, 16 ); // minimum alignment
|
---|
[6a3d2e7] | 158 | } // if
|
---|
[65deb18] | 159 | assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize );
|
---|
[6a3d2e7] | 160 |
|
---|
[65deb18] | 161 | base = (char *)limit + size;
|
---|
| 162 | context = base;
|
---|
| 163 | top = (char *)context + cxtSize;
|
---|
[6a3d2e7] | 164 | }
|
---|
| 165 |
|
---|
[0c92c9f] | 166 | // We need to call suspend from invoke.c, so we expose this wrapper that
|
---|
| 167 | // is not inline (We can't inline Cforall in C)
|
---|
| 168 | extern "C" {
|
---|
| 169 | void __suspend_internal(void) {
|
---|
| 170 | suspend();
|
---|
| 171 | }
|
---|
[39fea2f] | 172 |
|
---|
| 173 | void __leave_coroutine(void) {
|
---|
[b10affd] | 174 | coroutine_desc * src = TL_GET( this_coroutine ); // optimization
|
---|
[39fea2f] | 175 |
|
---|
| 176 | assertf( src->starter != 0,
|
---|
[b462670] | 177 | "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
---|
| 178 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
|
---|
[39fea2f] | 179 | src->name, src );
|
---|
| 180 | assertf( src->starter->state != Halted,
|
---|
[b462670] | 181 | "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
|
---|
[39fea2f] | 182 | "Possible cause is terminated coroutine's main routine has already returned.",
|
---|
| 183 | src->name, src, src->starter->name, src->starter );
|
---|
| 184 |
|
---|
| 185 | CoroutineCtxSwitch( src, src->starter );
|
---|
| 186 | }
|
---|
[0c92c9f] | 187 | }
|
---|
| 188 |
|
---|
[6a3d2e7] | 189 | // Local Variables: //
|
---|
| 190 | // mode: c //
|
---|
| 191 | // tab-width: 4 //
|
---|
[4aa2fb2] | 192 | // End: //
|
---|