Changes in / [7b28e4a:90cedbdd]


Ignore:
Location:
src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/bits/locks.h

    r7b28e4a r90cedbdd  
    126126
    127127        struct __bin_sem_t {
    128                 bool                    signaled;
    129                 pthread_mutex_t         lock;
    130                 pthread_cond_t          cond;
     128                int_fast8_t     counter;
     129                pthread_mutex_t lock;
     130                pthread_cond_t  cond;
    131131        };
    132132
    133133        static inline void ?{}(__bin_sem_t & this) with( this ) {
    134                 signaled = false;
     134                counter = 0;
    135135                pthread_mutex_init(&lock, NULL);
    136136                pthread_cond_init (&cond, NULL);
     
    145145                verify(__cfaabi_dbg_in_kernel());
    146146                pthread_mutex_lock(&lock);
    147                         if(!signaled) {   // this must be a loop, not if!
    148                                 pthread_cond_wait(&cond, &lock);
    149                         }
    150                         signaled = false;
     147                if(counter != 0) {   // this must be a loop, not if!
     148                        pthread_cond_wait(&cond, &lock);
     149                }
     150                counter = 1;
    151151                pthread_mutex_unlock(&lock);
    152152        }
     
    154154        static inline void post(__bin_sem_t & this) with( this ) {
    155155                verify(__cfaabi_dbg_in_kernel());
    156 
    157156                pthread_mutex_lock(&lock);
    158                         bool needs_signal = !signaled;
    159                         signaled = true;
     157                bool needs_signal = counter == 0;
     158                counter = 1;
    160159                pthread_mutex_unlock(&lock);
    161 
    162                 if (needs_signal)
     160                if (!needs_signal)
    163161                        pthread_cond_signal(&cond);
    164         }
     162                }
    165163#endif
  • src/libcfa/concurrency/kernel

    r7b28e4a r90cedbdd  
    113113        pthread_t kernel_thread;
    114114
     115        // Termination
     116        // Set to true to notify the processor should terminate
     117        volatile bool do_terminate;
     118
     119        // Termination synchronisation
     120        semaphore terminated;
     121
    115122        // RunThread data
    116123        // Action to do after a thread is ran
     
    125132
    126133        // Idle lock
    127         __bin_sem_t idleLock;
    128 
    129         // Termination
    130         // Set to true to notify the processor should terminate
    131         volatile bool do_terminate;
    132 
    133         // Termination synchronisation
    134         semaphore terminated;
     134        sem_t idleLock;
     135        // __bin_sem_t idleLock;
    135136
    136137        // Link lists fields
  • src/libcfa/concurrency/kernel.c

    r7b28e4a r90cedbdd  
    147147        runner.proc = &this;
    148148
    149         idleLock{};
     149        sem_init(&idleLock, 0, 0);
    150150
    151151        start( &this );
     
    155155        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
    156156                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
    157 
    158                 __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
    159                 wake( &this );
    160 
     157                terminate(&this);
     158                verify( __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
     159                verify( kernelTLS.this_processor != &this);
    161160                P( terminated );
    162161                verify( kernelTLS.this_processor != &this);
    163         }
    164 
    165         pthread_join( kernel_thread, NULL );
     162                pthread_join( kernel_thread, NULL );
     163        }
     164
     165        sem_destroy(&idleLock);
    166166}
    167167
     
    295295}
    296296
     297// Handles spinning logic
     298// TODO : find some strategy to put cores to sleep after some time
     299void spin(processor * this, unsigned int * spin_count) {
     300        // (*spin_count)++;
     301        halt(this);
     302}
     303
    297304// KERNEL_ONLY
    298305// Context invoker for processors
     
    401408                unlock( ready_queue_lock );
    402409
    403                 if(was_empty) {
     410                if( was_empty ) {
    404411                        lock      (proc_list_lock __cfaabi_dbg_ctx2);
    405412                        if(idles) {
    406                                 wake_fast(idles.head);
     413                                wake(idles.head);
    407414                        }
    408415                        unlock    (proc_list_lock);
    409416                }
    410                 else if( struct processor * idle = idles.head ) {
    411                         wake_fast(idle);
    412                 }
    413 
    414417        }
    415418
     
    657660
    658661void halt(processor * this) with( *this ) {
    659         // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
     662        verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
    660663
    661664        with( *cltr ) {
     
    668671        __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
    669672
    670         wait( idleLock );
     673        // #ifdef __CFA_WITH_VERIFY__
     674        //      int sval = 0;
     675        //      sem_getvalue(&this->idleLock, &sval);
     676        //      verifyf(sval < 200, "Binary semaphore reached value %d : \n", sval);
     677        // #endif
     678
     679        verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
     680        int __attribute__((unused)) ret = sem_wait(&idleLock);
     681        // verifyf(ret >= 0 || errno == EINTR, "Sem_wait returned %d (errno %d : %s\n", ret, errno, strerror(errno));
     682
     683        // wait( idleLock );
    671684
    672685        __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
     
    678691                unlock    (proc_list_lock);
    679692        }
     693}
     694
     695void wake(processor * this) {
     696        __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this);
     697        int __attribute__((unused)) ret = sem_post(&this->idleLock);
     698        // verifyf(ret >= 0 || errno == EINTR, "Sem_post returned %d (errno %d : %s\n", ret, errno, strerror(errno));
     699
     700        // #ifdef __CFA_WITH_VERIFY__
     701        //      int sval = 0;
     702        //      sem_getvalue(&this->idleLock, &sval);
     703        //      verifyf(sval < 200, "Binary semaphore reached value %d\n", sval);
     704        // #endif
     705
     706        // post( this->idleLock );
    680707}
    681708
  • src/libcfa/concurrency/kernel_private.h

    r7b28e4a r90cedbdd  
    5858void finishRunning(processor * this);
    5959void halt(processor * this);
    60 
    61 static inline void wake_fast(processor * this) {
    62         __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this);
    63         post( this->idleLock );
    64 }
    65 
    66 static inline void wake(processor * this) {
    67         disable_interrupts();
    68         wake_fast(this);
    69         enable_interrupts( __cfaabi_dbg_ctx );
    70 }
     60void wake(processor * this);
     61void terminate(processor * this);
     62void spin(processor * this, unsigned int * spin_count);
    7163
    7264struct event_kernel_t {
     
    7668
    7769extern event_kernel_t * event_kernel;
     70
     71//extern thread_local coroutine_desc * volatile this_coroutine;
     72//extern thread_local thread_desc *    volatile this_thread;
     73//extern thread_local processor *      volatile this_processor;
     74
     75// extern volatile thread_local bool preemption_in_progress;
     76// extern volatile thread_local bool preemption_enabled;
     77// extern volatile thread_local unsigned short disable_preempt_count;
    7878
    7979struct __cfa_kernel_preemption_state_t {
  • src/libcfa/concurrency/preemption.c

    r7b28e4a r90cedbdd  
    260260static void preempt( processor * this ) {
    261261        sigval_t value = { PREEMPT_NORMAL };
     262        pthread_sigqueue( this->kernel_thread, SIGUSR1, value );
     263}
     264
     265// kill wrapper : signal a processor
     266void terminate(processor * this) {
     267        disable_interrupts();
     268        __atomic_store_n(&this->do_terminate, true, __ATOMIC_SEQ_CST);
     269        wake( this );
     270        sigval_t value = { PREEMPT_TERMINATE };
     271        enable_interrupts( __cfaabi_dbg_ctx );
    262272        pthread_sigqueue( this->kernel_thread, SIGUSR1, value );
    263273}
  • src/tests/preempt_longrun/Makefile.am

    r7b28e4a r90cedbdd  
    3434
    3535clean-local:
    36         rm -f ${TESTS} core* out.log
     36        rm -f ${TESTS}
    3737
    3838% : %.c ${CC}
  • src/tests/preempt_longrun/Makefile.in

    r7b28e4a r90cedbdd  
    878878
    879879clean-local:
    880         rm -f ${TESTS} core* out.log
     880        rm -f ${TESTS}
    881881
    882882% : %.c ${CC}
  • src/tests/preempt_longrun/enter.c

    r7b28e4a r90cedbdd  
    1515
    1616monitor mon_t {};
     17
     18mon_t mon;
     19
    1720void foo( mon_t & mutex this ) {}
    1821
    19 mon_t mon;
    2022thread worker_t {};
     23
    2124void main( worker_t & this ) {
    2225        for( unsigned long i = 0; i < N; i++ ) {
     
    2528}
    2629
     30extern "C" {
     31static worker_t * workers;
     32}
     33
    2734int main(int argc, char * argv[] ) {
    2835        processor p;
    2936        {
    3037                worker_t w[7];
     38                workers = w;
    3139        }
    3240}
  • src/tests/preempt_longrun/processor.c

    r7b28e4a r90cedbdd  
    1313}
    1414
    15 static const unsigned long N = 50_000ul;
     15static const unsigned long N = 5_000ul;
    1616
    1717int main(int argc, char* argv[]) {
    1818        processor * p[15];
    19         write(STDOUT_FILENO, "Preparing\n", sizeof("Preparing\n"));
     19        write(STDERR_FILENO, "Preparing\n", sizeof("Preparing\n"));
    2020        for ( int pi = 0; pi < 15; pi++ ) {
    2121                p[pi] = new();
    2222        }
    23         write(STDOUT_FILENO, "Starting\n", sizeof("Starting\n"));
     23        write(STDERR_FILENO, "Starting\n", sizeof("Starting\n"));
    2424        for ( int i = 0; i < N; i++) {
    2525                int pi = i % 15;
     
    2727                p[pi] = new();
    2828        }
    29         write(STDOUT_FILENO, "Stopping\n", sizeof("Stopping\n"));
     29        write(STDERR_FILENO, "Stopping\n", sizeof("Stopping\n"));
    3030        for ( int pi = 0; pi < 15; pi++ ) {
    3131                delete( p[pi] );
    3232        }
    33         write(STDOUT_FILENO, "Done\n", sizeof("Done\n"));
     33        write(STDERR_FILENO, "Done\n", sizeof("Done\n"));
    3434}
Note: See TracChangeset for help on using the changeset viewer.