- Timestamp:
- Mar 23, 2017, 3:05:36 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- ae6f1ec
- Parents:
- bcda04c
- Location:
- src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Concurrency/Keywords.cc
rbcda04c rbd4d011 17 17 #include "Concurrency/Keywords.h" 18 18 19 #include "InitTweak/InitTweak.h" 19 20 #include "SymTab/AddVisit.h" 20 21 #include "SynTree/Declaration.h" … … 53 54 public: 54 55 55 ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error ) :56 type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ) {}56 ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main ) : 57 type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ) {} 57 58 58 59 virtual ~ConcurrentSueKeyword() {} … … 73 74 const std::string getter_name; 74 75 const std::string context_error; 76 bool needs_main; 75 77 76 78 std::list< Declaration * > declsToAdd, declsToAddAfter; … … 95 97 "__thrd", 96 98 "get_thread", 97 "thread keyword requires threads to be in scope, add #include <thread>" 99 "thread keyword requires threads to be in scope, add #include <thread>", 100 true 98 101 ) 99 102 {} … … 125 128 "__cor", 126 129 "get_coroutine", 127 "coroutine keyword requires coroutines to be in scope, add #include <coroutine>" 130 "coroutine keyword requires coroutines to be in scope, add #include <coroutine>", 131 true 128 132 ) 129 133 {} … … 155 159 "__mon", 156 160 "get_monitor", 157 "monitor keyword requires monitors to be in scope, add #include <monitor>" 161 "monitor keyword requires monitors to be in scope, add #include <monitor>", 162 false 158 163 ) 159 164 {} … … 198 203 }; 199 204 205 //----------------------------------------------------------------------------- 206 //Handles mutex routines definitions : 207 // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) { 208 // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) }; 209 // monitor_guard_t __guard = { __monitors, 2 }; 210 // /*Some code*/ => /*Some code*/ 211 // } } 212 // 213 class ThreadStarter final : public Visitor { 214 public: 215 216 using Visitor::visit; 217 virtual void visit( FunctionDecl * decl ) override final; 218 219 void addStartStatement( FunctionDecl * decl, DeclarationWithType * param ); 220 221 static void implement( std::list< Declaration * > & translationUnit ) { 222 ThreadStarter impl; 223 acceptAll( translationUnit, impl ); 224 } 225 }; 226 200 227 //============================================================================================= 201 228 // General entry routine … … 212 239 213 240 void implementThreadStarter( std::list< Declaration * > & translationUnit ) { 214 241 ThreadStarter ::implement( translationUnit ); 215 242 } 216 243 … … 246 273 forward->get_members().clear(); 247 274 248 FunctionType * type = new FunctionType( noQualifiers, false );275 FunctionType * get_type = new FunctionType( noQualifiers, false ); 249 276 ObjectDecl * this_decl = new ObjectDecl( 250 277 "this", … … 262 289 ); 263 290 264 type->get_parameters().push_back( this_decl );265 type->get_returnVals().push_back(291 get_type->get_parameters().push_back( this_decl ); 292 get_type->get_returnVals().push_back( 266 293 new ObjectDecl( 267 294 "ret", … … 284 311 Type::Static, 285 312 LinkageSpec::Cforall, 286 type,313 get_type, 287 314 nullptr, 288 315 noAttributes, … … 290 317 ); 291 318 319 FunctionDecl * main_decl = nullptr; 320 321 if( needs_main ) { 322 FunctionType * main_type = new FunctionType( noQualifiers, false ); 323 324 main_type->get_parameters().push_back( this_decl->clone() ); 325 326 main_decl = new FunctionDecl( 327 "main", 328 noStorage, 329 LinkageSpec::Cforall, 330 main_type, 331 nullptr 332 ); 333 } 334 292 335 declsToAdd.push_back( forward ); 336 if( needs_main ) declsToAdd.push_back( main_decl ); 293 337 declsToAdd.push_back( get_decl ); 294 338 … … 455 499 body->push_front( new DeclStmt( noLabels, monitors) ); 456 500 } 501 502 //============================================================================================= 503 // General entry routine 504 //============================================================================================= 505 void ThreadStarter::visit(FunctionDecl * decl) { 506 if( ! InitTweak::isConstructor(decl->get_name()) ) return; 507 508 DeclarationWithType * param = decl->get_functionType()->get_parameters().front(); 509 auto ptr = dynamic_cast< PointerType * >( param->get_type() ); 510 // if( ptr ) std::cerr << "FRED1" << std::endl; 511 auto type = dynamic_cast< StructInstType * >( ptr->get_base() ); 512 // if( type ) std::cerr << "FRED2" << std::endl; 513 if( type && type->get_baseStruct()->is_thread() ) { 514 addStartStatement( decl, param ); 515 } 516 } 517 518 void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) { 519 CompoundStmt * stmt = decl->get_statements(); 520 521 if( ! stmt ) return; 522 523 stmt->push_back( 524 new ExprStmt( 525 noLabels, 526 new UntypedExpr( 527 new NameExpr( "__thrd_start" ), 528 { new VariableExpr( param ) } 529 ) 530 ) 531 ); 532 } 457 533 }; -
src/Parser/lex.ll
rbcda04c rbd4d011 256 256 struct { KEYWORD_RETURN(STRUCT); } 257 257 switch { KEYWORD_RETURN(SWITCH); } 258 _Thread { KEYWORD_RETURN(THREAD); } // C11258 thread { KEYWORD_RETURN(THREAD); } // C11 259 259 _Thread_local { KEYWORD_RETURN(THREADLOCAL); } // C11 260 260 throw { KEYWORD_RETURN(THROW); } // CFA -
src/libcfa/concurrency/thread
rbcda04c rbd4d011 56 56 thread_desc * this_thread(void); 57 57 58 forall( dtype T | is_thread(T) ) 59 void __thrd_start( T* this ); 60 58 61 //----------------------------------------------------------------------------- 59 62 // Ctors and dtors -
src/libcfa/concurrency/thread.c
rbcda04c rbd4d011 31 31 32 32 //----------------------------------------------------------------------------- 33 // Forward declarations34 forall( dtype T | is_thread(T) )35 void start( T* this );36 37 //-----------------------------------------------------------------------------38 33 // Thread ctors and dtors 39 34 … … 53 48 void ?{}( scoped(T)* this ) { 54 49 (&this->handle){}; 55 start(&this->handle);50 __thrd_start(&this->handle); 56 51 } 57 52 … … 59 54 void ?{}( scoped(T)* this, P params ) { 60 55 (&this->handle){ params }; 61 start(&this->handle);56 __thrd_start(&this->handle); 62 57 } 63 58 … … 70 65 // Starting and stopping threads 71 66 forall( dtype T | is_thread(T) ) 72 void start( T* this ) {67 void __thrd_start( T* this ) { 73 68 coroutine_desc* thrd_c = get_coroutine(this); 74 69 thread_desc* thrd_h = get_thread (this); -
src/tests/monitor.c
rbcda04c rbd4d011 26 26 } 27 27 28 struct MyThread { thread_desc __thrd; }; 29 30 DECL_THREAD(MyThread); 31 32 void ?{}( MyThread * this ) {} 33 void ^?{}( MyThread * mutex this ) {} 28 thread MyThread {}; 34 29 35 30 void main( MyThread* this ) { … … 43 38 processor p; 44 39 { 45 scoped(MyThread)f[4];40 MyThread f[4]; 46 41 } 47 42 sout | global.value | endl; -
src/tests/thread.c
rbcda04c rbd4d011 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 // thread First; 7 // void main(First* this); 8 8 9 DECL_THREAD(First); 10 DECL_THREAD(Second); 9 // thread Second; 10 // void main(Second* this); 11 12 thread First { signal_once* lock; }; 13 thread Second { signal_once* lock; }; 11 14 12 15 void ?{}( First * this, signal_once* lock ) { this->lock = lock; } 13 16 void ?{}( Second * this, signal_once* lock ) { this->lock = lock; } 14 15 void ^?{}( First * mutex this ) {}16 void ^?{}( Second * mutex this ) {}17 17 18 18 void main(First* this) { … … 39 39 processor p; 40 40 { 41 scoped(First)f = { &lock };42 scoped(Second)s = { &lock };41 First f = { &lock }; 42 Second s = { &lock }; 43 43 } 44 44 }
Note: See TracChangeset
for help on using the changeset viewer.