Ignore:
Timestamp:
Mar 31, 2021, 1:48:15 PM (8 months ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
arm-eh, jacob/cs343-translation, master, new-ast-unique-expr
Children:
c7c178b
Parents:
f621e43f
Message:

Changed is_blocking_lock trait to use on_wakeup.
Fixed fast_lock to work with conditions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/locks.hfa

    rf621e43f r22b7579  
    119119
    120120static inline bool P(ThreadBenaphore & this)              { return P(this.ben) ? false : P(this.sem); }
    121 static inline bool P(ThreadBenaphore & this, $thread * t) { return P(this.ben) ? false : P(this.sem, t ); }
    122121static inline bool tryP(ThreadBenaphore & this)           { return tryP(this.ben); }
    123122static inline bool P(ThreadBenaphore & this, bool wait)   { return wait ? P(this) : tryP(this); }
     
    141140bool   V (semaphore & this);
    142141bool   V (semaphore & this, unsigned count);
     142$thread * V (semaphore & this, bool );
    143143
    144144//----------
     
    149149static inline void  ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };}
    150150static inline void ^?{}( single_acquisition_lock & this ) {}
    151 static inline void   lock      ( single_acquisition_lock & this ) { lock    ( (blocking_lock &)this ); }
    152 static inline bool   try_lock  ( single_acquisition_lock & this ) { return try_lock( (blocking_lock &)this ); }
    153 static inline void   unlock    ( single_acquisition_lock & this ) { unlock  ( (blocking_lock &)this ); }
    154 static inline void   on_wait   ( single_acquisition_lock & this ) { on_wait ( (blocking_lock &)this ); }
    155 static inline void   on_notify ( single_acquisition_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
    156 static inline void   set_recursion_count( single_acquisition_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); }
    157 static inline size_t get_recursion_count( single_acquisition_lock & this ) { return get_recursion_count( (blocking_lock &)this ); }
     151static inline void   lock     ( single_acquisition_lock & this ) { lock    ( (blocking_lock &)this ); }
     152static inline bool   try_lock ( single_acquisition_lock & this ) { return try_lock( (blocking_lock &)this ); }
     153static inline void   unlock   ( single_acquisition_lock & this ) { unlock  ( (blocking_lock &)this ); }
     154static inline size_t on_wait  ( single_acquisition_lock & this ) { return on_wait ( (blocking_lock &)this ); }
     155static inline void   on_wakeup( single_acquisition_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); }
     156static inline void   on_notify( single_acquisition_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
    158157
    159158//----------
     
    167166static inline bool   try_lock ( owner_lock & this ) { return try_lock( (blocking_lock &)this ); }
    168167static inline void   unlock   ( owner_lock & this ) { unlock  ( (blocking_lock &)this ); }
    169 static inline void   on_wait  ( owner_lock & this ) { on_wait ( (blocking_lock &)this ); }
     168static inline size_t on_wait  ( owner_lock & this ) { return on_wait ( (blocking_lock &)this ); }
     169static inline void   on_wakeup( owner_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); }
    170170static inline void   on_notify( owner_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
    171 static inline void   set_recursion_count( owner_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); }
    172 static inline size_t get_recursion_count( owner_lock & this ) { return get_recursion_count( (blocking_lock &)this ); }
    173171
    174172struct fast_lock {
     
    182180}
    183181
    184 static inline void $lock(fast_lock & this, $thread * thrd) {
    185         /* paranoid */verify(thrd != this.owner);
    186 
    187         for (;;) {
    188                 if ($try_lock(this, thrd)) return;
    189                 P(this.sem, thrd);
    190         }
    191 }
    192 
     182static inline void lock( fast_lock & this ) __attribute__((artificial));
    193183static inline void lock( fast_lock & this ) {
    194184        $thread * thrd = active_thread();
     
    201191}
    202192
    203 static inline void try_lock ( fast_lock & this ) {
     193static inline bool try_lock( fast_lock & this ) __attribute__((artificial));
     194static inline bool try_lock ( fast_lock & this ) {
    204195        $thread * thrd = active_thread();
    205196        /* paranoid */ verify(thrd != this.owner);
     
    207198}
    208199
    209 static inline void unlock( fast_lock & this ) {
     200static inline $thread * unlock( fast_lock & this ) __attribute__((artificial));
     201static inline $thread * unlock( fast_lock & this ) {
    210202        $thread * thrd = active_thread();
    211203        /* paranoid */ verify(thrd == this.owner);
    212         $thread * next = V(this.sem, false); // implicit fence
    213         // open 'owner' only after fence
     204
     205        // open 'owner' before unlocking anyone
     206        // so new and unlocked threads don't park incorrectly.
    214207        this.owner = 0p;
    215208
    216         // Unpark the next person (can be 0p, unpark handles it)
    217         unpark(next);
    218 }
    219 
    220 static inline void on_wait( fast_lock & this ) {
    221         unlock(this);
    222         #warning this is broken
    223 }
    224 
    225 static inline void on_notify( fast_lock & this, struct $thread * t ) {
    226         $lock(this, t);
    227         #warning this is broken
    228 }
    229 
    230 static inline void   set_recursion_count( fast_lock & this, size_t recursion ) {}
    231 static inline size_t get_recursion_count( fast_lock & this ) { return 0; }
     209        return V(this.sem,true);
     210}
     211
     212static inline size_t on_wait( fast_lock & this ) __attribute__((artificial));
     213static inline void on_wakeup( fast_lock & this, size_t ) __attribute__((artificial));
     214static inline void on_notify( fast_lock &, struct $thread * t ) __attribute__((artificial));
     215
     216static inline size_t on_wait( fast_lock & this ) { unlock(this); return 0; }
     217static inline void on_wakeup( fast_lock & this, size_t ) { lock(this); }
     218static inline void on_notify( fast_lock &, struct $thread * t ) { unpark(t); }
    232219
    233220struct mcs_node {
     
    263250
    264251        // For synchronization locks to use when releasing
    265         void on_wait( L & );
    266 
    267         // to get recursion count for cond lock to reset after waking
    268         size_t get_recursion_count( L & );
     252        size_t on_wait( L & );
    269253
    270254        // to set recursion count after getting signalled;
    271         void set_recursion_count( L &, size_t recursion );
     255        void on_wakeup( L &, size_t recursion );
    272256};
    273257
Note: See TracChangeset for help on using the changeset viewer.