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 Mar 30 17:20:57 2018 |
---|
13 | // Update Count : 9 |
---|
14 | // |
---|
15 | |
---|
16 | #include "coroutine.hfa" |
---|
17 | |
---|
18 | extern "C" { |
---|
19 | #include <stddef.h> |
---|
20 | #include <malloc.h> |
---|
21 | #include <errno.h> |
---|
22 | #include <string.h> |
---|
23 | #include <unistd.h> |
---|
24 | // use this define to make unwind.h play nice, definetely a hack |
---|
25 | #define HIDE_EXPORTS |
---|
26 | #include <unwind.h> |
---|
27 | #undef HIDE_EXPORTS |
---|
28 | #include <sys/mman.h> |
---|
29 | } |
---|
30 | |
---|
31 | #include "kernel_private.hfa" |
---|
32 | |
---|
33 | #define __CFA_INVOKE_PRIVATE__ |
---|
34 | #include "invoke.h" |
---|
35 | |
---|
36 | extern "C" { |
---|
37 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc *) __attribute__ ((__noreturn__)); |
---|
38 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__)); |
---|
39 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) { |
---|
40 | abort(); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | //----------------------------------------------------------------------------- |
---|
45 | // Global state variables |
---|
46 | |
---|
47 | // minimum feasible stack size in bytes |
---|
48 | #define MinStackSize 1000 |
---|
49 | extern size_t __page_size; // architecture pagesize HACK, should go in proper runtime singleton |
---|
50 | |
---|
51 | void __stack_prepare( __stack_info_t * this, size_t create_size ); |
---|
52 | |
---|
53 | //----------------------------------------------------------------------------- |
---|
54 | // Coroutine ctors and dtors |
---|
55 | void ?{}( __stack_info_t & this, void * storage, size_t storageSize ) { |
---|
56 | this.storage = (__stack_t *)storage; |
---|
57 | |
---|
58 | // Did we get a piece of storage ? |
---|
59 | if (this.storage || storageSize != 0) { |
---|
60 | // We either got a piece of storage or the user asked for a specific size |
---|
61 | // Immediately create the stack |
---|
62 | // (This is slightly unintuitive that non-default sized coroutines create are eagerly created |
---|
63 | // but it avoids that all coroutines carry an unnecessary size) |
---|
64 | verify( storageSize != 0 ); |
---|
65 | __stack_prepare( &this, storageSize ); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | void ^?{}(__stack_info_t & this) { |
---|
70 | bool userStack = ((intptr_t)this.storage & 0x1) != 0; |
---|
71 | if ( ! userStack && this.storage ) { |
---|
72 | *((intptr_t*)&this.storage) &= (intptr_t)-1; |
---|
73 | void * storage = this.storage->limit; |
---|
74 | __cfaabi_dbg_debug_do( |
---|
75 | storage = (char*)(storage) - __page_size; |
---|
76 | if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) { |
---|
77 | abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) ); |
---|
78 | } |
---|
79 | ); |
---|
80 | __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage); |
---|
81 | free( storage ); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ) with( this ) { |
---|
86 | (this.stack){storage, storageSize}; |
---|
87 | this.name = name; |
---|
88 | state = Start; |
---|
89 | starter = NULL; |
---|
90 | last = NULL; |
---|
91 | cancellation = NULL; |
---|
92 | } |
---|
93 | |
---|
94 | void ^?{}(coroutine_desc& this) { |
---|
95 | if(this.state != Halted && this.state != Start) { |
---|
96 | coroutine_desc * src = TL_GET( this_thread )->curr_cor; |
---|
97 | coroutine_desc * dst = &this; |
---|
98 | |
---|
99 | struct _Unwind_Exception storage; |
---|
100 | storage.exception_class = -1; |
---|
101 | storage.exception_cleanup = _CtxCoroutine_UnwindCleanup; |
---|
102 | this.cancellation = &storage; |
---|
103 | this.last = src; |
---|
104 | |
---|
105 | // not resuming self ? |
---|
106 | if ( src == dst ) { |
---|
107 | abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src ); |
---|
108 | } |
---|
109 | |
---|
110 | CoroutineCtxSwitch( src, dst ); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | // Part of the Public API |
---|
115 | // Not inline since only ever called once per coroutine |
---|
116 | forall(dtype T | is_coroutine(T)) |
---|
117 | void prime(T& cor) { |
---|
118 | coroutine_desc* this = get_coroutine(cor); |
---|
119 | assert(this->state == Start); |
---|
120 | |
---|
121 | this->state = Primed; |
---|
122 | resume(cor); |
---|
123 | } |
---|
124 | |
---|
125 | // Wrapper for co |
---|
126 | void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { |
---|
127 | // Safety note : Preemption must be disabled since there is a race condition |
---|
128 | // kernelTLS.this_thread->curr_cor and $rsp/$rbp must agree at all times |
---|
129 | verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); |
---|
130 | disable_interrupts(); |
---|
131 | |
---|
132 | // set state of current coroutine to inactive |
---|
133 | src->state = src->state == Halted ? Halted : Inactive; |
---|
134 | |
---|
135 | // set new coroutine that task is executing |
---|
136 | TL_GET( this_thread )->curr_cor = dst; |
---|
137 | |
---|
138 | // context switch to specified coroutine |
---|
139 | CtxSwitch( &src->context, &dst->context ); |
---|
140 | // when CtxSwitch returns we are back in the src coroutine |
---|
141 | |
---|
142 | // set state of new coroutine to active |
---|
143 | src->state = Active; |
---|
144 | |
---|
145 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
146 | verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); |
---|
147 | |
---|
148 | if( unlikely(src->cancellation != NULL) ) { |
---|
149 | _CtxCoroutine_Unwind(src->cancellation, src); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | [void *, size_t] __stack_alloc( size_t storageSize ) { |
---|
154 | static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment |
---|
155 | assert(__page_size != 0l); |
---|
156 | size_t size = libCeiling( storageSize, 16 ) + stack_data_size; |
---|
157 | |
---|
158 | // If we are running debug, we also need to allocate a guardpage to catch stack overflows. |
---|
159 | void * storage; |
---|
160 | __cfaabi_dbg_debug_do( |
---|
161 | storage = memalign( __page_size, size + __page_size ); |
---|
162 | ); |
---|
163 | __cfaabi_dbg_no_debug_do( |
---|
164 | storage = (void*)malloc(size); |
---|
165 | ); |
---|
166 | |
---|
167 | __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size); |
---|
168 | __cfaabi_dbg_debug_do( |
---|
169 | if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) { |
---|
170 | abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) ); |
---|
171 | } |
---|
172 | storage = (void *)(((intptr_t)storage) + __page_size); |
---|
173 | ); |
---|
174 | |
---|
175 | verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); |
---|
176 | return [storage, size]; |
---|
177 | } |
---|
178 | |
---|
179 | void __stack_prepare( __stack_info_t * this, size_t create_size ) { |
---|
180 | static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment |
---|
181 | bool userStack; |
---|
182 | void * storage; |
---|
183 | size_t size; |
---|
184 | if ( !this->storage ) { |
---|
185 | userStack = false; |
---|
186 | [storage, size] = __stack_alloc( create_size ); |
---|
187 | } else { |
---|
188 | userStack = true; |
---|
189 | __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%zu bytes)\n", this, this->storage, this->storage->size); |
---|
190 | |
---|
191 | // The stack must be aligned, advance the pointer to the next align data |
---|
192 | storage = (void*)libCeiling( (intptr_t)this->storage, libAlign()); |
---|
193 | |
---|
194 | // The size needs to be shrinked to fit all the extra data structure and be aligned |
---|
195 | ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage; |
---|
196 | size = libFloor(create_size - stack_data_size - diff, libAlign()); |
---|
197 | } // if |
---|
198 | assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize ); |
---|
199 | |
---|
200 | this->storage = (__stack_t *)((intptr_t)storage + size); |
---|
201 | this->storage->size = size; |
---|
202 | this->storage->limit = storage; |
---|
203 | this->storage->base = (void*)((intptr_t)storage + size); |
---|
204 | *((intptr_t*)&this->storage) |= userStack ? 0x1 : 0x0; |
---|
205 | } |
---|
206 | |
---|
207 | // We need to call suspend from invoke.c, so we expose this wrapper that |
---|
208 | // is not inline (We can't inline Cforall in C) |
---|
209 | extern "C" { |
---|
210 | void __suspend_internal(void) { |
---|
211 | suspend(); |
---|
212 | } |
---|
213 | |
---|
214 | void __leave_coroutine( coroutine_desc * src ) { |
---|
215 | coroutine_desc * starter = src->cancellation != 0 ? src->last : src->starter; |
---|
216 | |
---|
217 | src->state = Halted; |
---|
218 | |
---|
219 | assertf( starter != 0, |
---|
220 | "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n" |
---|
221 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", |
---|
222 | src->name, src ); |
---|
223 | assertf( starter->state != Halted, |
---|
224 | "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n" |
---|
225 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
226 | src->name, src, starter->name, starter ); |
---|
227 | |
---|
228 | CoroutineCtxSwitch( src, starter ); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | // Local Variables: // |
---|
233 | // mode: c // |
---|
234 | // tab-width: 4 // |
---|
235 | // End: // |
---|