source: src/libcfa/concurrency/coroutine.c @ b462670

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since b462670 was b462670, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Coroutines should now properly use the first resumner as their starter

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