Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    r5b7c8b5 r6cebfef  
    9393                ObjectDecl * addField( StructDecl * );
    9494                void addRoutines( ObjectDecl *, FunctionDecl * );
    95                 void addLockUnlockRoutines( StructDecl * );
    9695
    9796                virtual bool is_target( StructDecl * decl ) = 0;
     
    323322                StructDecl* dtor_guard_decl = nullptr;
    324323                StructDecl* thread_guard_decl = nullptr;
    325                 StructDecl* lock_guard_decl = nullptr;
    326324
    327325                static std::unique_ptr< Type > generic_func;
     
    465463        }
    466464
     465
    467466        void ConcurrentSueKeyword::handle( StructDecl * decl ) {
    468467                if( ! decl->body ) return;
     
    480479                FunctionDecl * func = forwardDeclare( decl );
    481480                ObjectDecl * field = addField( decl );
    482 
    483                 // add get_.* routine
    484481                addRoutines( field, func );
    485                 // add lock/unlock routines to monitors for use by mutex stmt
    486                 addLockUnlockRoutines( decl );
    487482        }
    488483
     
    617612        }
    618613
    619         // This function adds the get_.* routine body for coroutines, monitors etc
    620         //              after their corresponding struct has been made
    621614        void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
    622615                CompoundStmt * statement = new CompoundStmt();
     
    641634
    642635                declsToAddAfter.push_back( get_decl );
    643         }
    644 
    645         // Generates lock/unlock routines for monitors to be used by mutex stmts
    646         void ConcurrentSueKeyword::addLockUnlockRoutines( StructDecl * decl ) {
    647                 // this routine will be called for all ConcurrentSueKeyword children so only continue if we are a monitor
    648                 if ( !decl->is_monitor() ) return;
    649 
    650                 FunctionType * lock_fn_type = new FunctionType( noQualifiers, false );
    651                 FunctionType * unlock_fn_type = new FunctionType( noQualifiers, false );
    652 
    653                 // create this ptr parameter for both routines
    654                 ObjectDecl * this_decl = new ObjectDecl(
    655                         "this",
    656                         noStorageClasses,
    657                         LinkageSpec::Cforall,
    658                         nullptr,
    659                         new ReferenceType(
    660                                 noQualifiers,
    661                                 new StructInstType(
    662                                         noQualifiers,
    663                                         decl
    664                                 )
    665                         ),
    666                         nullptr
    667                 );
    668 
    669                 lock_fn_type->get_parameters().push_back( this_decl->clone() );
    670                 unlock_fn_type->get_parameters().push_back( this_decl->clone() );
    671                 fixupGenerics(lock_fn_type, decl);
    672                 fixupGenerics(unlock_fn_type, decl);
    673 
    674                 delete this_decl;
    675 
    676 
    677                 //////////////////////////////////////////////////////////////////////
    678                 // The following generates this lock routine for all monitors
    679                 /*
    680                         void lock (monitor_t & this) {
    681                                 lock(get_monitor(this));
    682                         }       
    683                 */
    684                 FunctionDecl * lock_decl = new FunctionDecl(
    685                         "lock",
    686                         Type::Static,
    687                         LinkageSpec::Cforall,
    688                         lock_fn_type,
    689                         nullptr,
    690                         { },
    691                         Type::Inline
    692                 );
    693 
    694                 UntypedExpr * get_monitor_lock =  new UntypedExpr (
    695                         new NameExpr( "get_monitor" ),
    696                         { new VariableExpr( lock_fn_type->get_parameters().front() ) }
    697                 );
    698 
    699                 CompoundStmt * lock_statement = new CompoundStmt();
    700                 lock_statement->push_back(
    701                         new ExprStmt(
    702                                 new UntypedExpr (
    703                                         new NameExpr( "lock" ),
    704                                         {
    705                                                 get_monitor_lock
    706                                         }
    707                                 )
    708                         )
    709                 );
    710                 lock_decl->set_statements( lock_statement );
    711 
    712                 //////////////////////////////////////////////////////////////////
    713                 // The following generates this routine for all monitors
    714                 /*
    715                         void unlock (monitor_t & this) {
    716                                 unlock(get_monitor(this));
    717                         }       
    718                 */
    719                 FunctionDecl * unlock_decl = new FunctionDecl(
    720                         "unlock",
    721                         Type::Static,
    722                         LinkageSpec::Cforall,
    723                         unlock_fn_type,
    724                         nullptr,
    725                         { },
    726                         Type::Inline
    727                 );
    728 
    729                 CompoundStmt * unlock_statement = new CompoundStmt();
    730 
    731                 UntypedExpr * get_monitor_unlock =  new UntypedExpr (
    732                         new NameExpr( "get_monitor" ),
    733                         { new VariableExpr( unlock_fn_type->get_parameters().front() ) }
    734                 );
    735 
    736                 unlock_statement->push_back(
    737                         new ExprStmt(
    738                                 new UntypedExpr(
    739                                         new NameExpr( "unlock" ),
    740                                         {
    741                                                 get_monitor_unlock
    742                                         }
    743                                 )
    744                         )
    745                 );
    746                 unlock_decl->set_statements( unlock_statement );
    747                
    748                 // pushes routines to declsToAddAfter to add at a later time
    749                 declsToAddAfter.push_back( lock_decl );
    750                 declsToAddAfter.push_back( unlock_decl );
    751636        }
    752637
     
    1052937                        assert( !thread_guard_decl );
    1053938                        thread_guard_decl = decl;
    1054                 }
    1055                 else if ( decl->name == "__mutex_stmt_lock_guard" && decl->body ) {
    1056                         assert( !lock_guard_decl );
    1057                         lock_guard_decl = decl;
    1058939                }
    1059940        }
     
    12001081                                new PointerType(
    12011082                                        noQualifiers,
    1202                                         new TypeofType( noQualifiers, args.front()->clone() )
     1083                                        new StructInstType(
     1084                                                noQualifiers,
     1085                                                monitor_decl
     1086                                        )
    12031087                                ),
    12041088                                new ConstantExpr( Constant::from_ulong( args.size() ) ),
     
    12091093                                map_range < std::list<Initializer*> > ( args, [](Expression * var ){
    12101094                                        return new SingleInit( new UntypedExpr(
    1211                                                 new NameExpr( "__get_pointer" ),
     1095                                                new NameExpr( "get_monitor" ),
    12121096                                                { var }
    12131097                                        ) );
     
    12151099                        )
    12161100                );
    1217 
    1218                 StructInstType * lock_guard_struct = new StructInstType( noQualifiers, lock_guard_decl );
    1219                 TypeExpr * lock_type_expr = new TypeExpr( new TypeofType( noQualifiers, args.front()->clone() ) );
    1220 
    1221                 lock_guard_struct->parameters.push_back( lock_type_expr ) ;
    12221101
    12231102                // in reverse order :
     
    12291108                                LinkageSpec::Cforall,
    12301109                                nullptr,
    1231                                 lock_guard_struct,
     1110                                new StructInstType(
     1111                                        noQualifiers,
     1112                                        guard_decl
     1113                                ),
    12321114                                new ListInit(
    12331115                                        {
Note: See TracChangeset for help on using the changeset viewer.