Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/kernel.c

    r65deb18 rb158d8f  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:33:18 2017
    13 // Update Count     : 2
     12// Last Modified On : Fri Dec  8 16:23:33 2017
     13// Update Count     : 3
    1414//
    1515
    1616//C Includes
    1717#include <stddef.h>
     18#define ftype `ftype`
    1819extern "C" {
    1920#include <stdio.h>
     
    2324#include <unistd.h>
    2425}
     26#undef ftype
    2527
    2628//CFA Includes
     
    8587}
    8688
    87 void ?{}( coStack_t & this, current_stack_info_t * info) with( this ) {
    88         size      = info->size;
    89         storage  = info->storage;
    90         limit    = info->limit;
    91         base      = info->base;
    92         context  = info->context;
    93         top      = info->top;
    94         userStack = true;
    95 }
    96 
    97 void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
    98         stack{ info };
    99         name = "Main Thread";
    100         errno_ = 0;
    101         state = Start;
    102         starter = NULL;
    103 }
    104 
    105 void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
    106         self_cor{ info };
     89void ?{}( coStack_t & this, current_stack_info_t * info) {
     90        this.size = info->size;
     91        this.storage = info->storage;
     92        this.limit = info->limit;
     93        this.base = info->base;
     94        this.context = info->context;
     95        this.top = info->top;
     96        this.userStack = true;
     97}
     98
     99void ?{}( coroutine_desc & this, current_stack_info_t * info) {
     100        (this.stack){ info };
     101        this.name = "Main Thread";
     102        this.errno_ = 0;
     103        this.state = Start;
     104        this.starter = NULL;
     105}
     106
     107void ?{}( thread_desc & this, current_stack_info_t * info) {
     108        (this.self_cor){ info };
    107109}
    108110
     
    131133void ?{}(processor & this, cluster * cltr) {
    132134        this.cltr = cltr;
    133         this.terminated{ 0 };
     135        (this.terminated){ 0 };
    134136        this.do_terminate = false;
    135137        this.preemption_alarm = NULL;
     
    141143void ?{}(processor & this, cluster * cltr, processorCtx_t & runner) {
    142144        this.cltr = cltr;
    143         this.terminated{ 0 };
     145        (this.terminated){ 0 };
    144146        this.do_terminate = false;
    145147        this.preemption_alarm = NULL;
     
    152154}
    153155
    154 void ^?{}(processor & this) with( this ){
    155         if( ! do_terminate ) {
     156void ^?{}(processor & this) {
     157        if( ! this.do_terminate ) {
    156158                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
    157                 do_terminate = true;
    158                 P( terminated );
    159                 pthread_join( kernel_thread, NULL );
    160         }
    161 }
    162 
    163 void ?{}(cluster & this) with( this ) {
    164         ready_queue{};
    165         ready_queue_lock{};
    166 
    167         preemption = default_preemption();
     159                this.do_terminate = true;
     160                P( this.terminated );
     161                pthread_join( this.kernel_thread, NULL );
     162        }
     163}
     164
     165void ?{}(cluster & this) {
     166        (this.ready_queue){};
     167        ( this.ready_queue_lock ){};
     168
     169        this.preemption = default_preemption();
    168170}
    169171
     
    238240// Once a thread has finished running, some of
    239241// its final actions must be executed from the kernel
    240 void finishRunning(processor * this) with( this->finish ) {
    241         if( action_code == Release ) {
    242                 unlock( *lock );
    243         }
    244         else if( action_code == Schedule ) {
    245                 ScheduleThread( thrd );
    246         }
    247         else if( action_code == Release_Schedule ) {
    248                 unlock( *lock );
    249                 ScheduleThread( thrd );
    250         }
    251         else if( action_code == Release_Multi ) {
    252                 for(int i = 0; i < lock_count; i++) {
    253                         unlock( *locks[i] );
     242void finishRunning(processor * this) {
     243        if( this->finish.action_code == Release ) {
     244                unlock( *this->finish.lock );
     245        }
     246        else if( this->finish.action_code == Schedule ) {
     247                ScheduleThread( this->finish.thrd );
     248        }
     249        else if( this->finish.action_code == Release_Schedule ) {
     250                unlock( *this->finish.lock );
     251                ScheduleThread( this->finish.thrd );
     252        }
     253        else if( this->finish.action_code == Release_Multi ) {
     254                for(int i = 0; i < this->finish.lock_count; i++) {
     255                        unlock( *this->finish.locks[i] );
    254256                }
    255257        }
    256         else if( action_code == Release_Multi_Schedule ) {
    257                 for(int i = 0; i < lock_count; i++) {
    258                         unlock( *locks[i] );
     258        else if( this->finish.action_code == Release_Multi_Schedule ) {
     259                for(int i = 0; i < this->finish.lock_count; i++) {
     260                        unlock( *this->finish.locks[i] );
    259261                }
    260                 for(int i = 0; i < thrd_count; i++) {
    261                         ScheduleThread( thrds[i] );
     262                for(int i = 0; i < this->finish.thrd_count; i++) {
     263                        ScheduleThread( this->finish.thrds[i] );
    262264                }
    263265        }
    264266        else {
    265                 assert(action_code == No_Action);
     267                assert(this->finish.action_code == No_Action);
    266268        }
    267269}
     
    332334        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    333335
    334         with( *this_processor->cltr ) {
    335                 lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
    336                 append( ready_queue, thrd );
    337                 unlock( ready_queue_lock );
    338         }
    339 
    340         verify( disable_preempt_count > 0 );
    341 }
    342 
    343 thread_desc * nextThread(cluster * this) with( *this ) {
    344         verify( disable_preempt_count > 0 );
    345         lock( ready_queue_lock __cfaabi_dbg_ctx2 );
    346         thread_desc * head = pop_head( ready_queue );
    347         unlock( ready_queue_lock );
     336        lock(   this_processor->cltr->ready_queue_lock __cfaabi_dbg_ctx2 );
     337        append( this_processor->cltr->ready_queue, thrd );
     338        unlock( this_processor->cltr->ready_queue_lock );
     339
     340        verify( disable_preempt_count > 0 );
     341}
     342
     343thread_desc * nextThread(cluster * this) {
     344        verify( disable_preempt_count > 0 );
     345        lock( this->ready_queue_lock __cfaabi_dbg_ctx2 );
     346        thread_desc * head = pop_head( this->ready_queue );
     347        unlock( this->ready_queue_lock );
    348348        verify( disable_preempt_count > 0 );
    349349        return head;
     
    361361        disable_interrupts();
    362362        this_processor->finish.action_code = Release;
    363         this_processor->finish.lock        = lock;
     363        this_processor->finish.lock = lock;
    364364
    365365        verify( disable_preempt_count > 0 );
     
    371371
    372372void BlockInternal( thread_desc * thrd ) {
     373        assert(thrd);
    373374        disable_interrupts();
     375        assert( thrd->self_cor.state != Halted );
    374376        this_processor->finish.action_code = Schedule;
    375         this_processor->finish.thrd        = thrd;
     377        this_processor->finish.thrd = thrd;
    376378
    377379        verify( disable_preempt_count > 0 );
     
    386388        disable_interrupts();
    387389        this_processor->finish.action_code = Release_Schedule;
    388         this_processor->finish.lock        = lock;
    389         this_processor->finish.thrd        = thrd;
     390        this_processor->finish.lock = lock;
     391        this_processor->finish.thrd = thrd;
    390392
    391393        verify( disable_preempt_count > 0 );
     
    399401        disable_interrupts();
    400402        this_processor->finish.action_code = Release_Multi;
    401         this_processor->finish.locks       = locks;
    402         this_processor->finish.lock_count  = count;
     403        this_processor->finish.locks = locks;
     404        this_processor->finish.lock_count = count;
    403405
    404406        verify( disable_preempt_count > 0 );
     
    412414        disable_interrupts();
    413415        this_processor->finish.action_code = Release_Multi_Schedule;
    414         this_processor->finish.locks       = locks;
    415         this_processor->finish.lock_count  = lock_count;
    416         this_processor->finish.thrds       = thrds;
    417         this_processor->finish.thrd_count  = thrd_count;
     416        this_processor->finish.locks = locks;
     417        this_processor->finish.lock_count = lock_count;
     418        this_processor->finish.thrds = thrds;
     419        this_processor->finish.thrd_count = thrd_count;
    418420
    419421        verify( disable_preempt_count > 0 );
     
    427429        verify( disable_preempt_count > 0 );
    428430        this_processor->finish.action_code = thrd ? Release_Schedule : Release;
    429         this_processor->finish.lock        = lock;
    430         this_processor->finish.thrd        = thrd;
     431        this_processor->finish.lock = lock;
     432        this_processor->finish.thrd = thrd;
    431433
    432434        suspend();
     
    579581void ^?{}(semaphore & this) {}
    580582
    581 void P(semaphore & this) with( this ){
    582         lock( lock __cfaabi_dbg_ctx2 );
    583         count -= 1;
    584         if ( count < 0 ) {
     583void P(semaphore & this) {
     584        lock( this.lock __cfaabi_dbg_ctx2 );
     585        this.count -= 1;
     586        if ( this.count < 0 ) {
    585587                // queue current task
    586                 append( waiting, (thread_desc *)this_thread );
     588                append( this.waiting, (thread_desc *)this_thread );
    587589
    588590                // atomically release spin lock and block
    589                 BlockInternal( &lock );
     591                BlockInternal( &this.lock );
    590592        }
    591593        else {
    592             unlock( lock );
    593         }
    594 }
    595 
    596 void V(semaphore & this) with( this ) {
     594            unlock( this.lock );
     595        }
     596}
     597
     598void V(semaphore & this) {
    597599        thread_desc * thrd = NULL;
    598         lock( lock __cfaabi_dbg_ctx2 );
    599         count += 1;
    600         if ( count <= 0 ) {
     600        lock( this.lock __cfaabi_dbg_ctx2 );
     601        this.count += 1;
     602        if ( this.count <= 0 ) {
    601603                // remove task at head of waiting list
    602                 thrd = pop_head( waiting );
    603         }
    604 
    605         unlock( lock );
     604                thrd = pop_head( this.waiting );
     605        }
     606
     607        unlock( this.lock );
    606608
    607609        // make new owner
Note: See TracChangeset for help on using the changeset viewer.