[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 |
|
---|
[9cc0472] | 28 | #include "kernel_private.h"
|
---|
[6a3d2e7] | 29 |
|
---|
| 30 | #define __CFA_INVOKE_PRIVATE__
|
---|
| 31 | #include "invoke.h"
|
---|
| 32 |
|
---|
[bd98b58] | 33 |
|
---|
[6a3d2e7] | 34 | //-----------------------------------------------------------------------------
|
---|
| 35 | // Global state variables
|
---|
| 36 |
|
---|
| 37 | // minimum feasible stack size in bytes
|
---|
| 38 | #define MinStackSize 1000
|
---|
| 39 | static size_t pageSize = 0; // architecture pagesize HACK, should go in proper runtime singleton
|
---|
| 40 |
|
---|
| 41 | //-----------------------------------------------------------------------------
|
---|
| 42 | // Coroutine ctors and dtors
|
---|
| 43 | void ?{}(coStack_t* this) {
|
---|
[47ecf2b] | 44 | this->size = 65000; // size of stack
|
---|
[6a3d2e7] | 45 | this->storage = NULL; // pointer to stack
|
---|
| 46 | this->limit = NULL; // stack grows towards stack limit
|
---|
| 47 | this->base = NULL; // base of stack
|
---|
| 48 | this->context = NULL; // address of cfa_context_t
|
---|
| 49 | this->top = NULL; // address of top of storage
|
---|
[47ecf2b] | 50 | this->userStack = false;
|
---|
[6a3d2e7] | 51 | }
|
---|
| 52 |
|
---|
| 53 | void ?{}(coStack_t* this, size_t size) {
|
---|
| 54 | this{};
|
---|
| 55 | this->size = size;
|
---|
| 56 |
|
---|
| 57 | create_stack(this, this->size);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[c3acb841] | 60 | void ?{}(coroutine_desc* this) {
|
---|
[db6f06a] | 61 | this{ "Anonymous Coroutine" };
|
---|
[6a3d2e7] | 62 | }
|
---|
| 63 |
|
---|
[c3acb841] | 64 | void ?{}(coroutine_desc* this, const char * name) {
|
---|
[84e2523] | 65 | this->name = name;
|
---|
| 66 | this->errno_ = 0;
|
---|
| 67 | this->state = Start;
|
---|
| 68 | this->starter = NULL;
|
---|
| 69 | this->last = NULL;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[c3acb841] | 72 | void ?{}(coroutine_desc* this, size_t size) {
|
---|
[6a3d2e7] | 73 | this{};
|
---|
| 74 | (&this->stack){size};
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void ^?{}(coStack_t* this) {
|
---|
| 78 | if ( ! this->userStack ) {
|
---|
| 79 | LIB_DEBUG_DO(
|
---|
| 80 | if ( mprotect( this->storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
|
---|
| 81 | abortf( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", this, errno, strerror( errno ) );
|
---|
| 82 | }
|
---|
| 83 | );
|
---|
| 84 | free( this->storage );
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[c3acb841] | 88 | void ^?{}(coroutine_desc* this) {}
|
---|
[6a3d2e7] | 89 |
|
---|
| 90 | // Part of the Public API
|
---|
| 91 | // Not inline since only ever called once per coroutine
|
---|
| 92 | forall(dtype T | is_coroutine(T))
|
---|
| 93 | void prime(T* cor) {
|
---|
[c3acb841] | 94 | coroutine_desc* this = get_coroutine(cor);
|
---|
[6a3d2e7] | 95 | assert(this->state == Start);
|
---|
| 96 |
|
---|
| 97 | this->state = Primed;
|
---|
| 98 | resume(cor);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[0c92c9f] | 101 | // Wrapper for co
|
---|
[c3acb841] | 102 | void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
---|
[6a3d2e7] | 103 | // THREAD_GETMEM( This )->disableInterrupts();
|
---|
| 104 |
|
---|
| 105 | // set state of current coroutine to inactive
|
---|
[1c273d0] | 106 | src->state = src->state == Halted ? Halted : Inactive;
|
---|
[6a3d2e7] | 107 |
|
---|
| 108 | // set new coroutine that task is executing
|
---|
[1c273d0] | 109 | this_coroutine = dst;
|
---|
[6a3d2e7] | 110 |
|
---|
| 111 | // context switch to specified coroutine
|
---|
[1c273d0] | 112 | assert( src->stack.context );
|
---|
[6a3d2e7] | 113 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
[47ecf2b] | 114 | // when CtxSwitch returns we are back in the src coroutine
|
---|
[6a3d2e7] | 115 |
|
---|
| 116 | // set state of new coroutine to active
|
---|
| 117 | src->state = Active;
|
---|
| 118 |
|
---|
| 119 | // THREAD_GETMEM( This )->enableInterrupts();
|
---|
| 120 | } //ctxSwitchDirect
|
---|
| 121 |
|
---|
| 122 | void create_stack( coStack_t* this, unsigned int storageSize ) {
|
---|
| 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 |
|
---|
| 128 | if ( (intptr_t)this->storage == 0 ) {
|
---|
| 129 | this->userStack = false;
|
---|
| 130 | this->size = libCeiling( storageSize, 16 );
|
---|
| 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
|
---|
| 134 | LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) );
|
---|
| 135 | LIB_NO_DEBUG_DO( this->storage = malloc( cxtSize + this->size + 8 ) );
|
---|
| 136 |
|
---|
| 137 | LIB_DEBUG_DO(
|
---|
| 138 | if ( mprotect( this->storage, pageSize, PROT_NONE ) == -1 ) {
|
---|
| 139 | abortf( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
|
---|
| 140 | } // if
|
---|
| 141 | );
|
---|
| 142 |
|
---|
| 143 | if ( (intptr_t)this->storage == 0 ) {
|
---|
| 144 | abortf( "Attempt to allocate %d bytes of storage for coroutine or task execution-state but insufficient memory available.", this->size );
|
---|
| 145 | } // if
|
---|
| 146 |
|
---|
| 147 | LIB_DEBUG_DO( this->limit = (char *)this->storage + pageSize );
|
---|
| 148 | LIB_NO_DEBUG_DO( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment
|
---|
| 149 |
|
---|
| 150 | } else {
|
---|
| 151 | 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() );
|
---|
| 152 | this->userStack = true;
|
---|
| 153 | this->size = storageSize - cxtSize;
|
---|
| 154 |
|
---|
| 155 | if ( this->size % 16 != 0u ) this->size -= 8;
|
---|
| 156 |
|
---|
| 157 | this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ); // minimum alignment
|
---|
| 158 | } // if
|
---|
| 159 | assertf( this->size >= MinStackSize, "Stack size %d provides less than minimum of %d bytes for a stack.", this->size, MinStackSize );
|
---|
| 160 |
|
---|
| 161 | this->base = (char *)this->limit + this->size;
|
---|
| 162 | this->context = this->base;
|
---|
| 163 | this->top = (char *)this->context + cxtSize;
|
---|
| 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 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[6a3d2e7] | 174 | // Local Variables: //
|
---|
| 175 | // mode: c //
|
---|
| 176 | // tab-width: 4 //
|
---|
[4aa2fb2] | 177 | // End: //
|
---|