Ignore:
Timestamp:
Jun 27, 2018, 3:28:41 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env, with_gc
Children:
b21c77a
Parents:
0182bfa (diff), 63238a4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into with_gc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    r0182bfa r28f3a19  
    192192                void postvisit(   StructDecl * decl );
    193193
    194                 std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* );
     194                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
    195195                void validate( DeclarationWithType * );
    196196                void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
     
    431431        void MutexKeyword::postvisit(FunctionDecl* decl) {
    432432
    433                 std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl );
    434                 if( mutexArgs.empty() ) return;
    435 
    436                 if( CodeGen::isConstructor(decl->name) ) SemanticError( decl, "constructors cannot have mutex parameters" );
    437 
     433                bool first = false;
     434                std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
    438435                bool isDtor = CodeGen::isDestructor( decl->name );
    439436
     437                // Is this function relevant to monitors
     438                if( mutexArgs.empty() ) {
     439                        // If this is the destructor for a monitor it must be mutex
     440                        if(isDtor) {
     441                                Type* ty = decl->get_functionType()->get_parameters().front()->get_type();
     442
     443                                // If it's a copy, it's not a mutex
     444                                ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
     445                                if( ! rty ) return;
     446
     447                                // If we are not pointing directly to a type, it's not a mutex
     448                                Type* base = rty->get_base();
     449                                if( dynamic_cast< ReferenceType * >( base ) ) return;
     450                                if( dynamic_cast< PointerType * >( base ) ) return;
     451
     452                                // Check if its a struct
     453                                StructInstType * baseStruct = dynamic_cast< StructInstType * >( base );
     454                                if( !baseStruct ) return;
     455
     456                                // Check if its a monitor
     457                                if(baseStruct->baseStruct->is_monitor() || baseStruct->baseStruct->is_thread())
     458                                        SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters\n" );
     459                        }
     460                        return;
     461                }
     462
     463                // Monitors can't be constructed with mutual exclusion
     464                if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" );
     465
     466                // It makes no sense to have multiple mutex parameters for the destructor
    440467                if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" );
    441468
     469                // Make sure all the mutex arguments are monitors
    442470                for(auto arg : mutexArgs) {
    443471                        validate( arg );
    444472                }
    445473
     474                // Check if we need to instrument the body
    446475                CompoundStmt* body = decl->get_statements();
    447476                if( ! body ) return;
    448477
     478                // Do we have the required headers
    449479                if( !monitor_decl || !guard_decl || !dtor_guard_decl )
    450                         SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>" );
    451 
     480                        SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>\n" );
     481
     482                // Instrument the body
    452483                if( isDtor ) {
    453484                        addDtorStatments( decl, body, mutexArgs );
     
    474505        }
    475506
    476         std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl ) {
     507        std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) {
    477508                std::list<DeclarationWithType*> mutexArgs;
    478509
     510                bool once = true;
    479511                for( auto arg : decl->get_functionType()->get_parameters()) {
    480512                        //Find mutex arguments
    481513                        Type* ty = arg->get_type();
    482514                        if( ! ty->get_mutex() ) continue;
     515
     516                        if(once) {first = true;}
     517                        once = false;
    483518
    484519                        //Append it to the list
Note: See TracChangeset for help on using the changeset viewer.