Changes in / [6d2386e:3edc2df]


Ignore:
Location:
src/libcfa
Files:
3 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/Makefile.am

    r6d2386e r3edc2df  
    9595
    9696cfa_includedir = $(CFA_INCDIR)
    97 nobase_cfa_include_HEADERS =    \
    98         ${headers}                      \
    99         ${stdhdr}                       \
    100         math                            \
    101         gmp                             \
    102         bits/defs.h             \
    103         bits/locks.h            \
    104         concurrency/invoke.h    \
    105         libhdr.h                        \
    106         libhdr/libalign.h       \
    107         libhdr/libdebug.h       \
    108         libhdr/libtools.h
     97nobase_cfa_include_HEADERS = ${headers} ${stdhdr} math gmp concurrency/invoke.h
    10998
    11099CLEANFILES = libcfa-prelude.c
  • src/libcfa/Makefile.in

    r6d2386e r3edc2df  
    264264        containers/result containers/vector concurrency/coroutine \
    265265        concurrency/thread concurrency/kernel concurrency/monitor \
    266         ${shell echo stdhdr/*} math gmp bits/defs.h bits/locks.h \
    267         concurrency/invoke.h libhdr.h libhdr/libalign.h \
    268         libhdr/libdebug.h libhdr/libtools.h
     266        ${shell echo stdhdr/*} math gmp concurrency/invoke.h
    269267HEADERS = $(nobase_cfa_include_HEADERS)
    270268am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP)
     
    432430stdhdr = ${shell echo stdhdr/*}
    433431cfa_includedir = $(CFA_INCDIR)
    434 nobase_cfa_include_HEADERS = \
    435         ${headers}                      \
    436         ${stdhdr}                       \
    437         math                            \
    438         gmp                             \
    439         bits/defs.h             \
    440         bits/locks.h            \
    441         concurrency/invoke.h    \
    442         libhdr.h                        \
    443         libhdr/libalign.h       \
    444         libhdr/libdebug.h       \
    445         libhdr/libtools.h
    446 
     432nobase_cfa_include_HEADERS = ${headers} ${stdhdr} math gmp concurrency/invoke.h
    447433CLEANFILES = libcfa-prelude.c
    448434all: all-am
  • src/libcfa/concurrency/alarm.c

    r6d2386e r3edc2df  
    186186
    187187        disable_interrupts();
    188         lock( event_kernel->lock DEBUG_CTX2 );
     188        lock( &event_kernel->lock DEBUG_CTX2 );
    189189        {
    190190                verify( validate( alarms ) );
     
    196196                }
    197197        }
    198         unlock( event_kernel->lock );
     198        unlock( &event_kernel->lock );
    199199        this->set = true;
    200200        enable_interrupts( DEBUG_CTX );
     
    203203void unregister_self( alarm_node_t * this ) {
    204204        disable_interrupts();
    205         lock( event_kernel->lock DEBUG_CTX2 );
     205        lock( &event_kernel->lock DEBUG_CTX2 );
    206206        {
    207207                verify( validate( &event_kernel->alarms ) );
    208208                remove( &event_kernel->alarms, this );
    209209        }
    210         unlock( event_kernel->lock );
     210        unlock( &event_kernel->lock );
    211211        enable_interrupts( DEBUG_CTX );
    212212        this->set = false;
  • src/libcfa/concurrency/invoke.h

    r6d2386e r3edc2df  
    1414//
    1515
    16 #include "bits/defs.h"
    17 #include "bits/locks.h"
     16#include <stdbool.h>
     17#include <stdint.h>
    1818
    1919#ifdef __CFORALL__
     
    2525#define _INVOKE_H_
    2626
     27        #define unlikely(x)    __builtin_expect(!!(x), 0)
     28        #define thread_local _Thread_local
     29
    2730        typedef void (*fptr_t)();
    2831        typedef int_fast16_t __lock_size_t;
     32
     33        struct spinlock {
     34                volatile int lock;
     35                #ifdef __CFA_DEBUG__
     36                        const char * prev_name;
     37                        void* prev_thrd;
     38                #endif
     39        };
    2940
    3041        struct __thread_queue_t {
     
    4758                void push( struct __condition_stack_t &, struct __condition_criterion_t * );
    4859                struct __condition_criterion_t * pop( struct __condition_stack_t & );
     60
     61                void  ?{}(spinlock & this);
     62                void ^?{}(spinlock & this);
    4963        }
    5064        #endif
     
    108122        struct monitor_desc {
    109123                // spinlock to protect internal data
    110                 struct __spinlock_t lock;
     124                struct spinlock lock;
    111125
    112126                // current owner of the monitor
  • src/libcfa/concurrency/kernel

    r6d2386e r3edc2df  
    2626//-----------------------------------------------------------------------------
    2727// Locks
    28 // // Lock the spinlock, spin if already acquired
    29 // void lock      ( spinlock * DEBUG_CTX_PARAM2 );
     28// Lock the spinlock, spin if already acquired
     29void lock      ( spinlock * DEBUG_CTX_PARAM2 );
    3030
    31 // // Lock the spinlock, yield repeatedly if already acquired
    32 // void lock_yield( spinlock * DEBUG_CTX_PARAM2 );
     31// Lock the spinlock, yield repeatedly if already acquired
     32void lock_yield( spinlock * DEBUG_CTX_PARAM2 );
    3333
    34 // // Lock the spinlock, return false if already acquired
    35 // bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );
     34// Lock the spinlock, return false if already acquired
     35bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );
    3636
    37 // // Unlock the spinlock
    38 // void unlock    ( spinlock * );
     37// Unlock the spinlock
     38void unlock    ( spinlock * );
    3939
    4040struct semaphore {
    41         __spinlock_t lock;
     41        spinlock lock;
    4242        int count;
    4343        __thread_queue_t waiting;
     
    5454struct cluster {
    5555        // Ready queue locks
    56         __spinlock_t ready_queue_lock;
     56        spinlock ready_queue_lock;
    5757
    5858        // Ready queue for threads
     
    7474        FinishOpCode action_code;
    7575        thread_desc * thrd;
    76         __spinlock_t * lock;
    77         __spinlock_t ** locks;
     76        spinlock * lock;
     77        spinlock ** locks;
    7878        unsigned short lock_count;
    7979        thread_desc ** thrds;
  • src/libcfa/concurrency/kernel.c

    r6d2386e r3edc2df  
    242242void finishRunning(processor * this) {
    243243        if( this->finish.action_code == Release ) {
    244                 unlock( *this->finish.lock );
     244                unlock( this->finish.lock );
    245245        }
    246246        else if( this->finish.action_code == Schedule ) {
     
    248248        }
    249249        else if( this->finish.action_code == Release_Schedule ) {
    250                 unlock( *this->finish.lock );
     250                unlock( this->finish.lock );
    251251                ScheduleThread( this->finish.thrd );
    252252        }
    253253        else if( this->finish.action_code == Release_Multi ) {
    254254                for(int i = 0; i < this->finish.lock_count; i++) {
    255                         unlock( *this->finish.locks[i] );
     255                        unlock( this->finish.locks[i] );
    256256                }
    257257        }
    258258        else if( this->finish.action_code == Release_Multi_Schedule ) {
    259259                for(int i = 0; i < this->finish.lock_count; i++) {
    260                         unlock( *this->finish.locks[i] );
     260                        unlock( this->finish.locks[i] );
    261261                }
    262262                for(int i = 0; i < this->finish.thrd_count; i++) {
     
    334334        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    335335
    336         lock(   this_processor->cltr->ready_queue_lock DEBUG_CTX2 );
     336        lock(   &this_processor->cltr->ready_queue_lock DEBUG_CTX2 );
    337337        append( this_processor->cltr->ready_queue, thrd );
    338         unlock( this_processor->cltr->ready_queue_lock );
     338        unlock( &this_processor->cltr->ready_queue_lock );
    339339
    340340        verify( disable_preempt_count > 0 );
     
    343343thread_desc * nextThread(cluster * this) {
    344344        verify( disable_preempt_count > 0 );
    345         lock( this->ready_queue_lock DEBUG_CTX2 );
     345        lock( &this->ready_queue_lock DEBUG_CTX2 );
    346346        thread_desc * head = pop_head( this->ready_queue );
    347         unlock( this->ready_queue_lock );
     347        unlock( &this->ready_queue_lock );
    348348        verify( disable_preempt_count > 0 );
    349349        return head;
     
    358358}
    359359
    360 void BlockInternal( __spinlock_t * lock ) {
     360void BlockInternal( spinlock * lock ) {
    361361        disable_interrupts();
    362362        this_processor->finish.action_code = Release;
     
    384384}
    385385
    386 void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
     386void BlockInternal( spinlock * lock, thread_desc * thrd ) {
    387387        assert(thrd);
    388388        disable_interrupts();
     
    398398}
    399399
    400 void BlockInternal(__spinlock_t * locks [], unsigned short count) {
     400void BlockInternal(spinlock * locks [], unsigned short count) {
    401401        disable_interrupts();
    402402        this_processor->finish.action_code = Release_Multi;
     
    411411}
    412412
    413 void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
     413void BlockInternal(spinlock * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
    414414        disable_interrupts();
    415415        this_processor->finish.action_code = Release_Multi_Schedule;
     
    426426}
    427427
    428 void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
     428void LeaveThread(spinlock * lock, thread_desc * thrd) {
    429429        verify( disable_preempt_count > 0 );
    430430        this_processor->finish.action_code = thrd ? Release_Schedule : Release;
     
    516516}
    517517
    518 static __spinlock_t kernel_abort_lock;
    519 static __spinlock_t kernel_debug_lock;
     518static spinlock kernel_abort_lock;
     519static spinlock kernel_debug_lock;
    520520static bool kernel_abort_called = false;
    521521
     
    523523        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
    524524        // the globalAbort flag is true.
    525         lock( kernel_abort_lock DEBUG_CTX2 );
     525        lock( &kernel_abort_lock DEBUG_CTX2 );
    526526
    527527        // first task to abort ?
    528528        if ( !kernel_abort_called ) {                   // not first task to abort ?
    529529                kernel_abort_called = true;
    530                 unlock( kernel_abort_lock );
     530                unlock( &kernel_abort_lock );
    531531        }
    532532        else {
    533                 unlock( kernel_abort_lock );
     533                unlock( &kernel_abort_lock );
    534534
    535535                sigset_t mask;
     
    561561extern "C" {
    562562        void __lib_debug_acquire() {
    563                 lock( kernel_debug_lock DEBUG_CTX2 );
     563                lock( &kernel_debug_lock DEBUG_CTX2 );
    564564        }
    565565
    566566        void __lib_debug_release() {
    567                 unlock( kernel_debug_lock );
     567                unlock( &kernel_debug_lock );
    568568        }
    569569}
     
    574574//-----------------------------------------------------------------------------
    575575// Locks
     576void ?{}( spinlock & this ) {
     577        this.lock = 0;
     578}
     579void ^?{}( spinlock & this ) {
     580
     581}
     582
     583bool try_lock( spinlock * this DEBUG_CTX_PARAM2 ) {
     584        return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
     585}
     586
     587void lock( spinlock * this DEBUG_CTX_PARAM2 ) {
     588        for ( unsigned int i = 1;; i += 1 ) {
     589                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
     590        }
     591        LIB_DEBUG_DO(
     592                this->prev_name = caller;
     593                this->prev_thrd = this_thread;
     594        )
     595}
     596
     597void lock_yield( spinlock * this DEBUG_CTX_PARAM2 ) {
     598        for ( unsigned int i = 1;; i += 1 ) {
     599                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
     600                yield();
     601        }
     602        LIB_DEBUG_DO(
     603                this->prev_name = caller;
     604                this->prev_thrd = this_thread;
     605        )
     606}
     607
     608
     609void unlock( spinlock * this ) {
     610        __sync_lock_release_4( &this->lock );
     611}
     612
    576613void  ?{}( semaphore & this, int count = 1 ) {
    577614        (this.lock){};
     
    582619
    583620void P(semaphore & this) {
    584         lock( this.lock DEBUG_CTX2 );
     621        lock( &this.lock DEBUG_CTX2 );
    585622        this.count -= 1;
    586623        if ( this.count < 0 ) {
     
    592629        }
    593630        else {
    594             unlock( this.lock );
     631            unlock( &this.lock );
    595632        }
    596633}
     
    598635void V(semaphore & this) {
    599636        thread_desc * thrd = NULL;
    600         lock( this.lock DEBUG_CTX2 );
     637        lock( &this.lock DEBUG_CTX2 );
    601638        this.count += 1;
    602639        if ( this.count <= 0 ) {
     
    605642        }
    606643
    607         unlock( this.lock );
     644        unlock( &this.lock );
    608645
    609646        // make new owner
  • src/libcfa/concurrency/kernel_private.h

    r6d2386e r3edc2df  
    4545//Block current thread and release/wake-up the following resources
    4646void BlockInternal(void);
    47 void BlockInternal(__spinlock_t * lock);
     47void BlockInternal(spinlock * lock);
    4848void BlockInternal(thread_desc * thrd);
    49 void BlockInternal(__spinlock_t * lock, thread_desc * thrd);
    50 void BlockInternal(__spinlock_t * locks [], unsigned short count);
    51 void BlockInternal(__spinlock_t * locks [], unsigned short count, thread_desc * thrds [], unsigned short thrd_count);
    52 void LeaveThread(__spinlock_t * lock, thread_desc * thrd);
     49void BlockInternal(spinlock * lock, thread_desc * thrd);
     50void BlockInternal(spinlock * locks [], unsigned short count);
     51void BlockInternal(spinlock * locks [], unsigned short count, thread_desc * thrds [], unsigned short thrd_count);
     52void LeaveThread(spinlock * lock, thread_desc * thrd);
    5353
    5454//-----------------------------------------------------------------------------
     
    6666struct event_kernel_t {
    6767        alarm_list_t alarms;
    68         __spinlock_t lock;
     68        spinlock lock;
    6969};
    7070
  • src/libcfa/concurrency/monitor.c

    r6d2386e r3edc2df  
    3434static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors );
    3535
    36 static inline void lock_all  ( __spinlock_t * locks [], __lock_size_t count );
    37 static inline void lock_all  ( monitor_desc * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count );
    38 static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count );
     36static inline void lock_all  ( spinlock * locks [], __lock_size_t count );
     37static inline void lock_all  ( monitor_desc * source [], spinlock * /*out*/ locks [], __lock_size_t count );
     38static inline void unlock_all( spinlock * locks [], __lock_size_t count );
    3939static inline void unlock_all( monitor_desc * locks [], __lock_size_t count );
    4040
    41 static inline void save   ( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
    42 static inline void restore( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] );
     41static inline void save   ( monitor_desc * ctx [], __lock_size_t count, spinlock * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
     42static inline void restore( monitor_desc * ctx [], __lock_size_t count, spinlock * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] );
    4343
    4444static inline void init     ( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
     
    5353static inline __lock_size_t count_max    ( const __waitfor_mask_t & mask );
    5454static inline __lock_size_t aggregate    ( monitor_desc * storage [], const __waitfor_mask_t & mask );
    55 
    56 #ifndef __CFA_LOCK_NO_YIELD
    57 #define DO_LOCK lock_yield
    58 #else
    59 #define DO_LOCK lock
    60 #endif
    6155
    6256//-----------------------------------------------------------------------------
     
    7771        unsigned int recursions[ count ];                         /* Save the current recursion levels to restore them later                             */ \
    7872        __waitfor_mask_t masks [ count ];                         /* Save the current waitfor masks to restore them later                                */ \
    79         __spinlock_t *   locks [ count ];                         /* We need to pass-in an array of locks to BlockInternal                               */ \
     73        spinlock *   locks    [ count ];                         /* We need to pass-in an array of locks to BlockInternal                               */ \
    8074
    8175#define monitor_save    save   ( monitors, count, locks, recursions, masks )
     
    9084        // Enter single monitor
    9185        static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
    92                 // Lock the monitor spinlock
    93                 DO_LOCK( this->lock DEBUG_CTX2 );
     86                // Lock the monitor spinlock, lock_yield to reduce contention
     87                lock_yield( &this->lock DEBUG_CTX2 );
    9488                thread_desc * thrd = this_thread;
    9589
     
    133127
    134128                // Release the lock and leave
    135                 unlock( this->lock );
     129                unlock( &this->lock );
    136130                return;
    137131        }
    138132
    139133        static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) {
    140                 // Lock the monitor spinlock
    141                 DO_LOCK( this->lock DEBUG_CTX2 );
     134                // Lock the monitor spinlock, lock_yield to reduce contention
     135                lock_yield( &this->lock DEBUG_CTX2 );
    142136                thread_desc * thrd = this_thread;
    143137
     
    151145                        set_owner( this, thrd );
    152146
    153                         unlock( this->lock );
     147                        unlock( &this->lock );
    154148                        return;
    155149                }
     
    202196        // Leave single monitor
    203197        void __leave_monitor_desc( monitor_desc * this ) {
    204                 // Lock the monitor spinlock, DO_LOCK to reduce contention
    205                 DO_LOCK( this->lock DEBUG_CTX2 );
     198                // Lock the monitor spinlock, lock_yield to reduce contention
     199                lock_yield( &this->lock DEBUG_CTX2 );
    206200
    207201                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
     
    216210                if( this->recursion != 0) {
    217211                        LIB_DEBUG_PRINT_SAFE("Kernel :  recursion still %d\n", this->recursion);
    218                         unlock( this->lock );
     212                        unlock( &this->lock );
    219213                        return;
    220214                }
     
    224218
    225219                // We can now let other threads in safely
    226                 unlock( this->lock );
     220                unlock( &this->lock );
    227221
    228222                //We need to wake-up the thread
     
    249243
    250244                // Lock the monitor now
    251                 DO_LOCK( this->lock DEBUG_CTX2 );
     245                lock_yield( &this->lock DEBUG_CTX2 );
    252246
    253247                disable_interrupts();
     
    736730}
    737731
    738 static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
     732static inline void lock_all( spinlock * locks [], __lock_size_t count ) {
    739733        for( __lock_size_t i = 0; i < count; i++ ) {
    740                 DO_LOCK( *locks[i] DEBUG_CTX2 );
    741         }
    742 }
    743 
    744 static inline void lock_all( monitor_desc * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ) {
     734                lock_yield( locks[i] DEBUG_CTX2 );
     735        }
     736}
     737
     738static inline void lock_all( monitor_desc * source [], spinlock * /*out*/ locks [], __lock_size_t count ) {
    745739        for( __lock_size_t i = 0; i < count; i++ ) {
    746                 __spinlock_t * l = &source[i]->lock;
    747                 DO_LOCK( *l DEBUG_CTX2 );
     740                spinlock * l = &source[i]->lock;
     741                lock_yield( l DEBUG_CTX2 );
    748742                if(locks) locks[i] = l;
    749743        }
    750744}
    751745
    752 static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) {
     746static inline void unlock_all( spinlock * locks [], __lock_size_t count ) {
    753747        for( __lock_size_t i = 0; i < count; i++ ) {
    754                 unlock( *locks[i] );
     748                unlock( locks[i] );
    755749        }
    756750}
     
    758752static inline void unlock_all( monitor_desc * locks [], __lock_size_t count ) {
    759753        for( __lock_size_t i = 0; i < count; i++ ) {
    760                 unlock( locks[i]->lock );
     754                unlock( &locks[i]->lock );
    761755        }
    762756}
     
    765759        monitor_desc * ctx [],
    766760        __lock_size_t count,
    767         __attribute((unused)) __spinlock_t * locks [],
     761        __attribute((unused)) spinlock * locks [],
    768762        unsigned int /*out*/ recursions [],
    769763        __waitfor_mask_t /*out*/ masks []
     
    778772        monitor_desc * ctx [],
    779773        __lock_size_t count,
    780         __spinlock_t * locks [],
     774        spinlock * locks [],
    781775        unsigned int /*out*/ recursions [],
    782776        __waitfor_mask_t /*out*/ masks []
  • src/libcfa/concurrency/preemption.c

    r6d2386e r3edc2df  
    355355                case SI_KERNEL:
    356356                        // LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread tick\n");
    357                         lock( event_kernel->lock DEBUG_CTX2 );
     357                        lock( &event_kernel->lock DEBUG_CTX2 );
    358358                        tick_preemption();
    359                         unlock( event_kernel->lock );
     359                        unlock( &event_kernel->lock );
    360360                        break;
    361361                // Signal was not sent by the kernel but by an other thread
Note: See TracChangeset for help on using the changeset viewer.