Ignore:
File:
1 edited

Legend:

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

    r55d6affb rda3963a  
    2222#include <signal.h>
    2323#include <unistd.h>
    24 extern "C" {
    25         #include <sys/eventfd.h>
    26 }
    2724
    2825//CFA Includes
     
    112109static void __run_thread(processor * this, $thread * dst);
    113110static void __wake_one(cluster * cltr);
     111static void wait(__bin_sem_t & this);
    114112
    115113static void push  (__cluster_idles & idles, processor & proc);
     
    117115static [unsigned idle, unsigned total, * processor] query( & __cluster_idles idles );
    118116
    119 extern void __cfa_io_start( processor * );
    120 extern void __cfa_io_drain( processor * );
    121 extern void __cfa_io_flush( processor * );
    122 extern void __cfa_io_stop ( processor * );
    123 static inline void __maybe_io_drain( processor * );
    124 
    125 extern void __disable_interrupts_hard();
    126 extern void __enable_interrupts_hard();
    127117
    128118//=============================================================================================
     
    140130        verify(this);
    141131
    142         __cfa_io_start( this );
    143 
    144132        __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
    145133        #if !defined(__CFA_NO_STATISTICS__)
     
    163151                MAIN_LOOP:
    164152                for() {
    165                         // Check if there is pending io
    166                         __maybe_io_drain( this );
    167 
    168153                        // Try to get the next thread
    169154                        readyThread = __next_thread( this->cltr );
    170155
    171156                        if( !readyThread ) {
    172                                 __cfa_io_flush( this );
    173157                                readyThread = __next_thread_slow( this->cltr );
    174158                        }
     
    206190                                #endif
    207191
    208                                 __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle);
    209 
    210                                 __disable_interrupts_hard();
    211                                 eventfd_t val;
    212                                 eventfd_read( this->idle, &val );
    213                                 __enable_interrupts_hard();
     192                                wait( this->idle );
    214193
    215194                                #if !defined(__CFA_NO_STATISTICS__)
     
    227206
    228207                        /* paranoid */ verify( readyThread );
    229 
    230                         // Reset io dirty bit
    231                         this->io.dirty = false;
    232208
    233209                        // We found a thread run it
     
    244220                                }
    245221                        #endif
    246 
    247                         if(this->io.pending && !this->io.dirty) {
    248                                 __cfa_io_flush( this );
    249                         }
    250222                }
    251223
     
    253225        }
    254226
    255         __cfa_io_stop( this );
    256 
    257227        post( this->terminated );
    258 
    259228
    260229        if(this == mainProcessor) {
     
    279248        /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next );
    280249        __builtin_prefetch( thrd_dst->context.SP );
    281 
    282         __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name);
    283250
    284251        $coroutine * proc_cor = get_coroutine(this->runner);
     
    363330        // Just before returning to the processor, set the processor coroutine to active
    364331        proc_cor->state = Active;
    365 
    366         __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst);
    367332
    368333        /* paranoid */ verify( ! __preemption_enabled() );
     
    584549// Kernel Idle Sleep
    585550//=============================================================================================
     551extern "C" {
     552        char * strerror(int);
     553}
     554#define CHECKED(x) { int err = x; if( err != 0 ) abort("KERNEL ERROR: Operation \"" #x "\" return error %d - %s\n", err, strerror(err)); }
     555
     556static void wait(__bin_sem_t & this) with( this ) {
     557        verify(__cfaabi_dbg_in_kernel());
     558        CHECKED( pthread_mutex_lock(&lock) );
     559                while(val < 1) {
     560                        pthread_cond_wait(&cond, &lock);
     561                }
     562                val -= 1;
     563        CHECKED( pthread_mutex_unlock(&lock) );
     564}
     565
     566static bool post(__bin_sem_t & this) with( this ) {
     567        bool needs_signal = false;
     568
     569        CHECKED( pthread_mutex_lock(&lock) );
     570                if(val < 1) {
     571                        val += 1;
     572                        pthread_cond_signal(&cond);
     573                        needs_signal = true;
     574                }
     575        CHECKED( pthread_mutex_unlock(&lock) );
     576
     577        return needs_signal;
     578}
     579
     580#undef CHECKED
     581
    586582// Wake a thread from the front if there are any
    587583static void __wake_one(cluster * this) {
     
    599595
    600596        // We found a processor, wake it up
    601         eventfd_t val;
    602         val = 1;
    603         eventfd_write( p->idle, val );
     597        post( p->idle );
    604598
    605599        #if !defined(__CFA_NO_STATISTICS__)
     
    619613        disable_interrupts();
    620614                /* paranoid */ verify( ! __preemption_enabled() );
    621                 eventfd_t val;
    622                 val = 1;
    623                 eventfd_write( this->idle, val );
     615                post( this->idle );
    624616        enable_interrupts( __cfaabi_dbg_ctx );
    625617}
     
    704696// Kernel Utilities
    705697//=============================================================================================
    706 #if defined(CFA_HAVE_LINUX_IO_URING_H)
    707 #include "io/types.hfa"
    708 #endif
    709 
    710 static inline void __maybe_io_drain( processor * proc ) {
    711         #if defined(CFA_HAVE_LINUX_IO_URING_H)
    712                 __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd);
    713 
    714                 // Check if we should drain the queue
    715                 $io_context * ctx = proc->io.ctx;
    716                 unsigned head = *ctx->cq.head;
    717                 unsigned tail = *ctx->cq.tail;
    718                 if(head != tail) __cfa_io_drain( proc );
    719         #endif
    720 }
    721 
    722698//-----------------------------------------------------------------------------
    723699// Debug
Note: See TracChangeset for help on using the changeset viewer.