Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    r549c006 ra843067  
    196196                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* );
    197197                void validate( DeclarationWithType * );
    198                 void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
    199198                void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
    200199
     
    207206                StructDecl* monitor_decl = nullptr;
    208207                StructDecl* guard_decl = nullptr;
    209                 StructDecl* dtor_guard_decl = nullptr;
    210208
    211209                static std::unique_ptr< Type > generic_func;
     
    231229
    232230                void postvisit( FunctionDecl * decl );
    233                 void previsit ( StructDecl   * decl );
    234231
    235232                void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
     
    239236                        acceptAll( translationUnit, impl );
    240237                }
    241 
    242           private :
    243                 bool thread_ctor_seen = false;
    244                 StructDecl * thread_decl = nullptr;
    245238        };
    246239
     
    410403                if( mutexArgs.empty() ) return;
    411404
    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 
    418405                for(auto arg : mutexArgs) {
    419406                        validate( arg );
     
    425412                if( !monitor_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
    426413                if( !guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
    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                 }
     414
     415                addStatments( decl, body, mutexArgs );
    435416        }
    436417
     
    444425                        assert( !guard_decl );
    445426                        guard_decl = decl;
    446                 }
    447                 else if( decl->name == "monitor_dtor_guard_t" ) {
    448                         assert( !dtor_guard_decl );
    449                         dtor_guard_decl = decl;
    450427                }
    451428        }
     
    480457                //Make sure that typed isn't mutex
    481458                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) );
    533459        }
    534460
     
    597523        // General entry routine
    598524        //=============================================================================================
    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 
    606525        void ThreadStarter::postvisit(FunctionDecl * decl) {
    607526                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                 }
    614527
    615528                DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
    616529                auto type  = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
    617530                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 
    622531                        addStartStatement( decl, param );
    623532                }
Note: See TracChangeset for help on using the changeset viewer.