Changeset 549c006 for src/Concurrency
- Timestamp:
- Sep 29, 2017, 12:18:51 PM (7 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:
- b8116cd
- Parents:
- ea156ae
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Concurrency/Keywords.cc
rea156ae r549c006 196 196 std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* ); 197 197 void validate( DeclarationWithType * ); 198 void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); 198 199 void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); 199 200 … … 206 207 StructDecl* monitor_decl = nullptr; 207 208 StructDecl* guard_decl = nullptr; 209 StructDecl* dtor_guard_decl = nullptr; 208 210 209 211 static std::unique_ptr< Type > generic_func; … … 229 231 230 232 void postvisit( FunctionDecl * decl ); 233 void previsit ( StructDecl * decl ); 231 234 232 235 void addStartStatement( FunctionDecl * decl, DeclarationWithType * param ); … … 236 239 acceptAll( translationUnit, impl ); 237 240 } 241 242 private : 243 bool thread_ctor_seen = false; 244 StructDecl * thread_decl = nullptr; 238 245 }; 239 246 … … 403 410 if( mutexArgs.empty() ) return; 404 411 412 if( CodeGen::isConstructor(decl->name) ) throw SemanticError( "constructors cannot have mutex parameters", decl ); 413 414 bool isDtor = CodeGen::isDestructor( decl->name ); 415 416 if( isDtor && mutexArgs.size() != 1 ) throw SemanticError( "destructors can only have 1 mutex argument", decl ); 417 405 418 for(auto arg : mutexArgs) { 406 419 validate( arg ); … … 412 425 if( !monitor_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl ); 413 426 if( !guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl ); 414 415 addStatments( decl, body, mutexArgs ); 427 if( !dtor_guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl ); 428 429 if( isDtor ) { 430 addDtorStatments( decl, body, mutexArgs ); 431 } 432 else { 433 addStatments( decl, body, mutexArgs ); 434 } 416 435 } 417 436 … … 425 444 assert( !guard_decl ); 426 445 guard_decl = decl; 446 } 447 else if( decl->name == "monitor_dtor_guard_t" ) { 448 assert( !dtor_guard_decl ); 449 dtor_guard_decl = decl; 427 450 } 428 451 } … … 457 480 //Make sure that typed isn't mutex 458 481 if( base->get_mutex() ) throw SemanticError( "mutex keyword may only appear once per argument ", arg ); 482 } 483 484 void MutexKeyword::addDtorStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) { 485 Type * arg_type = args.front()->get_type()->clone(); 486 arg_type->set_mutex( false ); 487 488 ObjectDecl * monitors = new ObjectDecl( 489 "__monitor", 490 noStorage, 491 LinkageSpec::Cforall, 492 nullptr, 493 new PointerType( 494 noQualifiers, 495 new StructInstType( 496 noQualifiers, 497 monitor_decl 498 ) 499 ), 500 new SingleInit( new UntypedExpr( 501 new NameExpr( "get_monitor" ), 502 { new CastExpr( new VariableExpr( args.front() ), arg_type ) } 503 )) 504 ); 505 506 assert(generic_func); 507 508 //in reverse order : 509 // monitor_guard_t __guard = { __monitors, #, func }; 510 body->push_front( 511 new DeclStmt( noLabels, new ObjectDecl( 512 "__guard", 513 noStorage, 514 LinkageSpec::Cforall, 515 nullptr, 516 new StructInstType( 517 noQualifiers, 518 dtor_guard_decl 519 ), 520 new ListInit( 521 { 522 new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ), 523 new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) ) 524 }, 525 noDesignators, 526 true 527 ) 528 )) 529 ); 530 531 //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) }; 532 body->push_front( new DeclStmt( noLabels, monitors) ); 459 533 } 460 534 … … 523 597 // General entry routine 524 598 //============================================================================================= 599 void ThreadStarter::previsit( StructDecl * decl ) { 600 if( decl->name == "thread_desc" && decl->body ) { 601 assert( !thread_decl ); 602 thread_decl = decl; 603 } 604 } 605 525 606 void ThreadStarter::postvisit(FunctionDecl * decl) { 526 607 if( ! CodeGen::isConstructor(decl->name) ) return; 608 609 Type * typeof_this = InitTweak::getTypeofThis(decl->type); 610 StructInstType * ctored_type = dynamic_cast< StructInstType * >( typeof_this ); 611 if( ctored_type && ctored_type->baseStruct == thread_decl ) { 612 thread_ctor_seen = true; 613 } 527 614 528 615 DeclarationWithType * param = decl->get_functionType()->get_parameters().front(); 529 616 auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) ); 530 617 if( type && type->get_baseStruct()->is_thread() ) { 618 if( !thread_decl || !thread_ctor_seen ) { 619 throw SemanticError("thread keyword requires threads to be in scope, add #include <thread>"); 620 } 621 531 622 addStartStatement( decl, param ); 532 623 }
Note: See TracChangeset
for help on using the changeset viewer.