source: src/libcfa/concurrency/threads.c@ 57f408e

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 57f408e was 80d9e49, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Coroutines no longer require virtual pointers or any code beyond the get_coroutine routine

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