source: src/libcfa/concurrency/coroutines.c@ c84e80a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since c84e80a was 8118303, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

First prototype of cfa threads running (1 thread on 1 processor)

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