Ignore:
Timestamp:
Oct 28, 2022, 3:12:16 PM (3 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, master
Children:
fa2e183
Parents:
e874605 (diff), 22a0e87 (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
libcfa/src/concurrency
Files:
1 added
12 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/clib/cfathread.cfa

    re874605 r93d2219  
    172172
    173173                pthread_attr_t attr;
    174                 if (int ret = pthread_attr_init(&attr); 0 != ret) {
     174                if (int ret = __cfaabi_pthread_attr_init(&attr); 0 != ret) {
    175175                        abort | "failed to create master epoll thread attr: " | ret | strerror(ret);
    176176                }
    177177
    178                 if (int ret = pthread_create(&master_poller, &attr, master_epoll, 0p); 0 != ret) {
     178                if (int ret = __cfaabi_pthread_create(&master_poller, &attr, master_epoll, 0p); 0 != ret) {
    179179                        abort | "failed to create master epoll thread: " | ret | strerror(ret);
    180180                }
  • libcfa/src/concurrency/invoke.h

    re874605 r93d2219  
    214214
    215215                #if defined( __CFA_WITH_VERIFY__ )
     216                        struct processor * volatile executing;
    216217                        void * canary;
    217218                #endif
  • libcfa/src/concurrency/io.cfa

    re874605 r93d2219  
    610610                if( we ) {
    611611                        sigval_t value = { PREEMPT_IO };
    612                         pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value);
     612                        __cfaabi_pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value);
    613613                }
    614614
  • libcfa/src/concurrency/io/setup.cfa

    re874605 r93d2219  
    344344        //      iopoll.run = false;
    345345        //      sigval val = { 1 };
    346         //      pthread_sigqueue( iopoll.thrd, SIGUSR1, val );
     346        //      __cfaabi_pthread_sigqueue( iopoll.thrd, SIGUSR1, val );
    347347
    348348        //      // Make sure all this is done
  • libcfa/src/concurrency/kernel.cfa

    re874605 r93d2219  
    321321                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor
    322322                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor
     323                /* paranoid */ verify( __atomic_exchange_n( &thrd_dst->executing, this, __ATOMIC_SEQ_CST) == 0p );
    323324                /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
    324325
     
    332333
    333334                /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
     335                /* paranoid */ verify( __atomic_exchange_n( &thrd_dst->executing, 0p, __ATOMIC_SEQ_CST) == this );
    334336                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst );
    335337                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst );
     338                /* paranoid */ verify( thrd_dst->state != Halted );
    336339                /* paranoid */ verify( thrd_dst->context.SP );
    337                 /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );
    338340                /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
    339341                /* paranoid */ verify( ! __preemption_enabled() );
  • libcfa/src/concurrency/kernel.hfa

    re874605 r93d2219  
    160160// P9_EMBEDDED( processor, dlink(processor) )
    161161static inline tytagref( dlink(processor), dlink(processor) ) ?`inner( processor & this ) {
    162     dlink(processor) & b = this.link;
    163     tytagref( dlink(processor), dlink(processor) ) result = { b };
    164     return result;
     162        dlink(processor) & b = this.link;
     163        tytagref( dlink(processor), dlink(processor) ) result = { b };
     164        return result;
    165165}
    166166
     
    269269                io_context_params params;
    270270        } io;
     271
     272        struct {
     273                struct processor ** procs;
     274                unsigned cnt;
     275        } managed;
    271276
    272277        #if !defined(__CFA_NO_STATISTICS__)
     
    298303static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; }
    299304
     305// set the number of internal processors
     306// these processors are in addition to any explicitly declared processors
     307unsigned set_concurrency( cluster & this, unsigned new_count );
     308
    300309#if !defined(__CFA_NO_STATISTICS__)
    301310        void print_stats_now( cluster & this, int flags );
  • libcfa/src/concurrency/kernel/private.hfa

    re874605 r93d2219  
    2020#endif
    2121
     22#include <signal.h>
     23
    2224#include "kernel.hfa"
    2325#include "thread.hfa"
     
    4850        #endif
    4951#endif
    50 
    5152// #define READYQ_USE_LINEAR_AVG
    5253#define READYQ_USE_LOGDBL_AVG
     
    6263#error must pick a scheme for averaging
    6364#endif
     65
     66extern "C" {
     67        __attribute__((visibility("protected"))) int __cfaabi_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
     68        __attribute__((visibility("protected"))) int __cfaabi_pthread_join(pthread_t _thread, void **retval);
     69        __attribute__((visibility("protected"))) pthread_t __cfaabi_pthread_self(void);
     70        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_init(pthread_attr_t *attr);
     71        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr);
     72        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize );
     73        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize );
     74        __attribute__((visibility("protected"))) int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value);
     75        __attribute__((visibility("protected"))) int __cfaabi_pthread_sigmask( int how, const sigset_t *set, sigset_t *oset);
     76}
    6477
    6578//-----------------------------------------------------------------------------
  • libcfa/src/concurrency/kernel/startup.cfa

    re874605 r93d2219  
    1616#define __cforall_thread__
    1717#define _GNU_SOURCE
     18
     19// #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
    1820
    1921// C Includes
     
    222224                ( this.runner ){};
    223225                init( this, "Main Processor", *mainCluster, 0p );
    224                 kernel_thread = pthread_self();
     226                kernel_thread = __cfaabi_pthread_self();
    225227
    226228                runner{ &this };
     
    283285}
    284286
     287extern "C"{
     288        void pthread_delete_kernel_threads_();
     289}
     290
     291
    285292static void __kernel_shutdown(void) {
    286293        if(!cfa_main_returned) return;
     294
     295        //delete kernel threads for pthread_concurrency
     296        pthread_delete_kernel_threads_();
     297
    287298        /* paranoid */ verify( __preemption_enabled() );
    288299        disable_interrupts();
     
    327338
    328339                /* paranoid */ verify( this.do_terminate == true );
    329                 __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
     340                __cfadbg_print_safe(runtime_core, "Kernel : destroyed main processor context %p\n", &runner);
    330341        }
    331342
     
    388399        (proc->runner){ proc, &info };
    389400
    390         __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
     401        __cfadbg_print_safe(runtime_core, "Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
    391402
    392403        //Set global state
     
    520531        random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl();
    521532        #if defined( __CFA_WITH_VERIFY__ )
     533                executing = 0p;
    522534                canary = 0x0D15EA5E0D15EA5Ep;
    523535        #endif
     
    652664        io.params = io_params;
    653665
     666        managed.procs = 0p;
     667        managed.cnt = 0;
     668
    654669        doregister(this);
    655670
     
    667682
    668683void ^?{}(cluster & this) libcfa_public {
     684        set_concurrency( this, 0 );
     685
    669686        destroy(this.io.arbiter);
    670687
     
    777794        pthread_attr_t attr;
    778795
    779         check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     796        check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    780797
    781798        size_t stacksize = max( PTHREAD_STACK_MIN, DEFAULT_STACK_SIZE );
     
    804821        #endif
    805822
    806         check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
    807         check( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
     823        check( __cfaabi_pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     824        check( __cfaabi_pthread_create( pthread, &attr, start, arg ), "pthread_create" );
    808825        return stack;
    809826}
    810827
    811828void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
    812         int err = pthread_join( pthread, retval );
     829        int err = __cfaabi_pthread_join( pthread, retval );
    813830        if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
    814831
     
    816833                pthread_attr_t attr;
    817834
    818                 check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     835                check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    819836
    820837                size_t stacksize;
    821838                // default stack size, normally defined by shell limit
    822                 check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     839                check( __cfaabi_pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
    823840                assert( stacksize >= PTHREAD_STACK_MIN );
    824841                stacksize += __page_size;
     
    838855}
    839856
     857unsigned set_concurrency( cluster & this, unsigned new ) libcfa_public {
     858        unsigned old = this.managed.cnt;
     859
     860        __cfadbg_print_safe(runtime_core, "Kernel : resizing cluster from %u to %u\n", old, (unsigned)new);
     861
     862        // Delete all the old unneeded procs
     863        if(old > new) for(i; (unsigned)new ~ old) {
     864                __cfadbg_print_safe(runtime_core, "Kernel : destroying %u\n", i);
     865                delete( this.managed.procs[i] );
     866        }
     867
     868        // Allocate new array (uses realloc and memcpies the data)
     869        this.managed.procs = alloc( new, this.managed.procs`realloc );
     870        this.managed.cnt = new;
     871
     872        // Create the desired new procs
     873        if(old < new) for(i; old ~ new) {
     874                __cfadbg_print_safe(runtime_core, "Kernel : constructing %u\n", i);
     875                (*(this.managed.procs[i] = alloc())){ this };
     876        }
     877
     878        // return the old count
     879        return old;
     880}
     881
    840882#if defined(__CFA_WITH_VERIFY__)
    841883static bool verify_fwd_bck_rng(void) {
  • libcfa/src/concurrency/locks.hfa

    re874605 r93d2219  
    2121
    2222#include "bits/weakso_locks.hfa"
    23 #include "containers/queueLockFree.hfa"
     23#include "containers/lockfree.hfa"
    2424#include "containers/list.hfa"
    2525
     
    423423}
    424424
    425 static inline size_t on_wait(simple_owner_lock & this) with(this) { 
     425static inline size_t on_wait(simple_owner_lock & this) with(this) {
    426426        lock( lock __cfaabi_dbg_ctx2 );
    427427        /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
  • libcfa/src/concurrency/preemption.cfa

    re874605 r93d2219  
    352352        sigset_t oldset;
    353353        int ret;
    354         ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
     354        ret = __cfaabi_pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
    355355        if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
    356356
     
    385385        sigaddset( &mask, sig );
    386386
    387         if ( pthread_sigmask( SIG_UNBLOCK, &mask, 0p ) == -1 ) {
     387        if ( __cfaabi_pthread_sigmask( SIG_UNBLOCK, &mask, 0p ) == -1 ) {
    388388            abort( "internal error, pthread_sigmask" );
    389389        }
     
    396396        sigaddset( &mask, sig );
    397397
    398         if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
     398        if ( __cfaabi_pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
    399399                abort( "internal error, pthread_sigmask" );
    400400        }
     
    404404static void preempt( processor * this ) {
    405405        sigval_t value = { PREEMPT_NORMAL };
    406         pthread_sigqueue( this->kernel_thread, SIGUSR1, value );
     406        __cfaabi_pthread_sigqueue( this->kernel_thread, SIGUSR1, value );
    407407}
    408408
     
    415415        sigset_t oldset;
    416416        int ret;
    417         ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
     417        ret = __cfaabi_pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
    418418        if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
    419419
     
    434434        sigset_t oldset;
    435435        int ret;
    436         ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
     436        ret = __cfaabi_pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
    437437        if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
    438438
     
    505505        sigval val;
    506506        val.sival_int = 0;
    507         pthread_sigqueue( alarm_thread, SIGALRM, val );
     507        __cfaabi_pthread_sigqueue( alarm_thread, SIGALRM, val );
    508508
    509509        // Wait for the preemption thread to finish
     
    579579        static_assert( sizeof( sigset_t ) == sizeof( cxt->uc_sigmask ), "Expected cxt->uc_sigmask to be of sigset_t" );
    580580        #endif
    581         if ( pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), 0p ) == -1 ) {
     581        if ( __cfaabi_pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), 0p ) == -1 ) {
    582582                abort( "internal error, sigprocmask" );
    583583        }
     
    607607        sigset_t mask;
    608608        sigfillset(&mask);
    609         if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
     609        if ( __cfaabi_pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
    610610            abort( "internal error, pthread_sigmask" );
    611611        }
  • libcfa/src/concurrency/thread.cfa

    re874605 r93d2219  
    5050        random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl();
    5151        #if defined( __CFA_WITH_VERIFY__ )
     52                executing = 0p;
    5253                canary = 0x0D15EA5E0D15EA5Ep;
    5354        #endif
     
    177178
    178179//-----------------------------------------------------------------------------
     180bool migrate( thread$ * thrd, struct cluster & cl ) {
     181
     182        monitor$ * tmon = get_monitor(thrd);
     183        monitor$ * __monitors[] = { tmon };
     184        monitor_guard_t __guard = { __monitors, 1 };
     185
     186
     187        {
     188                // if nothing needs to be done, return false
     189                if( thrd->curr_cluster == &cl ) return false;
     190
     191                // are we migrating ourself?
     192                const bool local = thrd == active_thread();
     193
     194                /* paranoid */ verify( !local || &cl != active_cluster() );
     195                /* paranoid */ verify( !local || thrd->curr_cluster == active_cluster() );
     196                /* paranoid */ verify( !local || thrd->curr_cluster == active_processor()->cltr );
     197                /* paranoid */ verify( local || tmon->signal_stack.top->owner->waiting_thread == thrd );
     198                /* paranoid */ verify( local || tmon->signal_stack.top );
     199
     200                // make sure we aren't interrupted while doing this
     201                // not as important if we aren't local
     202                disable_interrupts();
     203
     204                // actually move the thread
     205                unregister( thrd->curr_cluster, *thrd );
     206                thrd->curr_cluster = &cl;
     207                doregister( thrd->curr_cluster, *thrd );
     208
     209                // restore interrupts
     210                enable_interrupts();
     211
     212                // if this is the local thread, we are still running on the old cluster
     213                if(local) yield();
     214
     215                /* paranoid */ verify( !local || &cl == active_cluster() );
     216                /* paranoid */ verify( !local || thrd->curr_cluster == active_cluster() );
     217                /* paranoid */ verify( !local || thrd->curr_cluster == active_processor()->cltr );
     218                /* paranoid */ verify(  local || tmon->signal_stack.top );
     219                /* paranoid */ verify(  local || tmon->signal_stack.top->owner->waiting_thread == thrd );
     220
     221                return true;
     222        }
     223}
     224
     225//-----------------------------------------------------------------------------
    179226#define GENERATOR LCG
    180227
  • libcfa/src/concurrency/thread.hfa

    re874605 r93d2219  
    132132
    133133//----------
     134// misc
     135bool migrate( thread$ * thrd, struct cluster & cl );
     136
     137forall( T & | is_thread(T) )
     138static inline bool migrate( T & mutex thrd, struct cluster & cl ) { return migrate( &(thread&)thrd, cl ); }
     139
     140
     141//----------
    134142// prng
    135143static inline {
Note: See TracChangeset for help on using the changeset viewer.