Changes in / [d43cd01:653f2c7]


Ignore:
Location:
src
Files:
1 deleted
17 edited

Legend:

Unmodified
Added
Removed
  • src/benchmark/CorCtxSwitch.c

    rd43cd01 r653f2c7  
    3131
    3232        StartTime = Time();
     33        // for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) {
     34        //      resume( this_coroutine() );
     35        //      // resume( &s );       
     36        // }
    3337        resumer( &s, NoOfTimes );
    3438        EndTime = Time();
  • src/benchmark/csv-data.c

    rd43cd01 r653f2c7  
    3838
    3939        StartTime = Time();
     40        // for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) {
     41        //      resume( this_coroutine() );
     42        //      // resume( &s );
     43        // }
    4044        resumer( &s, NoOfTimes );
    4145        EndTime = Time();
  • src/libcfa/concurrency/alarm.c

    rd43cd01 r653f2c7  
    1616
    1717extern "C" {
    18 #include <errno.h>
    19 #include <stdio.h>
    20 #include <string.h>
    2118#include <time.h>
    22 #include <unistd.h>
    2319#include <sys/time.h>
    2420}
     
    2622#include "alarm.h"
    2723#include "kernel_private.h"
    28 #include "libhdr.h"
    2924#include "preemption.h"
    3025
     
    3631        timespec curr;
    3732        clock_gettime( CLOCK_REALTIME, &curr );
    38         __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
    39         LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
    40         return curr_time;
     33        return ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
    4134}
    4235
    4336void __kernel_set_timer( __cfa_time_t alarm ) {
    44         LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
    4537        itimerval val;
    4638        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
     
    7971}
    8072
    81 LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
    82         alarm_node_t ** it = &this->head;
    83         while( (*it) ) {
    84                 it = &(*it)->next;
    85         }
    86 
    87         return it == this->tail;
    88 })
    89 
    9073static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
    91         verify( !n->next );
     74        assert( !n->next );
    9275        if( p == this->tail ) {
    9376                this->tail = &n->next;
     
    9780        }
    9881        *p = n;
    99 
    100         verify( validate( this ) );
    10182}
    10283
     
    10889
    10990        insert_at( this, n, it );
    110 
    111         verify( validate( this ) );
    11291}
    11392
     
    121100                head->next = NULL;
    122101        }
    123         verify( validate( this ) );
    124102        return head;
    125103}
     
    127105static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
    128106        verify( it );
    129         verify( (*it) == n );
     107        verify( (*it)->next == n );
    130108
    131         (*it) = n->next;
     109        (*it)->next = n->next;
    132110        if( !n-> next ) {
    133111                this->tail = it;
    134112        }
    135113        n->next = NULL;
    136 
    137         verify( validate( this ) );
    138114}
    139115
    140116static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
    141117        alarm_node_t ** it = &this->head;
    142         while( (*it) && (*it) != n ) {
     118        while( (*it) && (*it)->next != n ) {
    143119                it = &(*it)->next;
    144120        }
    145121
    146         verify( validate( this ) );
    147 
    148122        if( *it ) { remove_at( this, n, it ); }
    149 
    150         verify( validate( this ) );
    151123}
    152124
    153125void register_self( alarm_node_t * this ) {
    154126        disable_interrupts();
    155         verify( !systemProcessor->pending_alarm );
    156         lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ );
     127        assert( !systemProcessor->pending_alarm );
     128        lock( &systemProcessor->alarm_lock );
    157129        {
    158                 verify( validate( &systemProcessor->alarms ) );
    159                 bool first = !systemProcessor->alarms.head;
    160 
    161130                insert( &systemProcessor->alarms, this );
    162131                if( systemProcessor->pending_alarm ) {
    163132                        tick_preemption();
    164133                }
    165                 if( first ) {
    166                         __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
    167                 }
    168134        }
    169135        unlock( &systemProcessor->alarm_lock );
    170136        this->set = true;
    171         enable_interrupts( __PRETTY_FUNCTION__ );
     137        enable_interrupts();
    172138}
    173139
    174140void unregister_self( alarm_node_t * this ) {
    175         // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
    176141        disable_interrupts();
    177         lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ );
    178         {
    179                 verify( validate( &systemProcessor->alarms ) );
    180                 remove( &systemProcessor->alarms, this );
    181         }
     142        lock( &systemProcessor->alarm_lock );
     143        remove( &systemProcessor->alarms, this );
    182144        unlock( &systemProcessor->alarm_lock );
    183145        disable_interrupts();
    184146        this->set = false;
    185         // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
    186147}
  • src/libcfa/concurrency/coroutine

    rd43cd01 r653f2c7  
    6363
    6464// Get current coroutine
    65 extern volatile thread_local coroutine_desc * this_coroutine;
     65coroutine_desc * this_coroutine(void);
    6666
    6767// Private wrappers for context switch and stack creation
     
    7171// Suspend implementation inlined for performance
    7272static inline void suspend() {
    73         coroutine_desc * src = this_coroutine;          // optimization
     73        coroutine_desc * src = this_coroutine();                // optimization
    7474
    7575        assertf( src->last != 0,
     
    8888forall(dtype T | is_coroutine(T))
    8989static inline void resume(T * cor) {
    90         coroutine_desc * src = this_coroutine;          // optimization
     90        coroutine_desc * src = this_coroutine();                // optimization
    9191        coroutine_desc * dst = get_coroutine(cor);
    9292
     
    112112
    113113static inline void resume(coroutine_desc * dst) {
    114         coroutine_desc * src = this_coroutine;          // optimization
     114        coroutine_desc * src = this_coroutine();                // optimization
    115115
    116116        // not resuming self ?
  • src/libcfa/concurrency/coroutine.c

    rd43cd01 r653f2c7  
    3232#include "invoke.h"
    3333
    34 extern volatile thread_local processor * this_processor;
     34extern thread_local processor * this_processor;
    3535
    3636//-----------------------------------------------------------------------------
     
    106106
    107107        // set state of current coroutine to inactive
    108         src->state = src->state == Halted ? Halted : Inactive;
     108        src->state = Inactive;
    109109
    110110        // set new coroutine that task is executing
    111         this_coroutine = dst;
     111        this_processor->current_coroutine = dst;
    112112
    113113        // context switch to specified coroutine
    114         assert( src->stack.context );
    115114        CtxSwitch( src->stack.context, dst->stack.context );
    116115        // when CtxSwitch returns we are back in the src coroutine             
  • src/libcfa/concurrency/invoke.c

    rd43cd01 r653f2c7  
    2929
    3030extern void __suspend_internal(void);
    31 extern void __leave_thread_monitor( struct thread_desc * this );
    32 extern void disable_interrupts();
    33 extern void enable_interrupts( const char * );
     31extern void __leave_monitor_desc( struct monitor_desc * this );
    3432
    3533void CtxInvokeCoroutine(
    36       void (*main)(void *),
    37       struct coroutine_desc *(*get_coroutine)(void *),
     34      void (*main)(void *), 
     35      struct coroutine_desc *(*get_coroutine)(void *), 
    3836      void *this
    3937) {
     
    5856
    5957void CtxInvokeThread(
    60       void (*dtor)(void *),
    61       void (*main)(void *),
    62       struct thread_desc *(*get_thread)(void *),
     58      void (*dtor)(void *), 
     59      void (*main)(void *), 
     60      struct thread_desc *(*get_thread)(void *), 
    6361      void *this
    6462) {
    65       // First suspend, once the thread arrives here,
    66       // the function pointer to main can be invalidated without risk
    6763      __suspend_internal();
    6864
    69       // Fetch the thread handle from the user defined thread structure
    7065      struct thread_desc* thrd = get_thread( this );
     66      struct coroutine_desc* cor = &thrd->cor;
     67      struct monitor_desc* mon = &thrd->mon;
     68      cor->state = Active;
    7169
    72       // Officially start the thread by enabling preemption
    73       enable_interrupts( __PRETTY_FUNCTION__ );
    74 
    75       // Call the main of the thread
     70      // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
    7671      main( this );
    7772
    78       // To exit a thread we must :
    79       // 1 - Mark it as halted
    80       // 2 - Leave its monitor
    81       // 3 - Disable the interupts
    82       // The order of these 3 operations is very important
    83       __leave_thread_monitor( thrd );
     73      __leave_monitor_desc( mon );
    8474
    8575      //Final suspend, should never return
     
    9080
    9181void CtxStart(
    92       void (*main)(void *),
    93       struct coroutine_desc *(*get_coroutine)(void *),
    94       void *this,
     82      void (*main)(void *), 
     83      struct coroutine_desc *(*get_coroutine)(void *), 
     84      void *this, 
    9585      void (*invoke)(void *)
    9686) {
     
    118108        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
    119109      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    120       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
     110      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7 
    121111
    122112#elif defined( __x86_64__ )
     
    138128      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
    139129      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    140       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
     130      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7 
    141131#else
    142132      #error Only __i386__ and __x86_64__ is supported for threads in cfa
  • src/libcfa/concurrency/invoke.h

    rd43cd01 r653f2c7  
    3131      struct spinlock {
    3232            volatile int lock;
    33             #ifdef __CFA_DEBUG__
    34                   const char * prev;
    35             #endif
    3633      };
    3734
     
    8683            struct __thread_queue_t entry_queue;      // queue of threads that are blocked waiting for the monitor
    8784            struct __condition_stack_t signal_stack;  // stack of conditions to run next once we exit the monitor
     85            struct monitor_desc * stack_owner;        // if bulk acquiring was used we need to synchronize signals with an other monitor
    8886            unsigned int recursion;                   // monitor routines can be called recursively, we need to keep track of that
    8987      };
  • src/libcfa/concurrency/kernel

    rd43cd01 r653f2c7  
    2828//-----------------------------------------------------------------------------
    2929// Locks
    30 bool try_lock( spinlock *
    31         #ifdef __CFA_DEBUG__
    32                 , const char * caller
    33         #endif
    34 );
    35 
    36 void lock( spinlock *
    37         #ifdef __CFA_DEBUG__
    38                 , const char * caller
    39         #endif
    40 );
    41 
     30bool try_lock( spinlock * );
     31void lock( spinlock * );
    4232void unlock( spinlock * );
    4333
     
    8878        struct processorCtx_t * runner;
    8979        cluster * cltr;
     80        coroutine_desc * current_coroutine;
     81        thread_desc * current_thread;
    9082        pthread_t kernel_thread;
    9183       
     
    9890        unsigned int preemption;
    9991
     92        unsigned short disable_preempt_count;
     93
    10094        bool pending_preemption;
    101 
    102         char * last_enable;
    10395};
    10496
  • src/libcfa/concurrency/kernel.c

    rd43cd01 r653f2c7  
    5959// Global state
    6060
    61 volatile thread_local processor * this_processor;
    62 volatile thread_local coroutine_desc * this_coroutine;
    63 volatile thread_local thread_desc * this_thread;
    64 volatile thread_local unsigned short disable_preempt_count = 1;
     61thread_local processor * this_processor;
     62
     63coroutine_desc * this_coroutine(void) {
     64        return this_processor->current_coroutine;
     65}
     66
     67thread_desc * this_thread(void) {
     68        return this_processor->current_thread;
     69}
    6570
    6671//-----------------------------------------------------------------------------
    6772// Main thread construction
    6873struct current_stack_info_t {
    69         machine_context_t ctx;
     74        machine_context_t ctx; 
    7075        unsigned int size;              // size of stack
    7176        void *base;                             // base of stack
     
    101106
    102107void ?{}( coroutine_desc * this, current_stack_info_t * info) {
    103         (&this->stack){ info };
     108        (&this->stack){ info }; 
    104109        this->name = "Main Thread";
    105110        this->errno_ = 0;
     
    131136void ?{}(processor * this, cluster * cltr) {
    132137        this->cltr = cltr;
     138        this->current_coroutine = NULL;
     139        this->current_thread = NULL;
    133140        (&this->terminated){};
    134141        this->is_terminated = false;
    135142        this->preemption_alarm = NULL;
    136143        this->preemption = default_preemption();
     144        this->disable_preempt_count = 1;                //Start with interrupts disabled
    137145        this->pending_preemption = false;
    138146
     
    142150void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
    143151        this->cltr = cltr;
     152        this->current_coroutine = NULL;
     153        this->current_thread = NULL;
    144154        (&this->terminated){};
    145155        this->is_terminated = false;
    146         this->preemption_alarm = NULL;
    147         this->preemption = default_preemption();
     156        this->disable_preempt_count = 0;
    148157        this->pending_preemption = false;
    149         this->kernel_thread = pthread_self();
    150158
    151159        this->runner = runner;
    152         LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner);
     160        LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
    153161        runner{ this };
    154162}
    155 
    156 LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
    157163
    158164void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
     
    162168
    163169        (&this->proc){ cltr, runner };
    164 
    165         verify( validate( &this->alarms ) );
    166170}
    167171
     
    180184
    181185void ^?{}(cluster * this) {
    182 
     186       
    183187}
    184188
     
    199203
    200204                thread_desc * readyThread = NULL;
    201                 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
     205                for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 
    202206                {
    203207                        readyThread = nextThread( this->cltr );
     
    205209                        if(readyThread)
    206210                        {
    207                                 verify( disable_preempt_count > 0 );
    208 
    209211                                runThread(this, readyThread);
    210 
    211                                 verify( disable_preempt_count > 0 );
    212212
    213213                                //Some actions need to be taken from the kernel
     
    229229}
    230230
    231 // runThread runs a thread by context switching
    232 // from the processor coroutine to the target thread
     231// runThread runs a thread by context switching 
     232// from the processor coroutine to the target thread 
    233233void runThread(processor * this, thread_desc * dst) {
    234234        coroutine_desc * proc_cor = get_coroutine(this->runner);
    235235        coroutine_desc * thrd_cor = get_coroutine(dst);
    236 
     236       
    237237        //Reset the terminating actions here
    238238        this->finish.action_code = No_Action;
    239239
    240240        //Update global state
    241         this_thread = dst;
     241        this->current_thread = dst;
    242242
    243243        // Context Switch to the thread
     
    246246}
    247247
    248 // Once a thread has finished running, some of
     248// Once a thread has finished running, some of 
    249249// its final actions must be executed from the kernel
    250250void finishRunning(processor * this) {
     
    256256        }
    257257        else if( this->finish.action_code == Release_Schedule ) {
    258                 unlock( this->finish.lock );
     258                unlock( this->finish.lock );           
    259259                ScheduleThread( this->finish.thrd );
    260260        }
     
    289289        processor * proc = (processor *) arg;
    290290        this_processor = proc;
    291         this_coroutine = NULL;
    292         this_thread = NULL;
    293         disable_preempt_count = 1;
    294291        // SKULLDUGGERY: We want to create a context for the processor coroutine
    295292        // which is needed for the 2-step context switch. However, there is no reason
    296         // to waste the perfectly valid stack create by pthread.
     293        // to waste the perfectly valid stack create by pthread. 
    297294        current_stack_info_t info;
    298295        machine_context_t ctx;
     
    303300
    304301        //Set global state
    305         this_coroutine = &proc->runner->__cor;
    306         this_thread = NULL;
     302        proc->current_coroutine = &proc->runner->__cor;
     303        proc->current_thread = NULL;
    307304
    308305        //We now have a proper context from which to schedule threads
    309306        LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
    310307
    311         // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
    312         // resume it to start it like it normally would, it will just context switch
    313         // back to here. Instead directly call the main since we already are on the
     308        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't 
     309        // resume it to start it like it normally would, it will just context switch 
     310        // back to here. Instead directly call the main since we already are on the 
    314311        // appropriate stack.
    315312        proc_cor_storage.__cor.state = Active;
     
    318315
    319316        // Main routine of the core returned, the core is now fully terminated
    320         LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
     317        LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner); 
    321318
    322319        return NULL;
     
    325322void start(processor * this) {
    326323        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    327 
    328         // SIGALRM must only be caught by the system processor
    329         sigset_t old_mask;
    330         bool is_system_proc = this_processor == &systemProcessor->proc;
    331         if ( is_system_proc ) {
    332                 // Child kernel-thread inherits the signal mask from the parent kernel-thread. So one special case for the
    333                 // system processor creating the user processor => toggle the blocking SIGALRM on system processor, create user
    334                 // processor, and toggle back (below) previous signal mask of the system processor.
    335 
    336                 sigset_t new_mask;
    337                 sigemptyset( &new_mask );
    338                 sigemptyset( &old_mask );
    339                 sigaddset( &new_mask, SIGALRM );
    340 
    341                 if ( sigprocmask( SIG_BLOCK, &new_mask, &old_mask ) == -1 ) {
    342                         abortf( "internal error, sigprocmask" );
    343                 }
    344 
    345                 assert( ! sigismember( &old_mask, SIGALRM ) );
    346         }
    347 
     324       
    348325        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
    349326
    350         // Toggle back previous signal mask of system processor.
    351         if ( is_system_proc ) {
    352                 if ( sigprocmask( SIG_SETMASK, &old_mask, NULL ) == -1 ) {
    353                         abortf( "internal error, sigprocmask" );
    354                 } // if
    355         } // if
    356 
    357         LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
     327        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
    358328}
    359329
     
    361331// Scheduler routines
    362332void ScheduleThread( thread_desc * thrd ) {
    363         // if( !thrd ) return;
    364         assert( thrd );
    365         assert( thrd->cor.state != Halted );
    366 
    367         verify( disable_preempt_count > 0 );
     333        if( !thrd ) return;
    368334
    369335        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    370 
    371         lock( &systemProcessor->proc.cltr->lock, __PRETTY_FUNCTION__ );
     336       
     337        lock( &systemProcessor->proc.cltr->lock );
    372338        append( &systemProcessor->proc.cltr->ready_queue, thrd );
    373339        unlock( &systemProcessor->proc.cltr->lock );
    374 
    375         verify( disable_preempt_count > 0 );
    376340}
    377341
    378342thread_desc * nextThread(cluster * this) {
    379         verify( disable_preempt_count > 0 );
    380         lock( &this->lock, __PRETTY_FUNCTION__ );
     343        lock( &this->lock );
    381344        thread_desc * head = pop_head( &this->ready_queue );
    382345        unlock( &this->lock );
    383         verify( disable_preempt_count > 0 );
    384346        return head;
    385347}
    386348
    387 void BlockInternal() {
    388         disable_interrupts();
    389         verify( disable_preempt_count > 0 );
     349void ScheduleInternal() {
    390350        suspend();
    391         verify( disable_preempt_count > 0 );
    392         enable_interrupts( __PRETTY_FUNCTION__ );
    393 }
    394 
    395 void BlockInternal( spinlock * lock ) {
    396         disable_interrupts();
     351}
     352
     353void ScheduleInternal( spinlock * lock ) {
    397354        this_processor->finish.action_code = Release;
    398355        this_processor->finish.lock = lock;
    399 
    400         verify( disable_preempt_count > 0 );
    401356        suspend();
    402         verify( disable_preempt_count > 0 );
    403 
    404         enable_interrupts( __PRETTY_FUNCTION__ );
    405 }
    406 
    407 void BlockInternal( thread_desc * thrd ) {
    408         disable_interrupts();
    409         assert( thrd->cor.state != Halted );
     357}
     358
     359void ScheduleInternal( thread_desc * thrd ) {
    410360        this_processor->finish.action_code = Schedule;
    411361        this_processor->finish.thrd = thrd;
    412 
    413         verify( disable_preempt_count > 0 );
    414362        suspend();
    415         verify( disable_preempt_count > 0 );
    416 
    417         enable_interrupts( __PRETTY_FUNCTION__ );
    418 }
    419 
    420 void BlockInternal( spinlock * lock, thread_desc * thrd ) {
    421         disable_interrupts();
     363}
     364
     365void ScheduleInternal( spinlock * lock, thread_desc * thrd ) {
    422366        this_processor->finish.action_code = Release_Schedule;
    423367        this_processor->finish.lock = lock;
    424368        this_processor->finish.thrd = thrd;
    425 
    426         verify( disable_preempt_count > 0 );
    427369        suspend();
    428         verify( disable_preempt_count > 0 );
    429 
    430         enable_interrupts( __PRETTY_FUNCTION__ );
    431 }
    432 
    433 void BlockInternal(spinlock ** locks, unsigned short count) {
    434         disable_interrupts();
     370}
     371
     372void ScheduleInternal(spinlock ** locks, unsigned short count) {
    435373        this_processor->finish.action_code = Release_Multi;
    436374        this_processor->finish.locks = locks;
    437375        this_processor->finish.lock_count = count;
    438 
    439         verify( disable_preempt_count > 0 );
    440376        suspend();
    441         verify( disable_preempt_count > 0 );
    442 
    443         enable_interrupts( __PRETTY_FUNCTION__ );
    444 }
    445 
    446 void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
    447         disable_interrupts();
     377}
     378
     379void ScheduleInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
    448380        this_processor->finish.action_code = Release_Multi_Schedule;
    449381        this_processor->finish.locks = locks;
     
    451383        this_processor->finish.thrds = thrds;
    452384        this_processor->finish.thrd_count = thrd_count;
    453 
    454         verify( disable_preempt_count > 0 );
    455385        suspend();
    456         verify( disable_preempt_count > 0 );
    457 
    458         enable_interrupts( __PRETTY_FUNCTION__ );
    459386}
    460387
     
    465392// Kernel boot procedures
    466393void kernel_startup(void) {
    467         LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
     394        LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");   
    468395
    469396        // Start by initializing the main thread
    470         // SKULLDUGGERY: the mainThread steals the process main thread
     397        // SKULLDUGGERY: the mainThread steals the process main thread 
    471398        // which will then be scheduled by the systemProcessor normally
    472399        mainThread = (thread_desc *)&mainThread_storage;
     
    476403        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
    477404
     405        // Enable preemption
     406        kernel_start_preemption();
     407
    478408        // Initialize the system cluster
    479409        systemCluster = (cluster *)&systemCluster_storage;
     
    487417        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
    488418
    489         // Add the main thread to the ready queue
     419        // Add the main thread to the ready queue 
    490420        // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
    491421        ScheduleThread(mainThread);
     
    493423        //initialize the global state variables
    494424        this_processor = &systemProcessor->proc;
    495         this_thread = mainThread;
    496         this_coroutine = &mainThread->cor;
    497         disable_preempt_count = 1;
    498 
    499         // Enable preemption
    500         kernel_start_preemption();
     425        this_processor->current_thread = mainThread;
     426        this_processor->current_coroutine = &mainThread->cor;
    501427
    502428        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
    503429        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
    504         // mainThread is on the ready queue when this call is made.
     430        // mainThread is on the ready queue when this call is made. 
    505431        resume( systemProcessor->proc.runner );
    506432
     
    509435        // THE SYSTEM IS NOW COMPLETELY RUNNING
    510436        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
    511 
    512         enable_interrupts( __PRETTY_FUNCTION__ );
    513437}
    514438
    515439void kernel_shutdown(void) {
    516440        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
    517 
    518         disable_interrupts();
    519441
    520442        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
     
    526448        // THE SYSTEM IS NOW COMPLETELY STOPPED
    527449
    528         // Disable preemption
    529         kernel_stop_preemption();
    530 
    531450        // Destroy the system processor and its context in reverse order of construction
    532451        // These were manually constructed so we need manually destroy them
     
    538457        ^(mainThread){};
    539458
    540         LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
     459        LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");   
    541460}
    542461
     
    548467        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
    549468        // the globalAbort flag is true.
    550         lock( &kernel_abort_lock, __PRETTY_FUNCTION__ );
     469        lock( &kernel_abort_lock );
    551470
    552471        // first task to abort ?
     
    554473                kernel_abort_called = true;
    555474                unlock( &kernel_abort_lock );
    556         }
     475        } 
    557476        else {
    558477                unlock( &kernel_abort_lock );
    559 
     478               
    560479                sigset_t mask;
    561480                sigemptyset( &mask );
     
    563482                sigaddset( &mask, SIGUSR1 );                    // block SIGUSR1 signals
    564483                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
    565                 _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
    566         }
    567 
    568         return this_thread;
     484                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it             
     485        }
     486
     487        return this_thread();
    569488}
    570489
     
    575494        __lib_debug_write( STDERR_FILENO, abort_text, len );
    576495
    577         if ( thrd != this_coroutine ) {
    578                 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
     496        if ( thrd != this_coroutine() ) {
     497                len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine()->name, this_coroutine() );
    579498                __lib_debug_write( STDERR_FILENO, abort_text, len );
    580         }
     499        } 
    581500        else {
    582501                __lib_debug_write( STDERR_FILENO, ".\n", 2 );
     
    586505extern "C" {
    587506        void __lib_debug_acquire() {
    588                 lock(&kernel_debug_lock, __PRETTY_FUNCTION__);
     507                lock(&kernel_debug_lock);
    589508        }
    590509
     
    606525}
    607526
    608 bool try_lock( spinlock * this, const char * caller ) {
    609         bool ret = this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
    610         this->prev = caller;
    611         return ret;
    612 }
    613 
    614 void lock( spinlock * this, const char * caller ) {
     527bool try_lock( spinlock * this ) {
     528        return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
     529}
     530
     531void lock( spinlock * this ) {
    615532        for ( unsigned int i = 1;; i += 1 ) {
    616533                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
    617534        }
    618         this->prev = caller;
    619535}
    620536
     
    631547
    632548void wait( signal_once * this ) {
    633         lock( &this->lock, __PRETTY_FUNCTION__ );
     549        lock( &this->lock );
    634550        if( !this->cond ) {
    635                 append( &this->blocked, (thread_desc*)this_thread );
    636                 BlockInternal( &this->lock );
    637         }
    638         else {
    639                 unlock( &this->lock );
    640         }
     551                append( &this->blocked, this_thread() );
     552                ScheduleInternal( &this->lock );
     553                lock( &this->lock );
     554        }
     555        unlock( &this->lock );
    641556}
    642557
    643558void signal( signal_once * this ) {
    644         lock( &this->lock, __PRETTY_FUNCTION__ );
     559        lock( &this->lock );
    645560        {
    646561                this->cond = true;
    647562
    648                 disable_interrupts();
    649563                thread_desc * it;
    650564                while( it = pop_head( &this->blocked) ) {
    651565                        ScheduleThread( it );
    652566                }
    653                 enable_interrupts( __PRETTY_FUNCTION__ );
    654567        }
    655568        unlock( &this->lock );
     
    677590                }
    678591                head->next = NULL;
    679         }
     592        }       
    680593        return head;
    681594}
     
    696609                this->top = top->next;
    697610                top->next = NULL;
    698         }
     611        }       
    699612        return top;
    700613}
  • src/libcfa/concurrency/kernel_private.h

    rd43cd01 r653f2c7  
    2727//-----------------------------------------------------------------------------
    2828// Scheduler
    29 
    30 extern "C" {
    31         void disable_interrupts();
    32         void enable_interrupts_noRF();
    33         void enable_interrupts( const char * );
    34 }
    35 
    3629void ScheduleThread( thread_desc * );
    37 static inline void WakeThread( thread_desc * thrd ) {
    38         if( !thrd ) return;
    39 
    40         disable_interrupts();
    41         ScheduleThread( thrd );
    42         enable_interrupts( __PRETTY_FUNCTION__ );
    43 }
    4430thread_desc * nextThread(cluster * this);
    4531
    46 void BlockInternal(void);
    47 void BlockInternal(spinlock * lock);
    48 void BlockInternal(thread_desc * thrd);
    49 void BlockInternal(spinlock * lock, thread_desc * thrd);
    50 void BlockInternal(spinlock ** locks, unsigned short count);
    51 void BlockInternal(spinlock ** locks, unsigned short count, thread_desc ** thrds, unsigned short thrd_count);
     32void ScheduleInternal(void);
     33void ScheduleInternal(spinlock * lock);
     34void ScheduleInternal(thread_desc * thrd);
     35void ScheduleInternal(spinlock * lock, thread_desc * thrd);
     36void ScheduleInternal(spinlock ** locks, unsigned short count);
     37void ScheduleInternal(spinlock ** locks, unsigned short count, thread_desc ** thrds, unsigned short thrd_count);
    5238
    5339//-----------------------------------------------------------------------------
     
    7460extern cluster * systemCluster;
    7561extern system_proc_t * systemProcessor;
    76 extern volatile thread_local processor * this_processor;
    77 extern volatile thread_local coroutine_desc * this_coroutine;
    78 extern volatile thread_local thread_desc * this_thread;
    79 extern volatile thread_local unsigned short disable_preempt_count;
     62extern thread_local processor * this_processor;
     63
     64static inline void disable_interrupts() {
     65        __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, 1, __ATOMIC_SEQ_CST );
     66        assert( prev != (unsigned short) -1 );
     67}
     68
     69static inline void enable_interrupts_noRF() {
     70        __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST );
     71        verify( prev != (unsigned short) 0 );
     72}
     73
     74static inline void enable_interrupts() {
     75        __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST );
     76        verify( prev != (unsigned short) 0 );
     77        if( prev == 1 && this_processor->pending_preemption ) {
     78                ScheduleInternal( this_processor->current_thread );
     79                this_processor->pending_preemption = false;
     80        }
     81}
    8082
    8183//-----------------------------------------------------------------------------
  • src/libcfa/concurrency/monitor

    rd43cd01 r653f2c7  
    2626static inline void ?{}(monitor_desc * this) {
    2727        this->owner = NULL;
     28        this->stack_owner = NULL;
    2829        this->recursion = 0;
    2930}
  • src/libcfa/concurrency/monitor.c

    rd43cd01 r653f2c7  
    4444
    4545extern "C" {
    46         void __enter_monitor_desc( monitor_desc * this ) {
    47                 lock( &this->lock, __PRETTY_FUNCTION__ );
    48                 thread_desc * thrd = this_thread;
    49 
    50                 // LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
     46        void __enter_monitor_desc(monitor_desc * this) {
     47                lock( &this->lock );
     48                thread_desc * thrd = this_thread();
     49
     50                LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
    5151
    5252                if( !this->owner ) {
     
    6262                        //Some one else has the monitor, wait in line for it
    6363                        append( &this->entry_queue, thrd );
    64                         // LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
    65                         BlockInternal( &this->lock );
    66 
    67                         //BlockInternal will unlock spinlock, no need to unlock ourselves
     64                        LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
     65                        ScheduleInternal( &this->lock );
     66
     67                        //ScheduleInternal will unlock spinlock, no need to unlock ourselves
    6868                        return;
    6969                }
     
    7575        // leave pseudo code :
    7676        //      TODO
    77         void __leave_monitor_desc( monitor_desc * this ) {
    78                 lock( &this->lock, __PRETTY_FUNCTION__ );
    79 
    80                 // LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i). ", this_thread, this, this->owner, this->recursion);
    81                 verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
     77        void __leave_monitor_desc(monitor_desc * this) {
     78                lock( &this->lock );
     79
     80                LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
     81                verifyf( this_thread() == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread(), this->owner, this->recursion );
    8282
    8383                //Leaving a recursion level, decrement the counter
     
    9696                unlock( &this->lock );
    9797
    98                 // LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
     98                LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
    9999
    100100                //We need to wake-up the thread
    101                 WakeThread( new_owner );
    102         }
    103 
    104         void __leave_thread_monitor( thread_desc * thrd ) {
    105                 monitor_desc * this = &thrd->mon;
    106                 lock( &this->lock, __PRETTY_FUNCTION__ );
    107 
    108                 disable_interrupts();
    109 
    110                 thrd->cor.state = Halted;
    111 
    112                 verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
    113 
    114                 //Leaving a recursion level, decrement the counter
    115                 this->recursion -= 1;
    116 
    117                 //If we haven't left the last level of recursion
    118                 //it means we don't need to do anything
    119                 if( this->recursion != 0) {
    120                         unlock( &this->lock );
    121                         return;
    122                 }
    123 
    124                 thread_desc * new_owner = next_thread( this );
    125 
    126                 //We can now let other threads in safely
    127                 unlock( &this->lock );
    128 
    129                 //We need to wake-up the thread
    130                 if( new_owner) ScheduleThread( new_owner );
     101                ScheduleThread( new_owner );
    131102        }
    132103}
     
    150121        enter( this->m, this->count );
    151122
    152         this->prev_mntrs = this_thread->current_monitors;
    153         this->prev_count = this_thread->current_monitor_count;
    154 
    155         this_thread->current_monitors      = m;
    156         this_thread->current_monitor_count = count;
     123        this->prev_mntrs = this_thread()->current_monitors;
     124        this->prev_count = this_thread()->current_monitor_count;
     125
     126        this_thread()->current_monitors      = m;
     127        this_thread()->current_monitor_count = count;
    157128}
    158129
     
    160131        leave( this->m, this->count );
    161132
    162         this_thread->current_monitors      = this->prev_mntrs;
    163         this_thread->current_monitor_count = this->prev_count;
     133        this_thread()->current_monitors      = this->prev_mntrs;
     134        this_thread()->current_monitor_count = this->prev_count;
    164135}
    165136
     
    199170        unsigned short count = this->monitor_count;
    200171        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
    201         spinlock *   locks     [ count ];               //We need to pass-in an array of locks to BlockInternal
     172        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to ScheduleInternal
    202173
    203174        LIB_DEBUG_PRINT_SAFE("count %i\n", count);
    204175
    205         __condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
     176        __condition_node_t waiter = { this_thread(), count, user_info };
    206177
    207178        __condition_criterion_t criteria[count];
     
    237208
    238209        // Everything is ready to go to sleep
    239         BlockInternal( locks, count, threads, thread_count );
     210        ScheduleInternal( locks, count, threads, thread_count );
    240211
    241212
     
    263234        //Some more checking in debug
    264235        LIB_DEBUG_DO(
    265                 thread_desc * this_thrd = this_thread;
     236                thread_desc * this_thrd = this_thread();
    266237                if ( this->monitor_count != this_thrd->current_monitor_count ) {
    267238                        abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->current_monitor_count );
     
    310281        unsigned short count = this->monitor_count;
    311282        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
    312         spinlock *   locks     [ count ];               //We need to pass-in an array of locks to BlockInternal
     283        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to ScheduleInternal
    313284
    314285        lock_all( this->monitors, locks, count );
    315286
    316287        //create creteria
    317         __condition_node_t waiter = { (thread_desc*)this_thread, count, 0 };
     288        __condition_node_t waiter = { this_thread(), count, 0 };
    318289
    319290        __condition_criterion_t criteria[count];
     
    338309
    339310        //Everything is ready to go to sleep
    340         BlockInternal( locks, count, &signallee, 1 );
     311        ScheduleInternal( locks, count, &signallee, 1 );
    341312
    342313
     
    364335// Internal scheduling
    365336void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) {
    366         // thread_desc * this = this_thread;
     337        // thread_desc * this = this_thread();
    367338
    368339        // unsigned short count = this->current_monitor_count;
    369340        // unsigned int recursions[ count ];            //Save the current recursion levels to restore them later
    370         // spinlock *   locks     [ count ];            //We need to pass-in an array of locks to BlockInternal
     341        // spinlock *   locks     [ count ];            //We need to pass-in an array of locks to ScheduleInternal
    371342
    372343        // lock_all( this->current_monitors, locks, count );
     
    377348
    378349        // // // Everything is ready to go to sleep
    379         // // BlockInternal( locks, count, threads, thread_count );
     350        // // ScheduleInternal( locks, count, threads, thread_count );
    380351
    381352
     
    422393static inline void lock_all( spinlock ** locks, unsigned short count ) {
    423394        for( int i = 0; i < count; i++ ) {
    424                 lock( locks[i], __PRETTY_FUNCTION__ );
     395                lock( locks[i] );
    425396        }
    426397}
     
    429400        for( int i = 0; i < count; i++ ) {
    430401                spinlock * l = &source[i]->lock;
    431                 lock( l, __PRETTY_FUNCTION__ );
     402                lock( l );
    432403                if(locks) locks[i] = l;
    433404        }
     
    486457
    487458static inline void brand_condition( condition * this ) {
    488         thread_desc * thrd = this_thread;
     459        thread_desc * thrd = this_thread();
    489460        if( !this->monitors ) {
    490461                LIB_DEBUG_PRINT_SAFE("Branding\n");
  • src/libcfa/concurrency/preemption.c

    rd43cd01 r653f2c7  
    1717#include "preemption.h"
    1818
    19 
    2019extern "C" {
    21 #include <errno.h>
    22 #include <execinfo.h>
    23 #define __USE_GNU
    2420#include <signal.h>
    25 #undef __USE_GNU
    26 #include <stdio.h>
    27 #include <string.h>
    28 #include <unistd.h>
    2921}
    3022
    31 
    32 #ifdef __USE_STREAM__
    33 #include "fstream"
    34 #endif
    35 #include "libhdr.h"
    36 
    37 #define __CFA_DEFAULT_PREEMPTION__ 10000
     23#define __CFA_DEFAULT_PREEMPTION__ 10
    3824
    3925__attribute__((weak)) unsigned int default_preemption() {
     
    4127}
    4228
    43 #define __CFA_SIGCXT__ ucontext_t *
    44 #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt
    45 
    4629static void preempt( processor   * this );
    4730static void timeout( thread_desc * this );
    48 
    49 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
    50 void sigHandler_alarm    ( __CFA_SIGPARMS__ );
    51 void sigHandler_segv     ( __CFA_SIGPARMS__ );
    52 void sigHandler_abort    ( __CFA_SIGPARMS__ );
    53 
    54 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
    5531
    5632//=============================================================================================
     
    5935
    6036void kernel_start_preemption() {
    61         LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n");
    62         __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO );
    63         __kernel_sigaction( SIGALRM, sigHandler_alarm    , SA_SIGINFO );
    64         __kernel_sigaction( SIGSEGV, sigHandler_segv     , SA_SIGINFO );
    65         __kernel_sigaction( SIGBUS , sigHandler_segv     , SA_SIGINFO );
    66         // __kernel_sigaction( SIGABRT, sigHandler_abort    , SA_SIGINFO );
     37
    6738}
    6839
    69 void kernel_stop_preemption() {
    70         //Block all signals, we are no longer in a position to handle them
    71         sigset_t mask;
    72         sigfillset( &mask );
    73         sigprocmask( SIG_BLOCK, &mask, NULL );
    74         LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");
    75 
    76         // assert( !systemProcessor->alarms.head );
    77         // assert( systemProcessor->alarms.tail == &systemProcessor->alarms.head );
    78 }
    79 
    80 LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
    81 
    8240void tick_preemption() {
    83         // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ticking preemption\n" );
    84 
    8541        alarm_list_t * alarms = &systemProcessor->alarms;
    8642        __cfa_time_t currtime = __kernel_get_time();
    8743        while( alarms->head && alarms->head->alarm < currtime ) {
    8844                alarm_node_t * node = pop(alarms);
    89                 // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking %p\n", node );
    90 
    9145                if( node->kernel_alarm ) {
    9246                        preempt( node->proc );
     
    9650                }
    9751
    98                 verify( validate( alarms ) );
    99 
    10052                if( node->period > 0 ) {
    101                         node->alarm = currtime + node->period;
     53                        node->alarm += node->period;
    10254                        insert( alarms, node );
    10355                }
     
    11062                __kernel_set_timer( alarms->head->alarm - currtime );
    11163        }
    112 
    113         verify( validate( alarms ) );
    114         // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking preemption done\n" );
    11564}
    11665
    11766void update_preemption( processor * this, __cfa_time_t duration ) {
    118         LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p updating preemption to %lu\n", this, duration );
    119 
     67        //     assert( THREAD_GETMEM( disableInt ) && THREAD_GETMEM( disableIntCnt ) == 1 );
    12068        alarm_node_t * alarm = this->preemption_alarm;
    121         duration *= 1000;
    12269
    12370        // Alarms need to be enabled
     
    15097
    15198void ^?{}( preemption_scope * this ) {
    152         disable_interrupts();
    153 
    15499        update_preemption( this->proc, 0 );
    155100}
     
    159104//=============================================================================================
    160105
    161 extern "C" {
    162         void disable_interrupts() {
    163                 __attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST );
    164                 verify( new_val < (unsigned short)65_000 );
    165                 verify( new_val != (unsigned short) 0 );
    166         }
    167 
    168         void enable_interrupts_noRF() {
    169                 unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
    170                 verify( prev != (unsigned short) 0 );
    171         }
    172 
    173         void enable_interrupts( const char * func ) {
    174                 processor * proc   = this_processor;
    175                 thread_desc * thrd = this_thread;
    176                 unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
    177                 verify( prev != (unsigned short) 0 );
    178                 if( prev == 1 && proc->pending_preemption ) {
    179                         proc->pending_preemption = false;
    180                         LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Executing deferred CtxSwitch on %p\n", this_processor );
    181                         BlockInternal( thrd );
    182                         LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Executing deferred back\n" );
    183                 }
    184 
    185                 proc->last_enable = func;
    186         }
    187 }
    188 
    189 static inline void signal_unblock( int sig ) {
    190         LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p unblocking sig %i\n", this_processor, sig );
    191 
    192         sigset_t mask;
    193         sigemptyset( &mask );
    194         sigaddset( &mask, sig );
    195 
    196         if ( sigprocmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
    197             abortf( "internal error, sigprocmask" );
    198         } // if
    199 }
    200 
    201106static inline bool preemption_ready() {
    202         return disable_preempt_count == 0;
     107        return this_processor->disable_preempt_count == 0;
    203108}
    204109
     
    211116}
    212117
    213 extern "C" {
    214         __attribute__((noinline)) void __debug_break() {
    215                 pthread_kill( pthread_self(), SIGTRAP );
     118void sigHandler_ctxSwitch( __attribute__((unused)) int sig ) {
     119        if( preemption_ready() ) {
     120                ScheduleInternal( this_processor->current_thread );
     121        }
     122        else {
     123                defer_ctxSwitch();
    216124        }
    217125}
    218126
    219 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
    220         LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ctx Switch IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) );
    221 
    222         if( preemption_ready() ) {
    223                 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Blocking thread %p on %p\n", this_thread, this_processor );
    224                 signal_unblock( SIGUSR1 );
    225                 BlockInternal( (thread_desc*)this_thread );
    226                 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Back\n\n");
    227         }
    228         else {
    229                 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Defering\n" );
    230                 defer_ctxSwitch();
    231                 signal_unblock( SIGUSR1 );
    232         }
    233 }
    234 
    235 void sigHandler_alarm( __CFA_SIGPARMS__ ) {
    236         LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "\nAlarm IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) );
    237 
    238         // if( ((intptr_t)cxt->uc_mcontext.gregs[REG_RIP]) > 0xFFFFFF ) __debug_break();
    239 
    240         if( try_lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ ) ) {
     127void sigHandler_alarm( __attribute__((unused)) int sig ) {
     128        if( try_lock( &systemProcessor->alarm_lock ) ) {
    241129                tick_preemption();
    242130                unlock( &systemProcessor->alarm_lock );
     
    245133                defer_alarm();
    246134        }
    247 
    248         signal_unblock( SIGALRM );
    249 
    250         if( preemption_ready() && this_processor->pending_preemption ) {
    251                 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm IRH : Blocking thread %p on %p\n", this_thread, this_processor );
    252                 this_processor->pending_preemption = false;
    253                 BlockInternal( (thread_desc*)this_thread );
    254                 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm Switch IRH : Back\n\n");
    255         }
    256135}
    257136
    258137static void preempt( processor * this ) {
    259         // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : signalling %p\n", this );
    260 
    261         if( this != systemProcessor ) {
    262                 pthread_kill( this->kernel_thread, SIGUSR1 );
    263         }
    264         else {
    265                 defer_ctxSwitch();
    266         }
     138        pthread_kill( this->kernel_thread, SIGUSR1 );
    267139}
    268140
     
    270142        //TODO : implement waking threads
    271143}
    272 
    273 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {
    274         struct sigaction act;
    275 
    276         act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
    277         act.sa_flags = flags;
    278 
    279         // disabled during signal handler
    280         sigemptyset( &act.sa_mask );
    281         sigaddset( &act.sa_mask, sig );
    282 
    283         if ( sigaction( sig, &act, NULL ) == -1 ) {
    284                 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,
    285                         " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
    286                         sig, handler, flags, errno, strerror( errno )
    287                 );
    288                 _exit( EXIT_FAILURE );
    289         }
    290 }
    291 
    292 typedef void (*sa_handler_t)(int);
    293 
    294 static void __kernel_sigdefault( int sig ) {
    295         struct sigaction act;
    296 
    297         // act.sa_handler = SIG_DFL;
    298         act.sa_flags = 0;
    299         sigemptyset( &act.sa_mask );
    300 
    301         if ( sigaction( sig, &act, NULL ) == -1 ) {
    302                 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,
    303                         " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
    304                         sig, errno, strerror( errno )
    305                 );
    306                 _exit( EXIT_FAILURE );
    307         }
    308 }
    309 
    310 //=============================================================================================
    311 // Terminating Signals logic
    312 //=============================================================================================
    313 
    314 LIB_DEBUG_DO(
    315         static void __kernel_backtrace( int start ) {
    316                 // skip first N stack frames
    317 
    318                 enum { Frames = 50 };
    319                 void * array[Frames];
    320                 int size = backtrace( array, Frames );
    321                 char ** messages = backtrace_symbols( array, size );
    322 
    323                 // find executable name
    324                 *index( messages[0], '(' ) = '\0';
    325                 #ifdef __USE_STREAM__
    326                 serr | "Stack back trace for:" | messages[0] | endl;
    327                 #else
    328                 fprintf( stderr, "Stack back trace for: %s\n", messages[0]);
    329                 #endif
    330 
    331                 // skip last 2 stack frames after main
    332                 for ( int i = start; i < size && messages != NULL; i += 1 ) {
    333                         char * name = NULL;
    334                         char * offset_begin = NULL;
    335                         char * offset_end = NULL;
    336 
    337                         for ( char *p = messages[i]; *p; ++p ) {
    338                                 // find parantheses and +offset
    339                                 if ( *p == '(' ) {
    340                                         name = p;
    341                                 }
    342                                 else if ( *p == '+' ) {
    343                                         offset_begin = p;
    344                                 }
    345                                 else if ( *p == ')' ) {
    346                                         offset_end = p;
    347                                         break;
    348                                 }
    349                         }
    350 
    351                         // if line contains symbol print it
    352                         int frameNo = i - start;
    353                         if ( name && offset_begin && offset_end && name < offset_begin ) {
    354                                 // delimit strings
    355                                 *name++ = '\0';
    356                                 *offset_begin++ = '\0';
    357                                 *offset_end++ = '\0';
    358 
    359                                 #ifdef __USE_STREAM__
    360                                 serr    | "("  | frameNo | ")" | messages[i] | ":"
    361                                         | name | "+" | offset_begin | offset_end | endl;
    362                                 #else
    363                                 fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
    364                                 #endif
    365                         }
    366                         // otherwise, print the whole line
    367                         else {
    368                                 #ifdef __USE_STREAM__
    369                                 serr | "(" | frameNo | ")" | messages[i] | endl;
    370                                 #else
    371                                 fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );
    372                                 #endif
    373                         }
    374                 }
    375 
    376                 free( messages );
    377         }
    378 )
    379 
    380 void sigHandler_segv( __CFA_SIGPARMS__ ) {
    381         LIB_DEBUG_DO(
    382                 #ifdef __USE_STREAM__
    383                 serr    | "*CFA runtime error* program cfa-cpp terminated with"
    384                         | (sig == SIGSEGV ? "segment fault." : "bus error.")
    385                         | endl;
    386                 #else
    387                 fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
    388                 #endif
    389 
    390                 // skip first 2 stack frames
    391                 __kernel_backtrace( 1 );
    392         )
    393         exit( EXIT_FAILURE );
    394 }
    395 
    396 // void sigHandler_abort( __CFA_SIGPARMS__ ) {
    397 //      // skip first 6 stack frames
    398 //      LIB_DEBUG_DO( __kernel_backtrace( 6 ); )
    399 
    400 //      // reset default signal handler
    401 //      __kernel_sigdefault( SIGABRT );
    402 
    403 //      raise( SIGABRT );
    404 // }
  • src/libcfa/concurrency/thread

    rd43cd01 r653f2c7  
    5454}
    5555
    56 extern volatile thread_local thread_desc * this_thread;
     56thread_desc * this_thread(void);
    5757
    5858forall( dtype T | is_thread(T) )
  • src/libcfa/concurrency/thread.c

    rd43cd01 r653f2c7  
    2828}
    2929
    30 extern volatile thread_local processor * this_processor;
     30extern thread_local processor * this_processor;
    3131
    3232//-----------------------------------------------------------------------------
     
    7171        coroutine_desc* thrd_c = get_coroutine(this);
    7272        thread_desc*  thrd_h = get_thread   (this);
    73         thrd_c->last = this_coroutine;
     73        thrd_c->last = this_coroutine();
     74        this_processor->current_coroutine = thrd_c;
    7475
    75         // LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
     76        LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
    7677
    77         disable_interrupts();
    7878        create_stack(&thrd_c->stack, thrd_c->stack.size);
    79         this_coroutine = thrd_c;
    8079        CtxStart(this, CtxInvokeThread);
    81         assert( thrd_c->last->stack.context );
    8280        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
    8381
    8482        ScheduleThread(thrd_h);
    85         enable_interrupts( __PRETTY_FUNCTION__ );
    8683}
    8784
    8885void yield( void ) {
    89         BlockInternal( (thread_desc *)this_thread );
     86        ScheduleInternal( this_processor->current_thread );
    9087}
    9188
     
    9895void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
    9996        // set state of current coroutine to inactive
    100         src->state = src->state == Halted ? Halted : Inactive;
     97        src->state = Inactive;
    10198        dst->state = Active;
    10299
     
    106103        // set new coroutine that the processor is executing
    107104        // and context switch to it
    108         this_coroutine = dst;
    109         assert( src->stack.context );
     105        this_processor->current_coroutine = dst;
    110106        CtxSwitch( src->stack.context, dst->stack.context );
    111         this_coroutine = src;
     107        this_processor->current_coroutine = src;
    112108
    113109        // set state of new coroutine to active
    114         dst->state = dst->state == Halted ? Halted : Inactive;
     110        dst->state = Inactive;
    115111        src->state = Active;
    116112}
  • src/libcfa/libhdr/libdebug.h

    rd43cd01 r653f2c7  
    1818
    1919#ifdef __CFA_DEBUG__
    20         #define LIB_DEBUG_DO(...) __VA_ARGS__
    21         #define LIB_NO_DEBUG_DO(...)
     20        #define LIB_DEBUG_DO(x) x
     21        #define LIB_NO_DEBUG_DO(x) ((void)0)
    2222#else
    23         #define LIB_DEBUG_DO(...)
    24         #define LIB_NO_DEBUG_DO(...) __VA_ARGS__
     23        #define LIB_DEBUG_DO(x) ((void)0)
     24        #define LIB_NO_DEBUG_DO(x) x     
    2525#endif
    2626
     
    5151
    5252#ifdef __CFA_DEBUG_PRINT__
    53         #define LIB_DEBUG_WRITE( fd, buffer, len )     __lib_debug_write( fd, buffer, len )
    54         #define LIB_DEBUG_ACQUIRE()                    __lib_debug_acquire()
    55         #define LIB_DEBUG_RELEASE()                    __lib_debug_release()
    56         #define LIB_DEBUG_PRINT_SAFE(...)              __lib_debug_print_safe   (__VA_ARGS__)
    57         #define LIB_DEBUG_PRINT_NOLOCK(...)            __lib_debug_print_nolock (__VA_ARGS__)
    58         #define LIB_DEBUG_PRINT_BUFFER(...)            __lib_debug_print_buffer (__VA_ARGS__)
    59         #define LIB_DEBUG_PRINT_BUFFER_DECL(fd, ...)   char text[256]; int len = snprintf( text, 256, __VA_ARGS__ ); __lib_debug_write( fd, text, len );
    60         #define LIB_DEBUG_PRINT_BUFFER_LOCAL(fd, ...)  len = snprintf( text, 256, __VA_ARGS__ ); __lib_debug_write( fd, text, len );
     53      #define LIB_DEBUG_WRITE( fd, buffer, len )  __lib_debug_write( fd, buffer, len )
     54      #define LIB_DEBUG_ACQUIRE()                 __lib_debug_acquire()
     55      #define LIB_DEBUG_RELEASE()                 __lib_debug_release()
     56      #define LIB_DEBUG_PRINT_SAFE(...)           __lib_debug_print_safe   (__VA_ARGS__)
     57      #define LIB_DEBUG_PRINT_NOLOCK(...)         __lib_debug_print_nolock (__VA_ARGS__)
     58      #define LIB_DEBUG_PRINT_BUFFER(...)         __lib_debug_print_buffer (__VA_ARGS__)
    6159#else
    62         #define LIB_DEBUG_WRITE(...)               ((void)0)
    63         #define LIB_DEBUG_ACQUIRE()                ((void)0)
    64         #define LIB_DEBUG_RELEASE()                ((void)0)
    65         #define LIB_DEBUG_PRINT_SAFE(...)          ((void)0)
    66         #define LIB_DEBUG_PRINT_NOLOCK(...)        ((void)0)
    67         #define LIB_DEBUG_PRINT_BUFFER(...)        ((void)0)
    68         #define LIB_DEBUG_PRINT_BUFFER_DECL(...)   ((void)0)
    69         #define LIB_DEBUG_PRINT_BUFFER_LOCAL(...)  ((void)0)
     60      #define LIB_DEBUG_WRITE(...)          ((void)0)
     61      #define LIB_DEBUG_ACQUIRE()           ((void)0)
     62      #define LIB_DEBUG_RELEASE()           ((void)0)
     63      #define LIB_DEBUG_PRINT_SAFE(...)     ((void)0)
     64      #define LIB_DEBUG_PRINT_NOLOCK(...)   ((void)0)
     65      #define LIB_DEBUG_PRINT_BUFFER(...)   ((void)0)
    7066#endif
    7167
  • src/tests/sched-int-block.c

    rd43cd01 r653f2c7  
    2929//------------------------------------------------------------------------------
    3030void wait_op( global_data_t * mutex a, global_data_t * mutex b, unsigned i ) {
    31         wait( &cond, (uintptr_t)this_thread );
     31        wait( &cond, (uintptr_t)this_thread() );
    3232
    3333        yield( ((unsigned)rand48()) % 10 );
     
    3838        }
    3939
    40         a->last_thread = b->last_thread = this_thread;
     40        a->last_thread = b->last_thread = this_thread();
    4141
    4242        yield( ((unsigned)rand48()) % 10 );
     
    5454        yield( ((unsigned)rand48()) % 10 );
    5555
    56         a->last_thread = b->last_thread = a->last_signaller = b->last_signaller = this_thread;
     56        a->last_thread = b->last_thread = a->last_signaller = b->last_signaller = this_thread();
    5757
    5858        if( !is_empty( &cond ) ) {
     
    8484//------------------------------------------------------------------------------
    8585void barge_op( global_data_t * mutex a ) {
    86         a->last_thread = this_thread;
     86        a->last_thread = this_thread();
    8787}
    8888
Note: See TracChangeset for help on using the changeset viewer.