Ignore:
Timestamp:
Feb 10, 2020, 11:17:38 AM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
3966d9a, 41efd33
Parents:
807a632 (diff), d231700 (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:
16 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/CtxSwitch-x86_64.S

    r807a632 r3b56166  
    8787CtxInvokeStub:
    8888        movq %rbx, %rdi
    89         jmp *%r12
     89        movq %r12, %rsi
     90        jmp *%r13
    9091        .size  CtxInvokeStub, .-CtxInvokeStub
    9192
  • libcfa/src/concurrency/alarm.cfa

    r807a632 r3b56166  
    1010// Created On       : Fri Jun 2 11:31:25 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri May 25 06:25:47 2018
    13 // Update Count     : 67
     12// Last Modified On : Sun Jan  5 08:41:36 2020
     13// Update Count     : 69
    1414//
    1515
     
    3939
    4040void __kernel_set_timer( Duration alarm ) {
    41         verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%jins)", alarm.tv);
    42         setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL );
     41        verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%jins)", alarm`ns);
     42        setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
    4343}
    4444
     
    113113                        this->tail = &this->head;
    114114                }
    115                 head->next = NULL;
     115                head->next = 0p;
    116116        }
    117117        verify( validate( this ) );
     
    127127                this->tail = it;
    128128        }
    129         n->next = NULL;
     129        n->next = 0p;
    130130
    131131        verify( validate( this ) );
  • libcfa/src/concurrency/coroutine.cfa

    r807a632 r3b56166  
    1010// Created On       : Mon Nov 28 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Mar 30 17:20:57 2018
    13 // Update Count     : 9
     12// Last Modified On : Tue Feb  4 12:29:25 2020
     13// Update Count     : 16
    1414//
    1515
     
    8989}
    9090
    91 void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ) with( this ) {
    92         (this.context){NULL, NULL};
     91void ?{}( coroutine_desc & this, const char name[], void * storage, size_t storageSize ) with( this ) {
     92        (this.context){0p, 0p};
    9393        (this.stack){storage, storageSize};
    9494        this.name = name;
    9595        state = Start;
    96         starter = NULL;
    97         last = NULL;
    98         cancellation = NULL;
     96        starter = 0p;
     97        last = 0p;
     98        cancellation = 0p;
    9999}
    100100
     
    131131
    132132[void *, size_t] __stack_alloc( size_t storageSize ) {
    133         static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
     133        const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
    134134        assert(__page_size != 0l);
    135135        size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
     
    157157
    158158void __stack_prepare( __stack_info_t * this, size_t create_size ) {
    159         static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
     159        const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
    160160        bool userStack;
    161161        void * storage;
     
    187187// is not inline (We can't inline Cforall in C)
    188188extern "C" {
    189         void __suspend_internal(void) {
    190                 suspend();
    191         }
    192 
    193         void __leave_coroutine( coroutine_desc * src ) {
     189        void __leave_coroutine( struct coroutine_desc * src ) {
    194190                coroutine_desc * starter = src->cancellation != 0 ? src->last : src->starter;
    195191
     
    207203                CoroutineCtxSwitch( src, starter );
    208204        }
     205
     206        struct coroutine_desc * __finish_coroutine(void) {
     207                struct coroutine_desc * cor = kernelTLS.this_thread->curr_cor;
     208
     209                if(cor->state == Primed) {
     210                        suspend();
     211                }
     212
     213                cor->state = Active;
     214
     215                return cor;
     216        }
    209217}
    210218
  • libcfa/src/concurrency/coroutine.hfa

    r807a632 r3b56166  
    1010// Created On       : Mon Nov 28 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jun 21 17:49:39 2019
    13 // Update Count     : 9
     12// Last Modified On : Tue Feb  4 12:29:26 2020
     13// Update Count     : 11
    1414//
    1515
     
    3535// void ^?{}( coStack_t & this );
    3636
    37 void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize );
     37void ?{}( coroutine_desc & this, const char name[], void * storage, size_t storageSize );
    3838void ^?{}( coroutine_desc & this );
    3939
    40 static inline void ?{}( coroutine_desc & this)                                       { this{ "Anonymous Coroutine", NULL, 0 }; }
    41 static inline void ?{}( coroutine_desc & this, size_t stackSize)                     { this{ "Anonymous Coroutine", NULL, stackSize }; }
     40static inline void ?{}( coroutine_desc & this)                                       { this{ "Anonymous Coroutine", 0p, 0 }; }
     41static inline void ?{}( coroutine_desc & this, size_t stackSize)                     { this{ "Anonymous Coroutine", 0p, stackSize }; }
    4242static inline void ?{}( coroutine_desc & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; }
    43 static inline void ?{}( coroutine_desc & this, const char * name)                    { this{ name, NULL, 0 }; }
    44 static inline void ?{}( coroutine_desc & this, const char * name, size_t stackSize ) { this{ name, NULL, stackSize }; }
     43static inline void ?{}( coroutine_desc & this, const char name[])                    { this{ name, 0p, 0 }; }
     44static inline void ?{}( coroutine_desc & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; }
    4545
    4646//-----------------------------------------------------------------------------
     
    6161// Start coroutine routines
    6262extern "C" {
    63       forall(dtype T | is_coroutine(T))
    64       void CtxInvokeCoroutine(T * this);
     63        void CtxInvokeCoroutine(void (*main)(void *), void * this);
    6564
    66       forall(dtype T | is_coroutine(T))
    67       void CtxStart(T * this, void ( *invoke)(T *));
     65        forall(dtype T)
     66        void CtxStart(void (*main)(T &), struct coroutine_desc * cor, T & this, void (*invoke)(void (*main)(void *), void *));
    6867
    6968        extern void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc *) __attribute__ ((__noreturn__));
     
    8988        src->state = Active;
    9089
    91         if( unlikely(src->cancellation != NULL) ) {
     90        if( unlikely(src->cancellation != 0p) ) {
    9291                _CtxCoroutine_Unwind(src->cancellation, src);
    9392        }
     
    128127        coroutine_desc * dst = get_coroutine(cor);
    129128
    130         if( unlikely(dst->context.SP == NULL) ) {
     129        if( unlikely(dst->context.SP == 0p) ) {
     130                TL_GET( this_thread )->curr_cor = dst;
    131131                __stack_prepare(&dst->stack, 65000);
    132                 CtxStart(&cor, CtxInvokeCoroutine);
     132                CtxStart(main, dst, cor, CtxInvokeCoroutine);
     133                TL_GET( this_thread )->curr_cor = src;
    133134        }
    134135
  • libcfa/src/concurrency/invoke.c

    r807a632 r3b56166  
    2929// Called from the kernel when starting a coroutine or task so must switch back to user mode.
    3030
    31 extern void __suspend_internal(void);
    32 extern void __leave_coroutine( struct coroutine_desc * );
    33 extern void __finish_creation( struct thread_desc * );
    34 extern void __leave_thread_monitor( struct thread_desc * this );
     31extern void __leave_coroutine ( struct coroutine_desc * );
     32extern struct coroutine_desc * __finish_coroutine(void);
     33extern void __leave_thread_monitor();
    3534extern void disable_interrupts() OPTIONAL_THREAD;
    3635extern void enable_interrupts( __cfaabi_dbg_ctx_param );
     
    3837void CtxInvokeCoroutine(
    3938        void (*main)(void *),
    40         struct coroutine_desc *(*get_coroutine)(void *),
    4139        void *this
    4240) {
    43         struct coroutine_desc* cor = get_coroutine( this );
     41        // Finish setting up the coroutine by setting its state
     42        struct coroutine_desc * cor = __finish_coroutine();
    4443
    45         if(cor->state == Primed) {
    46                 __suspend_internal();
    47         }
    48 
    49         cor->state = Active;
    50 
     44        // Call the main of the coroutine
    5145        main( this );
    5246
     
    8377
    8478void CtxInvokeThread(
    85         void (*dtor)(void *),
    8679        void (*main)(void *),
    87         struct thread_desc *(*get_thread)(void *),
    8880        void *this
    8981) {
    90         // Fetch the thread handle from the user defined thread structure
    91         struct thread_desc* thrd = get_thread( this );
    92 
    93         // First suspend, once the thread arrives here,
    94         // the function pointer to main can be invalidated without risk
    95         __finish_creation( thrd );
    96 
    9782        // Officially start the thread by enabling preemption
    9883        enable_interrupts( __cfaabi_dbg_ctx );
     
    10893        // The order of these 4 operations is very important
    10994        //Final suspend, should never return
    110         __leave_thread_monitor( thrd );
     95        __leave_thread_monitor();
    11196        __cabi_abort( "Resumed dead thread" );
    11297}
    11398
    114 
    11599void CtxStart(
    116100        void (*main)(void *),
    117         struct coroutine_desc *(*get_coroutine)(void *),
     101        struct coroutine_desc * cor,
    118102        void *this,
    119103        void (*invoke)(void *)
    120104) {
    121         struct coroutine_desc * cor = get_coroutine( this );
    122105        struct __stack_t * stack = cor->stack.storage;
    123106
     
    138121
    139122        fs->dummyReturn = NULL;
    140         fs->argument[0] = this;     // argument to invoke
     123        fs->argument[0] = main;     // argument to invoke
     124        fs->argument[1] = this;     // argument to invoke
    141125        fs->rturn = invoke;
    142126
     
    156140        fs->dummyReturn = NULL;
    157141        fs->rturn = CtxInvokeStub;
    158         fs->fixedRegisters[0] = this;
    159         fs->fixedRegisters[1] = invoke;
     142        fs->fixedRegisters[0] = main;
     143        fs->fixedRegisters[1] = this;
     144        fs->fixedRegisters[2] = invoke;
    160145
    161146#elif defined( __ARM_ARCH )
    162 
     147#error ARM needs to be upgrade to use to parameters like X86/X64 (A.K.A. : I broke this and do not know how to fix it)
    163148        struct FakeStack {
    164149                float fpRegs[16];                       // floating point registers
  • libcfa/src/concurrency/invoke.h

    r807a632 r3b56166  
    1010// Created On       : Tue Jan 17 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun 22 18:19:13 2019
    13 // Update Count     : 40
     12// Last Modified On : Thu Dec  5 16:26:03 2019
     13// Update Count     : 44
    1414//
    1515
     
    4646        #ifdef __cforall
    4747        extern "Cforall" {
    48                 extern thread_local struct KernelThreadData {
     48                extern __attribute__((aligned(128))) thread_local struct KernelThreadData {
    4949                        struct thread_desc    * volatile this_thread;
    5050                        struct processor      * volatile this_processor;
     
    5555                                volatile bool in_progress;
    5656                        } preemption_state;
     57
     58                        uint32_t rand_seed;
    5759                } kernelTLS __attribute__ ((tls_model ( "initial-exec" )));
    5860        }
     
    205207
    206208                static inline void ?{}(__monitor_group_t & this) {
    207                         (this.data){NULL};
     209                        (this.data){0p};
    208210                        (this.size){0};
    209211                        (this.func){NULL};
  • libcfa/src/concurrency/kernel.cfa

    r807a632 r3b56166  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jun 20 17:21:23 2019
    13 // Update Count     : 25
     12// Last Modified On : Tue Feb  4 13:03:15 2020
     13// Update Count     : 58
    1414//
    1515
     
    2626#include <signal.h>
    2727#include <unistd.h>
     28#include <limits.h>                                                                             // PTHREAD_STACK_MIN
     29#include <sys/mman.h>                                                                   // mprotect
    2830}
    2931
     
    4042//-----------------------------------------------------------------------------
    4143// Some assembly required
    42 #if   defined( __i386 )
     44#if defined( __i386 )
    4345        #define CtxGet( ctx )        \
    4446                __asm__ volatile (     \
     
    123125
    124126extern "C" {
    125 struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
     127        struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
    126128}
    127129
     
    131133// Global state
    132134thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = {
     135        NULL,                                                                                           // cannot use 0p
    133136        NULL,
    134         NULL,
    135         { 1, false, false }
     137        { 1, false, false },
     138        6u //this should be seeded better but due to a bug calling rdtsc doesn't work
    136139};
    137140
     
    139142// Struct to steal stack
    140143struct current_stack_info_t {
    141         __stack_t * storage;            // pointer to stack object
    142         void *base;                             // base of stack
    143         void *limit;                    // stack grows towards stack limit
    144         void *context;                  // address of cfa_context_t
     144        __stack_t * storage;                                                            // pointer to stack object
     145        void * base;                                                                            // base of stack
     146        void * limit;                                                                           // stack grows towards stack limit
     147        void * context;                                                                         // address of cfa_context_t
    145148};
    146149
     
    171174        name = "Main Thread";
    172175        state = Start;
    173         starter = NULL;
    174         last = NULL;
    175         cancellation = NULL;
     176        starter = 0p;
     177        last = 0p;
     178        cancellation = 0p;
    176179}
    177180
     
    184187        self_mon.recursion = 1;
    185188        self_mon_p = &self_mon;
    186         next = NULL;
    187 
    188         node.next = NULL;
    189         node.prev = NULL;
     189        next = 0p;
     190
     191        node.next = 0p;
     192        node.prev = 0p;
    190193        doregister(curr_cluster, this);
    191194
     
    206209
    207210static void start(processor * this);
    208 void ?{}(processor & this, const char * name, cluster & cltr) with( this ) {
     211void ?{}(processor & this, const char name[], cluster & cltr) with( this ) {
    209212        this.name = name;
    210213        this.cltr = &cltr;
    211214        terminated{ 0 };
    212215        do_terminate = false;
    213         preemption_alarm = NULL;
     216        preemption_alarm = 0p;
    214217        pending_preemption = false;
    215218        runner.proc = &this;
     
    231234        }
    232235
    233         pthread_join( kernel_thread, NULL );
    234 }
    235 
    236 void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
     236        pthread_join( kernel_thread, 0p );
     237        free( this.stack );
     238}
     239
     240void ?{}(cluster & this, const char name[], Duration preemption_rate) with( this ) {
    237241        this.name = name;
    238242        this.preemption_rate = preemption_rate;
     
    260264//Main of the processor contexts
    261265void main(processorCtx_t & runner) {
     266        // Because of a bug, we couldn't initialized the seed on construction
     267        // Do it here
     268        kernelTLS.rand_seed ^= rdtscl();
     269
    262270        processor * this = runner.proc;
    263271        verify(this);
     
    273281                __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
    274282
    275                 thread_desc * readyThread = NULL;
    276                 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
    277                 {
     283                thread_desc * readyThread = 0p;
     284                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) {
    278285                        readyThread = nextThread( this->cltr );
    279286
    280                         if(readyThread)
    281                         {
     287                        if(readyThread) {
    282288                                verify( ! kernelTLS.preemption_state.enabled );
    283289
     
    290296
    291297                                spin_count = 0;
    292                         }
    293                         else
    294                         {
     298                        } else {
    295299                                // spin(this, &spin_count);
    296300                                halt(this);
     
    405409        processor * proc = (processor *) arg;
    406410        kernelTLS.this_processor = proc;
    407         kernelTLS.this_thread    = NULL;
     411        kernelTLS.this_thread    = 0p;
    408412        kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
    409413        // SKULLDUGGERY: We want to create a context for the processor coroutine
     
    418422
    419423        //Set global state
    420         kernelTLS.this_thread    = NULL;
     424        kernelTLS.this_thread = 0p;
    421425
    422426        //We now have a proper context from which to schedule threads
     
    434438        __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
    435439
    436         return NULL;
     440        return 0p;
     441}
     442
     443static void Abort( int ret, const char func[] ) {
     444        if ( ret ) {                                                                            // pthread routines return errno values
     445                abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
     446        } // if
     447} // Abort
     448
     449void * create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
     450        pthread_attr_t attr;
     451
     452        Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     453
     454        size_t stacksize;
     455        // default stack size, normally defined by shell limit
     456        Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     457        assert( stacksize >= PTHREAD_STACK_MIN );
     458
     459        void * stack;
     460        __cfaabi_dbg_debug_do(
     461                stack = memalign( __page_size, stacksize + __page_size );
     462                // pthread has no mechanism to create the guard page in user supplied stack.
     463                if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     464                        abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     465                } // if
     466        );
     467        __cfaabi_dbg_no_debug_do(
     468                stack = malloc( stacksize );
     469        );
     470
     471        Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     472
     473        Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
     474        return stack;
    437475}
    438476
     
    440478        __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
    441479
    442         pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
     480        this->stack = create_pthread( &this->kernel_thread, CtxInvokeProcessor, (void *)this );
    443481
    444482        __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
     
    452490        verify( ! kernelTLS.preemption_state.enabled );
    453491
     492        kernelTLS.this_thread->curr_cor = dst;
    454493        __stack_prepare( &dst->stack, 65000 );
    455         CtxStart(&this->runner, CtxInvokeCoroutine);
     494        CtxStart(main, dst, this->runner, CtxInvokeCoroutine);
    456495
    457496        verify( ! kernelTLS.preemption_state.enabled );
     
    468507        // when CtxSwitch returns we are back in the src coroutine
    469508
     509        mainThread->curr_cor = &mainThread->self_cor;
     510
    470511        // set state of new coroutine to active
    471512        src->state = Active;
     
    497538        verify( ! kernelTLS.preemption_state.enabled );
    498539
    499         verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
     540        verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
    500541
    501542        with( *thrd->curr_cluster ) {
     
    676717        void ?{}(processorCtx_t & this, processor * proc) {
    677718                (this.__cor){ "Processor" };
    678                 this.__cor.starter = NULL;
     719                this.__cor.starter = 0p;
    679720                this.proc = proc;
    680721        }
     
    685726                terminated{ 0 };
    686727                do_terminate = false;
    687                 preemption_alarm = NULL;
     728                preemption_alarm = 0p;
    688729                pending_preemption = false;
    689730                kernel_thread = pthread_self();
     
    803844                sigemptyset( &mask );
    804845                sigaddset( &mask, SIGALRM );            // block SIGALRM signals
    805                 sigsuspend( &mask );                    // block the processor to prevent further damage during abort
    806                 _exit( EXIT_FAILURE );                  // if processor unblocks before it is killed, terminate it
     846                sigaddset( &mask, SIGUSR1 );            // block SIGALRM signals
     847                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
     848                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
    807849        }
    808850        else {
     
    819861        if(thrd) {
    820862                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
    821                 __cfaabi_dbg_bits_write( abort_text, len );
     863                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    822864
    823865                if ( &thrd->self_cor != thrd->curr_cor ) {
    824866                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
    825                         __cfaabi_dbg_bits_write( abort_text, len );
     867                        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    826868                }
    827869                else {
    828                         __cfaabi_dbg_bits_write( ".\n", 2 );
     870                        __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
    829871                }
    830872        }
    831873        else {
    832874                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
    833                 __cfaabi_dbg_bits_write( abort_text, len );
     875                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    834876        }
    835877}
     
    842884
    843885extern "C" {
    844         void __cfaabi_dbg_bits_acquire() {
     886        void __cfaabi_bits_acquire() {
    845887                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
    846888        }
    847889
    848         void __cfaabi_dbg_bits_release() {
     890        void __cfaabi_bits_release() {
    849891                unlock( kernel_debug_lock );
    850892        }
     
    879921
    880922void V(semaphore & this) with( this ) {
    881         thread_desc * thrd = NULL;
     923        thread_desc * thrd = 0p;
    882924        lock( lock __cfaabi_dbg_ctx2 );
    883925        count += 1;
     
    939981__cfaabi_dbg_debug_do(
    940982        extern "C" {
    941                 void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
     983                void __cfaabi_dbg_record(__spinlock_t & this, const char prev_name[]) {
    942984                        this.prev_name = prev_name;
    943985                        this.prev_thrd = kernelTLS.this_thread;
  • libcfa/src/concurrency/kernel.hfa

    r807a632 r3b56166  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun 22 11:39:17 2019
    13 // Update Count     : 16
     12// Last Modified On : Tue Feb  4 12:29:26 2020
     13// Update Count     : 22
    1414//
    1515
     
    2020#include "invoke.h"
    2121#include "time_t.hfa"
     22#include "coroutine.hfa"
    2223
    2324extern "C" {
     
    8889static inline void ?{}(FinishAction & this) {
    8990        this.action_code = No_Action;
    90         this.thrd = NULL;
    91         this.lock = NULL;
     91        this.thrd = 0p;
     92        this.lock = 0p;
    9293}
    9394static inline void ^?{}(FinishAction &) {}
     
    134135        semaphore terminated;
    135136
     137        // pthread Stack
     138        void * stack;
     139
    136140        // Link lists fields
    137141        struct __dbg_node_proc {
     
    146150};
    147151
    148 void  ?{}(processor & this, const char * name, struct cluster & cltr);
     152void  ?{}(processor & this, const char name[], struct cluster & cltr);
    149153void ^?{}(processor & this);
    150154
    151155static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
    152156static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
    153 static inline void  ?{}(processor & this, const char * name) { this{name, *mainCluster }; }
     157static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
    154158
    155159static inline [processor *&, processor *& ] __get( processor & this ) {
     
    191195extern Duration default_preemption();
    192196
    193 void ?{} (cluster & this, const char * name, Duration preemption_rate);
     197void ?{} (cluster & this, const char name[], Duration preemption_rate);
    194198void ^?{}(cluster & this);
    195199
    196200static inline void ?{} (cluster & this)                           { this{"Anonymous Cluster", default_preemption()}; }
    197201static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
    198 static inline void ?{} (cluster & this, const char * name)        { this{name, default_preemption()}; }
     202static inline void ?{} (cluster & this, const char name[])        { this{name, default_preemption()}; }
    199203
    200204static inline [cluster *&, cluster *& ] __get( cluster & this ) {
  • libcfa/src/concurrency/kernel_private.hfa

    r807a632 r3b56166  
    1010// Created On       : Mon Feb 13 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar 29 14:06:40 2018
    13 // Update Count     : 3
     12// Last Modified On : Sat Nov 30 19:25:02 2019
     13// Update Count     : 8
    1414//
    1515
     
    5757void main(processorCtx_t *);
    5858
     59void * create_pthread( pthread_t *, void * (*)(void *), void * );
     60
    5961static inline void wake_fast(processor * this) {
    6062        __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this);
     
    8688// Threads
    8789extern "C" {
    88       forall(dtype T | is_thread(T))
    89       void CtxInvokeThread(T * this);
     90      void CtxInvokeThread(void (*main)(void *), void * this);
    9091}
    9192
     
    100101// Utils
    101102#define KERNEL_STORAGE(T,X) static char storage_##X[sizeof(T)]
     103
     104static inline uint32_t tls_rand() {
     105        kernelTLS.rand_seed ^= kernelTLS.rand_seed << 6;
     106        kernelTLS.rand_seed ^= kernelTLS.rand_seed >> 21;
     107        kernelTLS.rand_seed ^= kernelTLS.rand_seed << 7;
     108        return kernelTLS.rand_seed;
     109}
    102110
    103111
  • libcfa/src/concurrency/monitor.cfa

    r807a632 r3b56166  
    1010// Created On       : Thd Feb 23 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Mar 30 14:30:26 2018
    13 // Update Count     : 9
     12// Last Modified On : Wed Dec  4 07:55:14 2019
     13// Update Count     : 10
    1414//
    1515
     
    243243        // last routine called by a thread.
    244244        // Should never return
    245         void __leave_thread_monitor( thread_desc * thrd ) {
     245        void __leave_thread_monitor() {
     246                thread_desc * thrd = TL_GET( this_thread );
    246247                monitor_desc * this = &thrd->self_mon;
    247248
     
    363364        this.waiting_thread = waiting_thread;
    364365        this.count = count;
    365         this.next = NULL;
     366        this.next = 0p;
    366367        this.user_info = user_info;
    367368}
     
    369370void ?{}(__condition_criterion_t & this ) with( this ) {
    370371        ready  = false;
    371         target = NULL;
    372         owner  = NULL;
    373         next   = NULL;
     372        target = 0p;
     373        owner  = 0p;
     374        next   = 0p;
    374375}
    375376
     
    378379        this.target = target;
    379380        this.owner  = &owner;
    380         this.next   = NULL;
     381        this.next   = 0p;
    381382}
    382383
     
    387388
    388389        // Check that everything is as expected
    389         assertf( this.monitors != NULL, "Waiting with no monitors (%p)", this.monitors );
     390        assertf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors );
    390391        verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count );
    391392        verifyf( this.monitor_count < 32u, "Excessive monitor count (%"PRIiFAST16")", this.monitor_count );
     
    449450
    450451        // Lock all monitors
    451         lock_all( this.monitors, NULL, count );
     452        lock_all( this.monitors, 0p, count );
    452453
    453454        //Pop the head of the waiting queue
     
    471472
    472473        //Check that everything is as expected
    473         verifyf( this.monitors != NULL, "Waiting with no monitors (%p)", this.monitors );
     474        verifyf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors );
    474475        verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count );
    475476
     
    674675
    675676static inline void reset_mask( monitor_desc * this ) {
    676         this->mask.accepted = NULL;
    677         this->mask.data = NULL;
     677        this->mask.accepted = 0p;
     678        this->mask.data = 0p;
    678679        this->mask.size = 0;
    679680}
     
    816817        }
    817818
    818         __cfaabi_dbg_print_safe( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL );
    819         return ready2run ? node->waiting_thread : NULL;
     819        __cfaabi_dbg_print_safe( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : 0p );
     820        return ready2run ? node->waiting_thread : 0p;
    820821}
    821822
     
    824825        if( !this.monitors ) {
    825826                // __cfaabi_dbg_print_safe( "Branding\n" );
    826                 assertf( thrd->monitors.data != NULL, "No current monitor to brand condition %p", thrd->monitors.data );
     827                assertf( thrd->monitors.data != 0p, "No current monitor to brand condition %p", thrd->monitors.data );
    827828                this.monitor_count = thrd->monitors.size;
    828829
  • libcfa/src/concurrency/monitor.hfa

    r807a632 r3b56166  
    1010// Created On       : Thd Feb 23 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Oct  7 18:06:45 2017
    13 // Update Count     : 10
     12// Last Modified On : Wed Dec  4 07:55:32 2019
     13// Update Count     : 11
    1414//
    1515
     
    3131        entry_queue{};
    3232        signal_stack{};
    33         owner         = NULL;
     33        owner         = 0p;
    3434        recursion     = 0;
    35         mask.accepted = NULL;
    36         mask.data     = NULL;
     35        mask.accepted = 0p;
     36        mask.data     = 0p;
    3737        mask.size     = 0;
    38         dtor_node     = NULL;
     38        dtor_node     = 0p;
    3939}
     40
     41static inline void ^?{}(monitor_desc & ) {}
    4042
    4143struct monitor_guard_t {
     
    120122
    121123static inline void ?{}( condition & this ) {
    122         this.monitors = NULL;
     124        this.monitors = 0p;
    123125        this.monitor_count = 0;
    124126}
  • libcfa/src/concurrency/mutex.cfa

    r807a632 r3b56166  
    1111// Author           : Thierry Delisle
    1212// Created On       : Fri May 25 01:37:11 2018
    13 // Last Modified By : Thierry Delisle
    14 // Last Modified On : Fri May 25 01:37:51 2018
    15 // Update Count     : 0
     13// Last Modified By : Peter A. Buhr
     14// Last Modified On : Wed Dec  4 09:16:39 2019
     15// Update Count     : 1
    1616//
    1717
     
    7373        this.lock{};
    7474        this.blocked_threads{};
    75         this.owner = NULL;
     75        this.owner = 0p;
    7676        this.recursion_count = 0;
    7777}
     
    8383void lock(recursive_mutex_lock & this) with(this) {
    8484        lock( lock __cfaabi_dbg_ctx2 );
    85         if( owner == NULL ) {
     85        if( owner == 0p ) {
    8686                owner = kernelTLS.this_thread;
    8787                recursion_count = 1;
     
    101101        bool ret = false;
    102102        lock( lock __cfaabi_dbg_ctx2 );
    103         if( owner == NULL ) {
     103        if( owner == 0p ) {
    104104                owner = kernelTLS.this_thread;
    105105                recursion_count = 1;
  • libcfa/src/concurrency/mutex.hfa

    r807a632 r3b56166  
    1111// Author           : Thierry Delisle
    1212// Created On       : Fri May 25 01:24:09 2018
    13 // Last Modified By : Thierry Delisle
    14 // Last Modified On : Fri May 25 01:24:12 2018
    15 // Update Count     : 0
     13// Last Modified By : Peter A. Buhr
     14// Last Modified On : Wed Dec  4 09:16:53 2019
     15// Update Count     : 1
    1616//
    1717
     
    110110
    111111        static inline void ?{}(lock_scope(L) & this) {
    112                 this.locks = NULL;
     112                this.locks = 0p;
    113113                this.count = 0;
    114114        }
  • libcfa/src/concurrency/preemption.cfa

    r807a632 r3b56166  
    1010// Created On       : Mon Jun 5 14:20:42 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jun  5 17:35:49 2018
    13 // Update Count     : 37
     12// Last Modified On : Thu Dec  5 16:34:05 2019
     13// Update Count     : 43
    1414//
    1515
     
    2424#include <string.h>
    2525#include <unistd.h>
     26#include <limits.h>                                                                             // PTHREAD_STACK_MIN
    2627}
    2728
     
    6465event_kernel_t * event_kernel;                        // kernel public handle to even kernel
    6566static pthread_t alarm_thread;                        // pthread handle to alarm thread
     67static void * alarm_stack;                                                        // pthread stack for alarm thread
    6668
    6769static void ?{}(event_kernel_t & this) with( this ) {
     
    8183// Get next expired node
    8284static inline alarm_node_t * get_expired( alarm_list_t * alarms, Time currtime ) {
    83         if( !alarms->head ) return NULL;                          // If no alarms return null
    84         if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
    85         return pop(alarms);                                       // Otherwise just pop head
     85        if( !alarms->head ) return 0p;                                          // If no alarms return null
     86        if( alarms->head->alarm >= currtime ) return 0p;        // If alarms head not expired return null
     87        return pop(alarms);                                                                     // Otherwise just pop head
    8688}
    8789
    8890// Tick one frame of the Discrete Event Simulation for alarms
    8991static void tick_preemption() {
    90         alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
    91         alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
    92         Time currtime = __kernel_get_time();                    // Check current time once so we everything "happens at once"
     92        alarm_node_t * node = 0p;                                                       // Used in the while loop but cannot be declared in the while condition
     93        alarm_list_t * alarms = &event_kernel->alarms;          // Local copy for ease of reading
     94        Time currtime = __kernel_get_time();                            // Check current time once so everything "happens at once"
    9395
    9496        //Loop throught every thing expired
     
    243245        sigaddset( &mask, sig );
    244246
    245         if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
     247        if ( pthread_sigmask( SIG_UNBLOCK, &mask, 0p ) == -1 ) {
    246248            abort( "internal error, pthread_sigmask" );
    247249        }
     
    254256        sigaddset( &mask, sig );
    255257
    256         if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
     258        if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
    257259            abort( "internal error, pthread_sigmask" );
    258260        }
     
    301303
    302304        // Setup proper signal handlers
    303         __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART );         // CtxSwitch handler
     305        __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART ); // CtxSwitch handler
    304306
    305307        signal_block( SIGALRM );
    306308
    307         pthread_create( &alarm_thread, NULL, alarm_loop, NULL );
     309        alarm_stack = create_pthread( &alarm_thread, alarm_loop, 0p );
    308310}
    309311
     
    316318        sigset_t mask;
    317319        sigfillset( &mask );
    318         sigprocmask( SIG_BLOCK, &mask, NULL );
     320        sigprocmask( SIG_BLOCK, &mask, 0p );
    319321
    320322        // Notify the alarm thread of the shutdown
     
    323325
    324326        // Wait for the preemption thread to finish
    325         pthread_join( alarm_thread, NULL );
     327
     328        pthread_join( alarm_thread, 0p );
     329        free( alarm_stack );
    326330
    327331        // Preemption is now fully stopped
     
    380384        static_assert( sizeof( sigset_t ) == sizeof( cxt->uc_sigmask ), "Expected cxt->uc_sigmask to be of sigset_t" );
    381385        #endif
    382         if ( pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), NULL ) == -1 ) {
     386        if ( pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), 0p ) == -1 ) {
    383387                abort( "internal error, sigprocmask" );
    384388        }
     
    399403        sigset_t mask;
    400404        sigfillset(&mask);
    401         if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
     405        if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
    402406            abort( "internal error, pthread_sigmask" );
    403407        }
     
    420424                                        {__cfaabi_dbg_print_buffer_decl( " KERNEL: Spurious wakeup %d.\n", err );}
    421425                                        continue;
    422                         case EINVAL :
     426                                case EINVAL :
    423427                                        abort( "Timeout was invalid." );
    424428                                default:
     
    453457EXIT:
    454458        __cfaabi_dbg_print_safe( "Kernel : Preemption thread stopping\n" );
    455         return NULL;
     459        return 0p;
    456460}
    457461
     
    466470        sigset_t oldset;
    467471        int ret;
    468         ret = pthread_sigmask(0, NULL, &oldset);
     472        ret = pthread_sigmask(0, 0p, &oldset);
    469473        if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
    470474
  • libcfa/src/concurrency/thread.cfa

    r807a632 r3b56166  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Mar 30 17:19:52 2018
    13 // Update Count     : 8
     12// Last Modified On : Wed Dec  4 09:17:49 2019
     13// Update Count     : 9
    1414//
    1515
     
    3333// Thread ctors and dtors
    3434void ?{}(thread_desc & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
    35         context{ NULL, NULL };
     35        context{ 0p, 0p };
    3636        self_cor{ name, storage, storageSize };
    3737        state = Start;
     
    4141        self_mon_p = &self_mon;
    4242        curr_cluster = &cl;
    43         next = NULL;
     43        next = 0p;
    4444
    45         node.next = NULL;
    46         node.prev = NULL;
     45        node.next = 0p;
     46        node.prev = 0p;
    4747        doregister(curr_cluster, this);
    4848
     
    5858void ?{}( scoped(T)& this ) with( this ) {
    5959        handle{};
    60         __thrd_start(handle);
     60        __thrd_start(handle, main);
    6161}
    6262
     
    6464void ?{}( scoped(T)& this, P params ) with( this ) {
    6565        handle{ params };
    66         __thrd_start(handle);
     66        __thrd_start(handle, main);
    6767}
    6868
     
    7575// Starting and stopping threads
    7676forall( dtype T | is_thread(T) )
    77 void __thrd_start( T& this ) {
     77void __thrd_start( T & this, void (*main_p)(T &) ) {
    7878        thread_desc * this_thrd = get_thread(this);
    7979        thread_desc * curr_thrd = TL_GET( this_thread );
    8080
    8181        disable_interrupts();
    82         CtxStart(&this, CtxInvokeThread);
     82        CtxStart(main_p, get_coroutine(this), this, CtxInvokeThread);
     83
    8384        this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
    8485        verify( this_thrd->context.SP );
    85         CtxSwitch( &curr_thrd->context, &this_thrd->context );
     86        // CtxSwitch( &curr_thrd->context, &this_thrd->context );
    8687
    8788        ScheduleThread(this_thrd);
    8889        enable_interrupts( __cfaabi_dbg_ctx );
    89 }
    90 
    91 extern "C" {
    92         // KERNEL ONLY
    93         void __finish_creation(thread_desc * this) {
    94                 // set new coroutine that the processor is executing
    95                 // and context switch to it
    96                 verify( kernelTLS.this_thread != this );
    97                 verify( kernelTLS.this_thread->context.SP );
    98                 CtxSwitch( &this->context, &kernelTLS.this_thread->context );
    99         }
    10090}
    10191
  • libcfa/src/concurrency/thread.hfa

    r807a632 r3b56166  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jun 21 17:51:33 2019
    13 // Update Count     : 5
     12// Last Modified On : Wed Dec  4 09:18:14 2019
     13// Update Count     : 6
    1414//
    1515
     
    5454
    5555forall( dtype T | is_thread(T) )
    56 void __thrd_start( T & this );
     56void __thrd_start( T & this, void (*)(T &) );
    5757
    5858//-----------------------------------------------------------------------------
     
    6161void ^?{}(thread_desc & this);
    6262
    63 static inline void ?{}(thread_desc & this)                                                                  { this{ "Anonymous Thread", *mainCluster, NULL, 65000 }; }
    64 static inline void ?{}(thread_desc & this, size_t stackSize )                                               { this{ "Anonymous Thread", *mainCluster, NULL, stackSize }; }
     63static inline void ?{}(thread_desc & this)                                                                  { this{ "Anonymous Thread", *mainCluster, 0p, 65000 }; }
     64static inline void ?{}(thread_desc & this, size_t stackSize )                                               { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; }
    6565static inline void ?{}(thread_desc & this, void * storage, size_t storageSize )                             { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; }
    66 static inline void ?{}(thread_desc & this, struct cluster & cl )                                            { this{ "Anonymous Thread", cl, NULL, 65000 }; }
    67 static inline void ?{}(thread_desc & this, struct cluster & cl, size_t stackSize )                          { this{ "Anonymous Thread", cl, NULL, stackSize }; }
     66static inline void ?{}(thread_desc & this, struct cluster & cl )                                            { this{ "Anonymous Thread", cl, 0p, 65000 }; }
     67static inline void ?{}(thread_desc & this, struct cluster & cl, size_t stackSize )                          { this{ "Anonymous Thread", cl, 0p, stackSize }; }
    6868static inline void ?{}(thread_desc & this, struct cluster & cl, void * storage, size_t storageSize )        { this{ "Anonymous Thread", cl, storage, storageSize }; }
    69 static inline void ?{}(thread_desc & this, const char * const name)                                         { this{ name, *mainCluster, NULL, 65000 }; }
    70 static inline void ?{}(thread_desc & this, const char * const name, struct cluster & cl )                   { this{ name, cl, NULL, 65000 }; }
    71 static inline void ?{}(thread_desc & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, NULL, stackSize }; }
     69static inline void ?{}(thread_desc & this, const char * const name)                                         { this{ name, *mainCluster, 0p, 65000 }; }
     70static inline void ?{}(thread_desc & this, const char * const name, struct cluster & cl )                   { this{ name, cl, 0p, 65000 }; }
     71static inline void ?{}(thread_desc & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; }
    7272
    7373//-----------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.