source: src/libcfa/concurrency/coroutines.c @ 84e2523

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 84e2523 was 84e2523, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

added coroutine constructor for named coroutines

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