source: src/libcfa/concurrency/coroutines.c@ 092528b

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 092528b was bd98b58, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Kernel now uses intrusive lists and blocking locks for ready queue management.
Update the plan for concurrency.

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