Changeset ab8c6a6 for src


Ignore:
Timestamp:
Oct 26, 2020, 12:17:28 PM (4 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
342be43
Parents:
912cc7d7
Message:

Thread Cancellation, a test for it and a required fix to Specialization.

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    r912cc7d7 rab8c6a6  
    4646        }
    4747
     48        // Only detects threads constructed with the keyword thread.
     49        inline static bool isThread( DeclarationWithType * decl ) {
     50                Type * baseType = decl->get_type()->stripDeclarator();
     51                StructInstType * instType = dynamic_cast<StructInstType *>( baseType );
     52                if ( nullptr == instType ) { return false; }
     53                return instType->baseStruct->is_thread();
     54        }
     55
    4856        //=============================================================================================
    4957        // Pass declarations
     
    119127                        "get_thread",
    120128                        "thread keyword requires threads to be in scope, add #include <thread.hfa>\n",
    121                         "",
     129                        "ThreadCancelled",
    122130                        true,
    123131                        AggregateDecl::Thread
     
    290298                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
    291299                void validate( DeclarationWithType * );
    292                 void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
    293                 void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
     300                void addDtorStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
     301                void addStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
     302                void addThreadDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args );
    294303
    295304                static void implement( std::list< Declaration * > & translationUnit ) {
     
    302311                StructDecl* guard_decl = nullptr;
    303312                StructDecl* dtor_guard_decl = nullptr;
     313                StructDecl* thread_guard_decl = nullptr;
    304314
    305315                static std::unique_ptr< Type > generic_func;
     
    801811                bool first = false;
    802812                std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
    803                 bool isDtor = CodeGen::isDestructor( decl->name );
     813                bool const isDtor = CodeGen::isDestructor( decl->name );
    804814
    805815                // Is this function relevant to monitors
     
    849859
    850860                // Instrument the body
    851                 if( isDtor ) {
    852                         addDtorStatments( decl, body, mutexArgs );
     861                if ( isDtor && isThread( mutexArgs.front() ) ) {
     862                        if( !thread_guard_decl ) {
     863                                SemanticError( decl, "thread destructor requires threads to be in scope, add #include <thread.hfa>\n" );
     864                        }
     865                        addThreadDtorStatements( decl, body, mutexArgs );
     866                }
     867                else if ( isDtor ) {
     868                        addDtorStatements( decl, body, mutexArgs );
    853869                }
    854870                else {
    855                         addStatments( decl, body, mutexArgs );
     871                        addStatements( decl, body, mutexArgs );
    856872                }
    857873        }
     
    870886                        assert( !dtor_guard_decl );
    871887                        dtor_guard_decl = decl;
     888                }
     889                else if( decl->name == "thread_dtor_guard_t" && decl->body ) {
     890                        assert( !thread_guard_decl );
     891                        thread_guard_decl = decl;
    872892                }
    873893        }
     
    908928        }
    909929
    910         void MutexKeyword::addDtorStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
     930        void MutexKeyword::addDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
    911931                Type * arg_type = args.front()->get_type()->clone();
    912932                arg_type->set_mutex( false );
     
    957977
    958978                //$monitor * __monitors[] = { get_monitor(a), get_monitor(b) };
    959                 body->push_front( new DeclStmt( monitors) );
    960         }
    961 
    962         void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
     979                body->push_front( new DeclStmt( monitors ) );
     980        }
     981
     982        void MutexKeyword::addThreadDtorStatements(
     983                        FunctionDecl*, CompoundStmt * body,
     984                        const std::list<DeclarationWithType * > & args ) {
     985                assert( args.size() == 1 );
     986                DeclarationWithType * arg = args.front();
     987                Type * arg_type = arg->get_type()->clone();
     988                assert( arg_type->get_mutex() );
     989                arg_type->set_mutex( false );
     990
     991                // thread_dtor_guard_t __guard = { this, intptr( 0 ) };
     992                body->push_front(
     993                        new DeclStmt( new ObjectDecl(
     994                                "__guard",
     995                                noStorageClasses,
     996                                LinkageSpec::Cforall,
     997                                nullptr,
     998                                new StructInstType(
     999                                        noQualifiers,
     1000                                        thread_guard_decl
     1001                                ),
     1002                                new ListInit(
     1003                                        {
     1004                                                new SingleInit( new CastExpr( new VariableExpr( arg ), arg_type ) ),
     1005                                                new SingleInit( new UntypedExpr(
     1006                                                        new NameExpr( "intptr" ), {
     1007                                                                new ConstantExpr( Constant::from_int( 0 ) ),
     1008                                                        }
     1009                                                ) ),
     1010                                        },
     1011                                        noDesignators,
     1012                                        true
     1013                                )
     1014                        ))
     1015                );
     1016        }
     1017
     1018        void MutexKeyword::addStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
    9631019                ObjectDecl * monitors = new ObjectDecl(
    9641020                        "__monitors",
  • src/GenPoly/Specialize.cc

    r912cc7d7 rab8c6a6  
    321321        }
    322322
     323        // Fold it into Specialize if we find a good way.
     324        struct StaticThunks final : public WithShortCircuiting {
     325                void previsit( Declaration * ) {
     326                        visit_children = false;
     327                }
     328                void postvisit( FunctionDecl * decl ) {
     329                        if ( isPrefix( decl->name, "_thunk" ) ) {
     330                                decl->storageClasses.is_static = true;
     331                        }
     332                }
     333        };
     334
    323335        void convertSpecializations( std::list< Declaration* >& translationUnit ) {
    324336                PassVisitor<Specialize> spec;
    325337                mutateAll( translationUnit, spec );
     338                PassVisitor<StaticThunks> staticThunks;
     339                acceptAll( translationUnit, staticThunks );
    326340        }
    327341} // namespace GenPoly
Note: See TracChangeset for help on using the changeset viewer.