Changeset ea7d2b0


Ignore:
Timestamp:
Nov 9, 2017, 1:58:39 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
34c6c767
Parents:
3351cc0
Message:

Moved spinlocks to bits/locks.h

Location:
src/libcfa
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/Makefile.am

    r3351cc0 rea7d2b0  
    9595
    9696cfa_includedir = $(CFA_INCDIR)
    97 nobase_cfa_include_HEADERS = ${headers} ${stdhdr} math gmp concurrency/invoke.h
     97nobase_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
    98109
    99110CLEANFILES = libcfa-prelude.c
  • src/libcfa/Makefile.in

    r3351cc0 rea7d2b0  
    264264        containers/result containers/vector concurrency/coroutine \
    265265        concurrency/thread concurrency/kernel concurrency/monitor \
    266         ${shell echo stdhdr/*} math gmp concurrency/invoke.h
     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
    267269HEADERS = $(nobase_cfa_include_HEADERS)
    268270am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP)
     
    430432stdhdr = ${shell echo stdhdr/*}
    431433cfa_includedir = $(CFA_INCDIR)
    432 nobase_cfa_include_HEADERS = ${headers} ${stdhdr} math gmp concurrency/invoke.h
     434nobase_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
    433447CLEANFILES = libcfa-prelude.c
    434448all: all-am
  • src/libcfa/concurrency/alarm.c

    r3351cc0 rea7d2b0  
    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

    r3351cc0 rea7d2b0  
    1414//
    1515
    16 #include <stdbool.h>
    17 #include <stdint.h>
     16#include "bits/defs.h"
     17#include "bits/locks.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 
    3027        typedef void (*fptr_t)();
    3128        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         };
    4029
    4130        struct __thread_queue_t {
     
    5847                void push( struct __condition_stack_t &, struct __condition_criterion_t * );
    5948                struct __condition_criterion_t * pop( struct __condition_stack_t & );
    60 
    61                 void  ?{}(spinlock & this);
    62                 void ^?{}(spinlock & this);
    6349        }
    6450        #endif
     
    122108        struct monitor_desc {
    123109                // spinlock to protect internal data
    124                 struct spinlock lock;
     110                struct __spinlock_t lock;
    125111
    126112                // current owner of the monitor
  • src/libcfa/concurrency/kernel

    r3351cc0 rea7d2b0  
    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
     29// void 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
     32// void 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
     35// bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );
    3636
    37 // Unlock the spinlock
    38 void unlock    ( spinlock * );
     37// // Unlock the spinlock
     38// void unlock    ( spinlock * );
    3939
    4040struct semaphore {
    41         spinlock lock;
     41        __spinlock_t lock;
    4242        int count;
    4343        __thread_queue_t waiting;
     
    5454struct cluster {
    5555        // Ready queue locks
    56         spinlock ready_queue_lock;
     56        __spinlock_t ready_queue_lock;
    5757
    5858        // Ready queue for threads
     
    7474        FinishOpCode action_code;
    7575        thread_desc * thrd;
    76         spinlock * lock;
    77         spinlock ** locks;
     76        __spinlock_t * lock;
     77        __spinlock_t ** locks;
    7878        unsigned short lock_count;
    7979        thread_desc ** thrds;
  • src/libcfa/concurrency/kernel.c

    r3351cc0 rea7d2b0  
    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 * lock ) {
     360void BlockInternal( __spinlock_t * lock ) {
    361361        disable_interrupts();
    362362        this_processor->finish.action_code = Release;
     
    384384}
    385385
    386 void BlockInternal( spinlock * lock, thread_desc * thrd ) {
     386void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
    387387        assert(thrd);
    388388        disable_interrupts();
     
    398398}
    399399
    400 void BlockInternal(spinlock * locks [], unsigned short count) {
     400void BlockInternal(__spinlock_t * locks [], unsigned short count) {
    401401        disable_interrupts();
    402402        this_processor->finish.action_code = Release_Multi;
     
    411411}
    412412
    413 void BlockInternal(spinlock * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
     413void BlockInternal(__spinlock_t * 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 * lock, thread_desc * thrd) {
     428void LeaveThread(__spinlock_t * 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 kernel_abort_lock;
    519 static spinlock kernel_debug_lock;
     518static __spinlock_t kernel_abort_lock;
     519static __spinlock_t 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
    576 void ?{}( spinlock & this ) {
    577         this.lock = 0;
    578 }
    579 void ^?{}( spinlock & this ) {
    580 
    581 }
    582 
    583 bool try_lock( spinlock * this DEBUG_CTX_PARAM2 ) {
    584         return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
    585 }
    586 
    587 void 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 
    597 void 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 
    609 void unlock( spinlock * this ) {
    610         __sync_lock_release_4( &this->lock );
    611 }
    612 
    613576void  ?{}( semaphore & this, int count = 1 ) {
    614577        (this.lock){};
     
    619582
    620583void P(semaphore & this) {
    621         lock( &this.lock DEBUG_CTX2 );
     584        lock( this.lock DEBUG_CTX2 );
    622585        this.count -= 1;
    623586        if ( this.count < 0 ) {
     
    629592        }
    630593        else {
    631             unlock( &this.lock );
     594            unlock( this.lock );
    632595        }
    633596}
     
    635598void V(semaphore & this) {
    636599        thread_desc * thrd = NULL;
    637         lock( &this.lock DEBUG_CTX2 );
     600        lock( this.lock DEBUG_CTX2 );
    638601        this.count += 1;
    639602        if ( this.count <= 0 ) {
     
    642605        }
    643606
    644         unlock( &this.lock );
     607        unlock( this.lock );
    645608
    646609        // make new owner
  • src/libcfa/concurrency/kernel_private.h

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

    r3351cc0 rea7d2b0  
    3434static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors );
    3535
    36 static inline void lock_all  ( spinlock * locks [], __lock_size_t count );
    37 static inline void lock_all  ( monitor_desc * source [], spinlock * /*out*/ locks [], __lock_size_t count );
    38 static inline void unlock_all( spinlock * locks [], __lock_size_t count );
     36static inline void lock_all  ( __spinlock_t * locks [], __lock_size_t count );
     37static inline void lock_all  ( monitor_desc * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count );
     38static inline void unlock_all( __spinlock_t * 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 * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
    42 static inline void restore( monitor_desc * ctx [], __lock_size_t count, spinlock * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] );
     41static inline void save   ( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
     42static inline void restore( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * 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 [] );
     
    7171        unsigned int recursions[ count ];                         /* Save the current recursion levels to restore them later                             */ \
    7272        __waitfor_mask_t masks [ count ];                         /* Save the current waitfor masks to restore them later                                */ \
    73         spinlock *   locks    [ count ];                         /* We need to pass-in an array of locks to BlockInternal                               */ \
     73        __spinlock_t *   locks [ count ];                         /* We need to pass-in an array of locks to BlockInternal                               */ \
    7474
    7575#define monitor_save    save   ( monitors, count, locks, recursions, masks )
     
    8585        static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
    8686                // Lock the monitor spinlock, lock_yield to reduce contention
    87                 lock_yield( &this->lock DEBUG_CTX2 );
     87                lock_yield( this->lock DEBUG_CTX2 );
    8888                thread_desc * thrd = this_thread;
    8989
     
    127127
    128128                // Release the lock and leave
    129                 unlock( &this->lock );
     129                unlock( this->lock );
    130130                return;
    131131        }
     
    133133        static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) {
    134134                // Lock the monitor spinlock, lock_yield to reduce contention
    135                 lock_yield( &this->lock DEBUG_CTX2 );
     135                lock_yield( this->lock DEBUG_CTX2 );
    136136                thread_desc * thrd = this_thread;
    137137
     
    145145                        set_owner( this, thrd );
    146146
    147                         unlock( &this->lock );
     147                        unlock( this->lock );
    148148                        return;
    149149                }
     
    197197        void __leave_monitor_desc( monitor_desc * this ) {
    198198                // Lock the monitor spinlock, lock_yield to reduce contention
    199                 lock_yield( &this->lock DEBUG_CTX2 );
     199                lock_yield( this->lock DEBUG_CTX2 );
    200200
    201201                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
     
    210210                if( this->recursion != 0) {
    211211                        LIB_DEBUG_PRINT_SAFE("Kernel :  recursion still %d\n", this->recursion);
    212                         unlock( &this->lock );
     212                        unlock( this->lock );
    213213                        return;
    214214                }
     
    218218
    219219                // We can now let other threads in safely
    220                 unlock( &this->lock );
     220                unlock( this->lock );
    221221
    222222                //We need to wake-up the thread
     
    243243
    244244                // Lock the monitor now
    245                 lock_yield( &this->lock DEBUG_CTX2 );
     245                lock_yield( this->lock DEBUG_CTX2 );
    246246
    247247                disable_interrupts();
     
    730730}
    731731
    732 static inline void lock_all( spinlock * locks [], __lock_size_t count ) {
     732static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
    733733        for( __lock_size_t i = 0; i < count; i++ ) {
    734                 lock_yield( locks[i] DEBUG_CTX2 );
    735         }
    736 }
    737 
    738 static inline void lock_all( monitor_desc * source [], spinlock * /*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_t * /*out*/ locks [], __lock_size_t count ) {
    739739        for( __lock_size_t i = 0; i < count; i++ ) {
    740                 spinlock * l = &source[i]->lock;
    741                 lock_yield( l DEBUG_CTX2 );
     740                __spinlock_t * l = &source[i]->lock;
     741                lock_yield( *l DEBUG_CTX2 );
    742742                if(locks) locks[i] = l;
    743743        }
    744744}
    745745
    746 static inline void unlock_all( spinlock * locks [], __lock_size_t count ) {
     746static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) {
    747747        for( __lock_size_t i = 0; i < count; i++ ) {
    748                 unlock( locks[i] );
     748                unlock( *locks[i] );
    749749        }
    750750}
     
    752752static inline void unlock_all( monitor_desc * locks [], __lock_size_t count ) {
    753753        for( __lock_size_t i = 0; i < count; i++ ) {
    754                 unlock( &locks[i]->lock );
     754                unlock( locks[i]->lock );
    755755        }
    756756}
     
    759759        monitor_desc * ctx [],
    760760        __lock_size_t count,
    761         __attribute((unused)) spinlock * locks [],
     761        __attribute((unused)) __spinlock_t * locks [],
    762762        unsigned int /*out*/ recursions [],
    763763        __waitfor_mask_t /*out*/ masks []
     
    772772        monitor_desc * ctx [],
    773773        __lock_size_t count,
    774         spinlock * locks [],
     774        __spinlock_t * locks [],
    775775        unsigned int /*out*/ recursions [],
    776776        __waitfor_mask_t /*out*/ masks []
  • src/libcfa/concurrency/preemption.c

    r3351cc0 rea7d2b0  
    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.