Ignore:
File:
1 edited

Legend:

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

    rac5816d r4aeaee5  
    33#include <stdbool.h>
    44
     5#include "bits/algorithm.hfa"
    56#include "bits/locks.hfa"
    67#include "bits/sequence.hfa"
     8#include "bits/containers.hfa"
    79
    810#include "invoke.h"
     
    1012#include "time_t.hfa"
    1113#include "time.hfa"
     14#include <sys/time.h>
     15#include "alarm.hfa"
    1216
    13 //-----------------------------------------------------------------------------
    14 // is_blocking_lock
     17///////////////////////////////////////////////////////////////////
     18//// is_blocking_lock
     19///////////////////////////////////////////////////////////////////
     20
    1521trait is_blocking_lock(dtype L | sized(L)) {
    16         // For synchronization locks to use when acquiring
    17         void on_notify( L &, struct $thread * );
    18 
    19         // For synchronization locks to use when releasing
    20         void on_wait( L & );
    21 
    22         // to get recursion count for cond lock to reset after waking
    23         size_t get_recursion_count( L & );
    24 
    25         // to set recursion count after getting signalled;
    26         void set_recursion_count( L &, size_t recursion );
     22        void add_( L &, struct $thread * );             // For synchronization locks to use when acquiring
     23        void remove_( L & );    // For synchronization locks to use when releasing
     24        size_t get_recursion_count( L & ); // to get recursion count for cond lock to reset after waking
     25        void set_recursion_count( L &, size_t recursion ); // to set recursion count after getting signalled;
    2726};
    2827
    29 //-----------------------------------------------------------------------------
    30 // info_thread
    31 // the info thread is a wrapper around a thread used
    32 // to store extra data for use in the condition variable
     28///////////////////////////////////////////////////////////////////
     29//// info_thread
     30///////////////////////////////////////////////////////////////////
     31
    3332forall(dtype L | is_blocking_lock(L)) {
    34         struct info_thread;
     33        struct info_thread {
     34                inline Seqable;
     35                struct $thread * t;
     36                uintptr_t info;
     37                L * lock;
     38                bool listed;                                    // true if info_thread is on queue, false otherwise;
     39        };
    3540
    36         // for use by sequence
    37         info_thread(L) *& Back( info_thread(L) * this );
    38         info_thread(L) *& Next( info_thread(L) * this );
     41
     42        void ?{}( info_thread(L) & this, $thread * t );
     43        void ?{}( info_thread(L) & this, $thread * t, uintptr_t info );
     44        void ^?{}( info_thread(L) & this );
    3945}
    4046
    41 //-----------------------------------------------------------------------------
    42 // Blocking Locks
     47///////////////////////////////////////////////////////////////////
     48//// Blocking Locks
     49///////////////////////////////////////////////////////////////////
     50
     51// struct lock_thread {
     52//      struct $thread * t;
     53//      lock_thread * next;
     54// };
     55
     56// void ?{}( lock_thread & this, struct $thread * thd );
     57// void ^?{}( lock_thread & this );
     58
     59// lock_thread *& get_next( lock_thread & );
     60
    4361struct blocking_lock {
    4462        // Spin lock used for mutual exclusion
     
    4664
    4765        // List of blocked threads
    48         Sequence( $thread ) blocked_threads;
     66        __queue_t( $thread ) blocked_threads;
    4967
    5068        // Count of current blocked threads
     
    7694};
    7795
    78 void  ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner );
     96void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner );
    7997void ^?{}( blocking_lock & this );
    8098
    81 void  ?{}( single_acquisition_lock & this );
     99void ?{}( single_acquisition_lock & this );
    82100void ^?{}( single_acquisition_lock & this );
    83101
    84 void  ?{}( owner_lock & this );
     102void ?{}( owner_lock & this );
    85103void ^?{}( owner_lock & this );
    86104
    87 void  ?{}( multiple_acquisition_lock & this );
     105void ?{}( multiple_acquisition_lock & this );
    88106void ^?{}( multiple_acquisition_lock & this );
    89107
     
    91109bool try_lock( blocking_lock & this );
    92110void unlock( blocking_lock & this );
    93 void on_notify( blocking_lock & this, struct $thread * t );
    94 void on_wait( blocking_lock & this );
     111void add_( blocking_lock & this, struct $thread * t );
     112void remove_( blocking_lock & this );
    95113size_t wait_count( blocking_lock & this );
    96114void set_recursion_count( blocking_lock & this, size_t recursion );
     
    99117void lock( single_acquisition_lock & this );
    100118void unlock( single_acquisition_lock & this );
    101 void on_notify( single_acquisition_lock & this, struct $thread * t );
    102 void on_wait( single_acquisition_lock & this );
     119void add_( single_acquisition_lock & this, struct $thread * t );
     120void remove_( single_acquisition_lock & this );
    103121void set_recursion_count( single_acquisition_lock & this, size_t recursion );
    104122size_t get_recursion_count( single_acquisition_lock & this );
     
    106124void lock( owner_lock & this );
    107125void unlock( owner_lock & this );
    108 void on_notify( owner_lock & this, struct $thread * t );
    109 void on_wait( owner_lock & this );
     126void add_( owner_lock & this, struct $thread * t );
     127void remove_( owner_lock & this );
    110128void set_recursion_count( owner_lock & this, size_t recursion );
    111129size_t get_recursion_count( owner_lock & this );
     
    113131void lock( multiple_acquisition_lock & this );
    114132void unlock( multiple_acquisition_lock & this );
    115 void on_notify( multiple_acquisition_lock & this, struct $thread * t );
    116 void on_wait( multiple_acquisition_lock & this );
     133void add_( multiple_acquisition_lock & this, struct $thread * t );
     134void remove_( multiple_acquisition_lock & this );
    117135void set_recursion_count( multiple_acquisition_lock & this, size_t recursion );
    118136size_t get_recursion_count( multiple_acquisition_lock & this );
    119137
    120 //-----------------------------------------------------------------------------
    121 // Synchronization Locks
     138///////////////////////////////////////////////////////////////////
     139//// Synchronization Locks
     140///////////////////////////////////////////////////////////////////
    122141forall(dtype L | is_blocking_lock(L)) {
    123142        struct condition_variable {
    124143                // Spin lock used for mutual exclusion
    125144                __spinlock_t lock;
     145
     146                info_thread(L) * last_thread;
    126147
    127148                // List of blocked threads
     
    132153        };
    133154
    134         void  ?{}( condition_variable(L) & this );
     155        void ?{}( condition_variable(L) & this );
    135156        void ^?{}( condition_variable(L) & this );
     157
     158        struct alarm_node_wrap {
     159                alarm_node_t alarm_node;
     160
     161                condition_variable(L) * cond;
     162
     163                info_thread(L) * i;
     164        };
     165
     166        void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback );
     167        void ^?{}( alarm_node_wrap(L) & this );
     168
     169        void alarm_node_callback( alarm_node_wrap(L) & this );
     170
     171        void alarm_node_wrap_cast( alarm_node_t & a );
    136172
    137173        bool notify_one( condition_variable(L) & this );
     
    140176        uintptr_t front( condition_variable(L) & this );
    141177
    142         bool empty  ( condition_variable(L) & this );
    143         int  counter( condition_variable(L) & this );
     178        bool empty( condition_variable(L) & this );
     179        int counter( condition_variable(L) & this );
    144180
     181        // TODO: look into changing timout routines to return bool showing if signalled or woken by kernel
    145182        void wait( condition_variable(L) & this );
    146183        void wait( condition_variable(L) & this, uintptr_t info );
    147         bool wait( condition_variable(L) & this, Duration duration );
    148         bool wait( condition_variable(L) & this, uintptr_t info, Duration duration );
    149         bool wait( condition_variable(L) & this, Time time );
    150         bool wait( condition_variable(L) & this, uintptr_t info, Time time );
     184        void wait( condition_variable(L) & this, Duration duration );
     185        void wait( condition_variable(L) & this, uintptr_t info, Duration duration );
     186        void wait( condition_variable(L) & this, Time time );
     187        void wait( condition_variable(L) & this, uintptr_t info, Time time );
    151188
    152189        void wait( condition_variable(L) & this, L & l );
    153190        void wait( condition_variable(L) & this, L & l, uintptr_t info );
    154         bool wait( condition_variable(L) & this, L & l, Duration duration );
    155         bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration );
    156         bool wait( condition_variable(L) & this, L & l, Time time );
    157         bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time );
     191        void wait( condition_variable(L) & this, L & l, Duration duration );
     192        void wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration );
     193        void wait( condition_variable(L) & this, L & l, Time time );
     194        void wait( condition_variable(L) & this, L & l, uintptr_t info, Time time );
    158195}
Note: See TracChangeset for help on using the changeset viewer.