Changes in / [31ce3d6:6e8f1df]
- Files:
-
- 20 edited
-
doc/proposals/concurrency/thePlan.md (modified) (1 diff)
-
src/Concurrency/Keywords.cc (modified) (11 diffs)
-
src/benchmark/CorCtxSwitch.c (modified) (1 diff)
-
src/benchmark/bench.c (modified) (4 diffs)
-
src/benchmark/csv-data.c (modified) (1 diff)
-
src/examples/multicore.c (modified) (1 diff)
-
src/libcfa/concurrency/coroutine (modified) (1 diff)
-
src/libcfa/concurrency/invoke.c (modified) (4 diffs)
-
src/libcfa/concurrency/invoke.h (modified) (3 diffs)
-
src/libcfa/concurrency/kernel.c (modified) (6 diffs)
-
src/libcfa/concurrency/kernel_private.h (modified) (1 diff)
-
src/libcfa/concurrency/monitor (modified) (2 diffs)
-
src/libcfa/concurrency/monitor.c (modified) (2 diffs)
-
src/libcfa/concurrency/thread (modified) (2 diffs)
-
src/libcfa/concurrency/thread.c (modified) (4 diffs)
-
src/main.cc (modified) (1 diff)
-
src/tests/coroutine.c (modified) (3 diffs)
-
src/tests/monitor.c (modified) (1 diff)
-
src/tests/multi-monitor.c (modified) (1 diff)
-
src/tests/thread.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/concurrency/thePlan.md
r31ce3d6 r6e8f1df 8 8 _Phase 2_ : Minimum Viable Product 9 9 done - Monitor type and enter/leave mutex member routines 10 done - Multi monitors calls, 11 done - Monitors as a language feature (not calling enter/leave by hand) 10 Monitors as a language feature (not calling enter/leave by hand) 12 11 Internal scheduling 13 12 14 13 _Phase 3_ : Kernel features 15 Preemption16 14 Detach thread 17 15 Cluster migration 16 Preemption 18 17 19 18 _Phase 4_ : Monitor features 19 Multi monitors calls, 20 20 External scheduling 21 21 -
src/Concurrency/Keywords.cc
r31ce3d6 r6e8f1df 17 17 #include "Concurrency/Keywords.h" 18 18 19 #include "SymTab/AddVisit.h"20 19 #include "SynTree/Declaration.h" 21 20 #include "SynTree/Expression.h" … … 30 29 namespace { 31 30 const std::list<Label> noLabels; 32 const std::list< Attribute * > noAttributes;33 31 Type::StorageClasses noStorage; 34 32 Type::Qualifiers noQualifiers; … … 65 63 // void main( MyCoroutine * this ); 66 64 // 67 class CoroutineKeyword final : public Visitor { 68 template< typename Visitor > 69 friend void SymTab::acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor ); 70 public: 71 72 using Visitor::visit; 73 virtual void visit( StructDecl * decl ) override final; 74 75 void handle( StructDecl * ); 76 Declaration * addField( StructDecl * ); 77 void addRoutines( StructDecl *, Declaration * ); 78 79 static void implement( std::list< Declaration * > & translationUnit ) { 80 CoroutineKeyword impl; 81 SymTab::acceptAndAdd( translationUnit, impl ); 82 } 83 84 private: 85 std::list< Declaration * > declsToAdd, declsToAddAfter; 86 StructDecl* coroutine_decl = nullptr; 65 class CoroutineKeyword final : public Mutator { 66 public: 67 68 static void implement( std::list< Declaration * > & translationUnit ) {} 87 69 }; 88 70 … … 115 97 116 98 using Visitor::visit; 117 virtual void visit( FunctionDecl * decl ) override final;118 virtual void visit( StructDecl * decl ) override final;99 virtual void visit( FunctionDecl *functionDecl ) override final; 100 virtual void visit( StructDecl *functionDecl ) override final; 119 101 120 102 std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* ); … … 129 111 private: 130 112 StructDecl* monitor_decl = nullptr; 131 StructDecl* guard_decl = nullptr;132 113 }; 133 114 … … 143 124 144 125 //============================================================================================= 145 // Coroutine keyword implementation146 //=============================================================================================147 void CoroutineKeyword::visit(StructDecl * decl) {148 if( decl->get_name() == "coroutine_desc" ) {149 assert( !coroutine_decl );150 coroutine_decl = decl;151 }152 else if ( false ) {153 handle( decl );154 }155 156 }157 158 void CoroutineKeyword::handle( StructDecl * decl ) {159 if( ! decl->has_body() ) return;160 161 if( !coroutine_decl ) throw SemanticError( "coroutine keyword requires coroutines to be in scope, add #include <coroutine>", decl );162 163 Declaration * field = addField( decl );164 addRoutines( decl, field );165 }166 167 Declaration * CoroutineKeyword::addField( StructDecl * decl ) {168 Declaration * cor = new ObjectDecl(169 "__cor",170 noStorage,171 LinkageSpec::Cforall,172 nullptr,173 new StructInstType(174 noQualifiers,175 coroutine_decl176 ),177 nullptr178 );179 180 decl->get_members().push_front( cor );181 182 return cor;183 }184 185 void CoroutineKeyword::addRoutines( StructDecl * decl, Declaration * field ) {186 FunctionType * type = new FunctionType( noQualifiers, false );187 type->get_parameters().push_back(188 new ObjectDecl(189 "this",190 noStorage,191 LinkageSpec::Cforall,192 nullptr,193 new PointerType(194 noQualifiers,195 new StructInstType(196 noQualifiers,197 decl198 )199 ),200 nullptr201 )202 );203 204 CompoundStmt * statement = new CompoundStmt( noLabels );205 statement->push_back(206 new ReturnStmt(207 noLabels,208 new UntypedMemberExpr(209 new NameExpr( "__cor" ),210 new NameExpr( "this" )211 )212 )213 );214 215 declsToAddAfter.push_back(216 new FunctionDecl(217 "get_coroutine",218 Type::Static,219 LinkageSpec::Cforall,220 type,221 statement,222 noAttributes,223 Type::Inline224 )225 );226 }227 228 229 //=============================================================================================230 126 // Mutex keyword implementation 231 127 //============================================================================================= … … 241 137 if( ! body ) return; 242 138 243 if( !monitor_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl ); 244 if( !guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl ); 245 139 assert(monitor_decl); 246 140 addStatments( body, mutexArgs ); 247 141 } … … 252 146 monitor_decl = decl; 253 147 } 254 else if( decl->get_name() == "monitor_guard_t" ) {255 assert( !guard_decl );256 guard_decl = decl;257 }258 148 } 259 149 … … 289 179 290 180 void MutexKeyword::addStatments( CompoundStmt * body, const std::list<DeclarationWithType * > & args ) { 181 291 182 ObjectDecl * monitors = new ObjectDecl( 292 183 "__monitors", … … 327 218 new StructInstType( 328 219 noQualifiers, 329 guard_decl220 "monitor_guard_t" 330 221 ), 331 222 new ListInit( … … 333 224 new SingleInit( new VariableExpr( monitors ) ), 334 225 new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ) 335 }, 336 noDesignators, 337 true 226 } 338 227 ) 339 228 )) 340 229 ); 341 230 342 //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b)};231 //monitor_desc * __monitors[] = { a, b }; 343 232 body->push_front( new DeclStmt( noLabels, monitors) ); 344 233 } -
src/benchmark/CorCtxSwitch.c
r31ce3d6 r6e8f1df 24 24 25 25 struct GreatSuspender { 26 coroutine_desc __cor;26 coroutine_desc c; 27 27 }; 28 28 -
src/benchmark/bench.c
r31ce3d6 r6e8f1df 86 86 //======================================= 87 87 88 struct CoroutineDummy { coroutine_desc __cor; };88 struct CoroutineDummy { coroutine_desc c; }; 89 89 DECL_COROUTINE(CoroutineDummy); 90 90 void main(CoroutineDummy * this) {} … … 119 119 struct CoroutineResume { 120 120 int N; 121 coroutine_desc __cor;121 coroutine_desc c; 122 122 }; 123 123 … … 150 150 //======================================= 151 151 152 struct ThreadDummy { thread_desc __thrd; };152 struct ThreadDummy { thread_desc t; }; 153 153 DECL_THREAD(ThreadDummy); 154 154 void main(ThreadDummy * this) {} … … 180 180 int N; 181 181 long long result; 182 thread_desc __thrd;182 thread_desc t; 183 183 }; 184 184 -
src/benchmark/csv-data.c
r31ce3d6 r6e8f1df 26 26 27 27 struct GreatSuspender { 28 coroutine_desc __cor;28 coroutine_desc c; 29 29 }; 30 30 -
src/examples/multicore.c
r31ce3d6 r6e8f1df 2 2 #include <thread> 3 3 4 struct MyThread { thread_desc __thrd; };4 struct MyThread { thread_desc t; }; 5 5 6 6 DECL_THREAD(MyThread); -
src/libcfa/concurrency/coroutine
r31ce3d6 r6e8f1df 30 30 }; 31 31 32 #define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X* this) { return &this-> __cor; } void main(X* this)32 #define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X* this) { return &this->c; } void main(X* this) 33 33 34 34 //----------------------------------------------------------------------------- -
src/libcfa/concurrency/invoke.c
r31ce3d6 r6e8f1df 56 56 57 57 void CtxInvokeThread( 58 void (*dtor)(void *),59 58 void (*main)(void *), 60 59 struct thread_desc *(*get_thread)(void *), … … 64 63 65 64 struct thread_desc* thrd = get_thread( this ); 66 struct coroutine_desc* cor = &thrd->c or;65 struct coroutine_desc* cor = &thrd->c; 67 66 cor->state = Active; 68 67 … … 92 91 struct FakeStack { 93 92 void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) 94 uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls)95 uint16_t fcw;// X97 FPU control word (preserved across function calls)96 void *rturn; // where to go on return from uSwitch93 uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) 94 uint16_t fcw; // X97 FPU control word (preserved across function calls) 95 void *rturn; // where to go on return from uSwitch 97 96 void *dummyReturn; // fake return compiler would have pushed on call to uInvoke 98 97 void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here … … 106 105 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke 107 106 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke; 108 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520109 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7110 107 111 108 #elif defined( __x86_64__ ) 112 109 113 110 struct FakeStack { 114 void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15115 uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls)116 uint16_t fcw; // X97 FPU control word (preserved across function calls)117 void *rturn; // where to go on return from uSwitch118 void *dummyReturn; // NULL return address to provide proper alignment111 void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 112 uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) 113 uint16_t fcw; // X97 FPU control word (preserved across function calls) 114 void *rturn; // where to go on return from uSwitch 115 void *dummyReturn; // NULL return address to provide proper alignment 119 116 }; 120 117 -
src/libcfa/concurrency/invoke.h
r31ce3d6 r6e8f1df 28 28 #define unlikely(x) __builtin_expect(!!(x), 0) 29 29 #define thread_local _Thread_local 30 #define SCHEDULER_CAPACITY 10 30 31 31 32 struct spinlock { … … 59 60 60 61 struct coStack_t { 61 unsigned int size; // size of stack62 void *storage; // pointer to stack63 void *limit; // stack grows towards stack limit64 void *base; // base of stack65 void *context; // address of cfa_context_t66 void *top; // address of top of storage67 bool userStack; // whether or not the user allocated the stack62 unsigned int size; // size of stack 63 void *storage; // pointer to stack 64 void *limit; // stack grows towards stack limit 65 void *base; // base of stack 66 void *context; // address of cfa_context_t 67 void *top; // address of top of storage 68 bool userStack; 68 69 }; 69 70 … … 71 72 72 73 struct coroutine_desc { 73 struct coStack_t stack; // stack information of the coroutine74 const char *name; // textual name for coroutine/task, initialized by uC++ generated code75 int errno_; // copy of global UNIX variable errno76 enum coroutine_state state; // current execution status for coroutine77 struct coroutine_desc *starter; // first coroutine to resume this one78 struct coroutine_desc *last; // last coroutine to resume this one74 struct coStack_t stack; 75 const char *name; // textual name for coroutine/task, initialized by uC++ generated code 76 int errno_; // copy of global UNIX variable errno 77 enum coroutine_state state; // current execution status for coroutine 78 struct coroutine_desc *starter; // first coroutine to resume this one 79 struct coroutine_desc *last; // last coroutine to resume this one 79 80 }; 80 81 81 82 struct thread_desc { 82 struct coroutine_desc c or;// coroutine body used to store context83 struct coroutine_desc c; // coroutine body used to store context 83 84 struct signal_once terminated; // indicate if execuation state is not halted 84 struct thread_desc * next; // instrusive link field for threads85 struct thread_desc * next; // instrusive link field for threads 85 86 }; 86 87 -
src/libcfa/concurrency/kernel.c
r31ce3d6 r6e8f1df 107 107 108 108 void ?{}( thread_desc * this, current_stack_info_t * info) { 109 (&this->c or){ info };109 (&this->c){ info }; 110 110 } 111 111 … … 113 113 // Processor coroutine 114 114 void ?{}(processorCtx_t * this, processor * proc) { 115 (&this-> __cor){};115 (&this->c){}; 116 116 this->proc = proc; 117 117 proc->runner = this; … … 119 119 120 120 void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) { 121 (&this-> __cor){ info };121 (&this->c){ info }; 122 122 this->proc = proc; 123 123 proc->runner = this; … … 255 255 processorCtx_t proc_cor_storage = { proc, &info }; 256 256 257 LIB_DEBUG_PRINTF("Coroutine : created stack %p\n", proc_cor_storage. __cor.stack.base);257 LIB_DEBUG_PRINTF("Coroutine : created stack %p\n", proc_cor_storage.c.stack.base); 258 258 259 259 //Set global state 260 proc->current_coroutine = &proc->runner-> __cor;260 proc->current_coroutine = &proc->runner->c; 261 261 proc->current_thread = NULL; 262 262 … … 268 268 // back to here. Instead directly call the main since we already are on the 269 269 // appropriate stack. 270 proc_cor_storage. __cor.state = Active;270 proc_cor_storage.c.state = Active; 271 271 main( &proc_cor_storage ); 272 proc_cor_storage. __cor.state = Halted;272 proc_cor_storage.c.state = Halted; 273 273 274 274 // Main routine of the core returned, the core is now fully terminated … … 359 359 this_processor = systemProcessor; 360 360 this_processor->current_thread = mainThread; 361 this_processor->current_coroutine = &mainThread->c or;361 this_processor->current_coroutine = &mainThread->c; 362 362 363 363 // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX -
src/libcfa/concurrency/kernel_private.h
r31ce3d6 r6e8f1df 35 35 struct processorCtx_t { 36 36 processor * proc; 37 coroutine_desc __cor;37 coroutine_desc c; 38 38 }; 39 39 -
src/libcfa/concurrency/monitor
r31ce3d6 r6e8f1df 34 34 } 35 35 36 //Basic entering routine 37 void enter(monitor_desc *); 38 void leave(monitor_desc *); 39 36 40 //Array entering routine 37 41 void enter(monitor_desc **, int count); … … 45 49 static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) { 46 50 return ((intptr_t)lhs) < ((intptr_t)rhs); 51 } 52 53 static inline void ?{}( monitor_guard_t * this, monitor_desc ** m ) { 54 this->m = m; 55 this->count = 1; 56 enter( *this->m ); 47 57 } 48 58 -
src/libcfa/concurrency/monitor.c
r31ce3d6 r6e8f1df 74 74 void enter(monitor_desc ** monitors, int count) { 75 75 for(int i = 0; i < count; i++) { 76 // printf("%d\n", i); 76 77 enter( monitors[i] ); 77 78 } … … 80 81 void leave(monitor_desc ** monitors, int count) { 81 82 for(int i = count - 1; i >= 0; i--) { 83 // printf("%d\n", i); 82 84 leave( monitors[i] ); 83 85 } -
src/libcfa/concurrency/thread
r31ce3d6 r6e8f1df 28 28 // Anything that is resumed is a coroutine. 29 29 trait is_thread(dtype T) { 30 void ^?{}(T* this);31 30 void main(T* this); 32 31 thread_desc* get_thread(T* this); 33 32 }; 34 33 35 #define DECL_THREAD(X) thread_desc* get_thread(X* this) { return &this-> __thrd; } void main(X* this)34 #define DECL_THREAD(X) thread_desc* get_thread(X* this) { return &this->t; } void main(X* this) 36 35 37 36 forall( dtype T | is_thread(T) ) 38 37 static inline coroutine_desc* get_coroutine(T* this) { 39 return &get_thread(this)->c or;38 return &get_thread(this)->c; 40 39 } 41 40 42 41 static inline coroutine_desc* get_coroutine(thread_desc* this) { 43 return &this->c or;42 return &this->c; 44 43 } 45 44 … … 65 64 void ?{}( scoped(T)* this, P params ); 66 65 67 forall( dtype T | sized(T) | is_thread(T) )66 forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } ) 68 67 void ^?{}( scoped(T)* this ); 69 68 -
src/libcfa/concurrency/thread.c
r31ce3d6 r6e8f1df 42 42 43 43 void ?{}(thread_desc* this) { 44 (&this->c or){};45 this->c or.name = "Anonymous Coroutine";44 (&this->c){}; 45 this->c.name = "Anonymous Coroutine"; 46 46 (&this->terminated){}; 47 47 this->next = NULL; … … 49 49 50 50 void ^?{}(thread_desc* this) { 51 ^(&this->c or){};51 ^(&this->c){}; 52 52 } 53 53 … … 64 64 } 65 65 66 forall( dtype T | sized(T) | is_thread(T) )66 forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } ) 67 67 void ^?{}( scoped(T)* this ) { 68 68 stop(&this->handle); … … 120 120 extern "C" { 121 121 void __thread_signal_termination( thread_desc * this ) { 122 this->c or.state = Halted;122 this->c.state = Halted; 123 123 LIB_DEBUG_PRINTF("Thread end : %p\n", this); 124 124 signal( &this->terminated ); -
src/main.cc
r31ce3d6 r6e8f1df 241 241 OPTPRINT( "fixNames" ) 242 242 CodeGen::fixNames( translationUnit ); 243 OPTPRINT( " genInit" )243 OPTPRINT( "tweakInit" ) 244 244 InitTweak::genInit( translationUnit ); 245 245 OPTPRINT( "expandMemberTuples" ); -
src/tests/coroutine.c
r31ce3d6 r6e8f1df 4 4 struct Fibonacci { 5 5 int fn; // used for communication 6 coroutine_desc __cor;6 coroutine_desc c; 7 7 }; 8 8 … … 12 12 13 13 coroutine_desc* get_coroutine(Fibonacci* this) { 14 return &this-> __cor;14 return &this->c; 15 15 } 16 16 … … 18 18 #ifdef MORE_DEBUG 19 19 sout | "Starting main of coroutine " | this | endl; 20 sout | "Started from " | this-> __cor.last | endl;20 sout | "Started from " | this->c.last | endl; 21 21 #endif 22 22 int fn1, fn2; // retained between resumes -
src/tests/monitor.c
r31ce3d6 r6e8f1df 13 13 } 14 14 15 monitor_desc * get_monitor( global_t * this ) { 16 return &this->m; 15 static global_t global; 16 17 void increment( /*mutex*/ global_t * this ) { 18 monitor_desc * mon = &this->m; 19 monitor_guard_t g1 = { &mon }; 20 { 21 monitor_guard_t g2 = { &mon }; 22 { 23 monitor_guard_t g3 = { &mon }; 24 this->value += 1; 25 } 26 } 17 27 } 18 28 19 static global_t global; 20 21 void increment3( global_t * mutex this ) { 22 this->value += 1; 23 } 24 25 void increment2( global_t * mutex this ) { 26 increment3( this ); 27 } 28 29 void increment( global_t * mutex this ) { 30 increment2( this ); 31 } 32 33 struct MyThread { thread_desc __thrd; }; 29 struct MyThread { thread_desc t; }; 34 30 35 31 DECL_THREAD(MyThread); -
src/tests/multi-monitor.c
r31ce3d6 r6e8f1df 6 6 static int global12, global23, global13; 7 7 8 struct monitor_t { 9 monitor_desc m; 10 }; 8 static monitor_desc m1, m2, m3; 11 9 12 monitor_desc * get_monitor( monitor_t * this ) { 13 return &this->m; 14 } 15 16 static monitor_t m1, m2, m3; 17 18 void increment( monitor_t * mutex p1, monitor_t * mutex p2, int * value ) { 10 void increment( /*mutex*/ monitor_desc * p1, /*mutex*/ monitor_desc * p2, int * value ) { 11 monitor_desc * mons[] = { p1, p2 }; 12 monitor_guard_t g = { mons, 2 }; 19 13 *value += 1; 20 14 } 21 15 22 16 struct MyThread { 23 thread_desc __thrd;17 thread_desc t; 24 18 int target; 25 19 }; -
src/tests/thread.c
r31ce3d6 r6e8f1df 4 4 #include <thread> 5 5 6 struct First { thread_desc __thrd; signal_once* lock; };7 struct Second { thread_desc __thrd; signal_once* lock; };6 struct First { thread_desc t; signal_once* lock; }; 7 struct Second { thread_desc t; signal_once* lock; }; 8 8 9 9 DECL_THREAD(First);
Note:
See TracChangeset
for help on using the changeset viewer.