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 | |
---|
16 | extern "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 |
---|
34 | static size_t pageSize = 0; // architecture pagesize |
---|
35 | |
---|
36 | static coroutine main_coroutine; |
---|
37 | static coroutine* current_coroutine = &main_coroutine; |
---|
38 | |
---|
39 | coroutine* this_coroutine(void) { |
---|
40 | return current_coroutine; |
---|
41 | } |
---|
42 | |
---|
43 | void ctxSwitchDirect(coroutine* src, coroutine* dst); |
---|
44 | void create_stack( coStack_t* this, unsigned int storageSize ); // used by all constructors |
---|
45 | |
---|
46 | void ?{}(coStack_t* this) { |
---|
47 | this->size = 10240; // size of stack |
---|
48 | this->storage = NULL; // pointer to stack |
---|
49 | this->limit = NULL; // stack grows towards stack limit |
---|
50 | this->base = NULL; // base of stack |
---|
51 | this->context = NULL; // address of cfa_context_t |
---|
52 | this->top = NULL; // address of top of storage |
---|
53 | this->userStack = false; |
---|
54 | |
---|
55 | create_stack(this, this->size); |
---|
56 | } |
---|
57 | |
---|
58 | void ?{}(coroutine* this) |
---|
59 | { |
---|
60 | this->name = "Anonymous Coroutine"; |
---|
61 | this->errno_ = 0; |
---|
62 | this->state = Start; |
---|
63 | this->notHalted = true; |
---|
64 | this->starter = NULL; |
---|
65 | this->last = NULL; |
---|
66 | } |
---|
67 | |
---|
68 | void ?{}(coroutine* this, covptr_t* object) |
---|
69 | { |
---|
70 | this{}; |
---|
71 | |
---|
72 | startCoroutine(object, invokeCoroutine); |
---|
73 | } |
---|
74 | |
---|
75 | void suspend() { |
---|
76 | coroutine* src = this_coroutine(); // optimization |
---|
77 | |
---|
78 | assertf( src->last != 0, |
---|
79 | "Attempt to suspend coroutine %.256s (%p) that has never been resumed.\n" |
---|
80 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", |
---|
81 | src->name, src ); |
---|
82 | assertf( src->last->notHalted, |
---|
83 | "Attempt by coroutine %.256s (%p) to suspend back to terminated coroutine %.256s (%p).\n" |
---|
84 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
85 | src->name, src, src->last->name, src->last ); |
---|
86 | |
---|
87 | ctxSwitchDirect( src, src->last ); |
---|
88 | } |
---|
89 | |
---|
90 | forall(dtype T | coroutine_t(T)) |
---|
91 | void resume(T* cor) { |
---|
92 | coroutine* src = this_coroutine(); // optimization |
---|
93 | coroutine* dst = get_coroutine(vtable(cor)); |
---|
94 | |
---|
95 | if ( src != dst ) { // not resuming self ? |
---|
96 | assertf( dst->notHalted , |
---|
97 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" |
---|
98 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
99 | src->name, src, dst->name, dst ); |
---|
100 | dst->last = src; // set last resumer |
---|
101 | } // if |
---|
102 | ctxSwitchDirect( src, dst ); // always done for performance testing |
---|
103 | } |
---|
104 | |
---|
105 | void ctxSwitchDirect(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 | 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 | |
---|
124 | // used by all constructors |
---|
125 | void create_stack( coStack_t* this, unsigned int storageSize ) { |
---|
126 | //TEMP HACK do this on proper kernel startup |
---|
127 | if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE ); |
---|
128 | |
---|
129 | size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment |
---|
130 | |
---|
131 | if ( (intptr_t)this->storage == 0 ) { |
---|
132 | this->userStack = false; |
---|
133 | this->size = libCeiling( storageSize, 16 ); |
---|
134 | // use malloc/memalign because "new" raises an exception for out-of-memory |
---|
135 | |
---|
136 | // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment |
---|
137 | LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) ); |
---|
138 | LIB_NO_DEBUG_DO( this->storage = malloc( cxtSize + this->size + 8 ) ); |
---|
139 | |
---|
140 | LIB_DEBUG_DO( |
---|
141 | if ( mprotect( this->storage, pageSize, PROT_NONE ) == -1 ) { |
---|
142 | abortf( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) ); |
---|
143 | } // if |
---|
144 | ); |
---|
145 | |
---|
146 | if ( (intptr_t)this->storage == 0 ) { |
---|
147 | abortf( "Attempt to allocate %d bytes of storage for coroutine or task execution-state but insufficient memory available.", this->size ); |
---|
148 | } // if |
---|
149 | |
---|
150 | LIB_DEBUG_DO( this->limit = (char *)this->storage + pageSize ); |
---|
151 | LIB_NO_DEBUG_DO( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment |
---|
152 | |
---|
153 | } else { |
---|
154 | 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() ); |
---|
155 | this->userStack = true; |
---|
156 | this->size = storageSize - cxtSize; |
---|
157 | |
---|
158 | if ( this->size % 16 != 0u ) this->size -= 8; |
---|
159 | |
---|
160 | this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ); // minimum alignment |
---|
161 | } // if |
---|
162 | assertf( this->size >= MinStackSize, "Stack size %d provides less than minimum of %d bytes for a stack.", this->size, MinStackSize ); |
---|
163 | |
---|
164 | this->base = (char *)this->limit + this->size; |
---|
165 | this->context = this->base; |
---|
166 | this->top = (char *)this->context + cxtSize; |
---|
167 | } |
---|
168 | |
---|
169 | // Local Variables: // |
---|
170 | // mode: c // |
---|
171 | // tab-width: 4 // |
---|
172 | // End: // |
---|