| [f4e35326] | 1 | #pragma once
 | 
|---|
 | 2 | 
 | 
|---|
| [848439f] | 3 | #include <stdbool.h>
 | 
|---|
 | 4 | 
 | 
|---|
 | 5 | #include "bits/algorithm.hfa"
 | 
|---|
 | 6 | #include "bits/locks.hfa"
 | 
|---|
| [cad1df1] | 7 | #include "bits/sequence.hfa"
 | 
|---|
| [848439f] | 8 | #include "bits/containers.hfa"
 | 
|---|
 | 9 | 
 | 
|---|
 | 10 | #include "invoke.h"
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | #include "time_t.hfa"
 | 
|---|
 | 13 | #include "time.hfa"
 | 
|---|
 | 14 | #include <sys/time.h>
 | 
|---|
| [eeb5023] | 15 | #include "alarm.hfa"
 | 
|---|
| [848439f] | 16 | 
 | 
|---|
 | 17 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 18 | //// is_blocking_lock
 | 
|---|
 | 19 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | trait is_blocking_lock(dtype L | sized(L)) {
 | 
|---|
 | 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;
 | 
|---|
 | 26 | };
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 29 | //// info_thread
 | 
|---|
 | 30 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | forall(dtype L | is_blocking_lock(L)) {
 | 
|---|
 | 33 |         struct info_thread {
 | 
|---|
| [cad1df1] | 34 |                 inline Seqable;
 | 
|---|
| [848439f] | 35 |                 struct $thread * t;
 | 
|---|
 | 36 |                 uintptr_t info;
 | 
|---|
 | 37 |                 L * lock;
 | 
|---|
| [eeb5023] | 38 |                 bool listed;                                    // true if info_thread is on queue, false otherwise;
 | 
|---|
| [848439f] | 39 |         };
 | 
|---|
 | 40 | 
 | 
|---|
 | 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 );
 | 
|---|
 | 45 | }
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 48 | //// Blocking Locks
 | 
|---|
 | 49 | ///////////////////////////////////////////////////////////////////
 | 
|---|
| [6a8882c] | 50 | 
 | 
|---|
| [cad1df1] | 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 | 
 | 
|---|
| [848439f] | 61 | struct blocking_lock {
 | 
|---|
 | 62 |         // Spin lock used for mutual exclusion
 | 
|---|
 | 63 |         __spinlock_t lock;
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |         // List of blocked threads
 | 
|---|
| [cad1df1] | 66 |         __queue_t( $thread ) blocked_threads;
 | 
|---|
| [848439f] | 67 | 
 | 
|---|
 | 68 |         // Count of current blocked threads
 | 
|---|
 | 69 |         size_t wait_count;
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |         // Flag if the lock allows multiple acquisition
 | 
|---|
 | 72 |         bool multi_acquisition;
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |         // Flag if lock can be released by non owner
 | 
|---|
 | 75 |         bool strict_owner;
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |         // Current thread owning the lock
 | 
|---|
 | 78 |         struct $thread * owner;
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 |         // Number of recursion level
 | 
|---|
 | 81 |         size_t recursion_count;
 | 
|---|
 | 82 | };
 | 
|---|
 | 83 | 
 | 
|---|
| [6a8882c] | 84 | struct single_acquisition_lock {
 | 
|---|
| [848439f] | 85 |         inline blocking_lock;
 | 
|---|
 | 86 | };
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 | struct owner_lock {
 | 
|---|
 | 89 |         inline blocking_lock;
 | 
|---|
 | 90 | };
 | 
|---|
 | 91 | 
 | 
|---|
| [6a8882c] | 92 | struct multiple_acquisition_lock {
 | 
|---|
| [848439f] | 93 |         inline blocking_lock;
 | 
|---|
 | 94 | };
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner );
 | 
|---|
 | 97 | void ^?{}( blocking_lock & this );
 | 
|---|
 | 98 | 
 | 
|---|
| [6a8882c] | 99 | void ?{}( single_acquisition_lock & this );
 | 
|---|
 | 100 | void ^?{}( single_acquisition_lock & this );
 | 
|---|
| [848439f] | 101 | 
 | 
|---|
 | 102 | void ?{}( owner_lock & this );
 | 
|---|
 | 103 | void ^?{}( owner_lock & this );
 | 
|---|
 | 104 | 
 | 
|---|
| [6a8882c] | 105 | void ?{}( multiple_acquisition_lock & this );
 | 
|---|
 | 106 | void ^?{}( multiple_acquisition_lock & this );
 | 
|---|
| [848439f] | 107 | 
 | 
|---|
 | 108 | void lock( blocking_lock & this );
 | 
|---|
 | 109 | bool try_lock( blocking_lock & this );
 | 
|---|
 | 110 | void unlock( blocking_lock & this );
 | 
|---|
 | 111 | void add_( blocking_lock & this, struct $thread * t );
 | 
|---|
 | 112 | void remove_( blocking_lock & this );
 | 
|---|
 | 113 | size_t wait_count( blocking_lock & this );
 | 
|---|
 | 114 | void set_recursion_count( blocking_lock & this, size_t recursion );
 | 
|---|
 | 115 | size_t get_recursion_count( blocking_lock & this );
 | 
|---|
 | 116 | 
 | 
|---|
| [6a8882c] | 117 | void lock( single_acquisition_lock & this );
 | 
|---|
 | 118 | void unlock( single_acquisition_lock & this );
 | 
|---|
 | 119 | void add_( single_acquisition_lock & this, struct $thread * t );
 | 
|---|
 | 120 | void remove_( single_acquisition_lock & this );
 | 
|---|
 | 121 | void set_recursion_count( single_acquisition_lock & this, size_t recursion );
 | 
|---|
 | 122 | size_t get_recursion_count( single_acquisition_lock & this );
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 | void lock( owner_lock & this );
 | 
|---|
 | 125 | void unlock( owner_lock & this );
 | 
|---|
 | 126 | void add_( owner_lock & this, struct $thread * t );
 | 
|---|
 | 127 | void remove_( owner_lock & this );
 | 
|---|
 | 128 | void set_recursion_count( owner_lock & this, size_t recursion );
 | 
|---|
 | 129 | size_t get_recursion_count( owner_lock & this );
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | void lock( multiple_acquisition_lock & this );
 | 
|---|
 | 132 | void unlock( multiple_acquisition_lock & this );
 | 
|---|
 | 133 | void add_( multiple_acquisition_lock & this, struct $thread * t );
 | 
|---|
 | 134 | void remove_( multiple_acquisition_lock & this );
 | 
|---|
 | 135 | void set_recursion_count( multiple_acquisition_lock & this, size_t recursion );
 | 
|---|
 | 136 | size_t get_recursion_count( multiple_acquisition_lock & this );
 | 
|---|
| [848439f] | 137 | 
 | 
|---|
 | 138 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 139 | //// Synchronization Locks
 | 
|---|
 | 140 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 141 | forall(dtype L | is_blocking_lock(L)) {
 | 
|---|
| [eeb5023] | 142 |         struct condition_variable {
 | 
|---|
| [848439f] | 143 |                 // Spin lock used for mutual exclusion
 | 
|---|
 | 144 |                 __spinlock_t lock;
 | 
|---|
 | 145 | 
 | 
|---|
| [cad1df1] | 146 |                 info_thread(L) * last_thread;
 | 
|---|
 | 147 | 
 | 
|---|
| [848439f] | 148 |                 // List of blocked threads
 | 
|---|
| [cad1df1] | 149 |                 Sequence( info_thread(L) ) blocked_threads;
 | 
|---|
| [848439f] | 150 | 
 | 
|---|
 | 151 |                 // Count of current blocked threads
 | 
|---|
 | 152 |                 int count;
 | 
|---|
 | 153 |         };
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |         void ?{}( condition_variable(L) & this );
 | 
|---|
 | 156 |         void ^?{}( condition_variable(L) & this );
 | 
|---|
 | 157 | 
 | 
|---|
| [eeb5023] | 158 |         struct alarm_node_wrap {
 | 
|---|
 | 159 |                 alarm_node_t alarm_node;
 | 
|---|
| [848439f] | 160 | 
 | 
|---|
| [eeb5023] | 161 |                 condition_variable(L) * cond;
 | 
|---|
| [848439f] | 162 | 
 | 
|---|
| [cad1df1] | 163 |                 info_thread(L) * i;
 | 
|---|
| [eeb5023] | 164 |         };
 | 
|---|
| [848439f] | 165 | 
 | 
|---|
| [4aeaee5] | 166 |         void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback );
 | 
|---|
| [eeb5023] | 167 |         void ^?{}( alarm_node_wrap(L) & this );
 | 
|---|
| [848439f] | 168 | 
 | 
|---|
| [eeb5023] | 169 |         void alarm_node_callback( alarm_node_wrap(L) & this );
 | 
|---|
| [848439f] | 170 | 
 | 
|---|
| [eeb5023] | 171 |         void alarm_node_wrap_cast( alarm_node_t & a );
 | 
|---|
| [848439f] | 172 | 
 | 
|---|
| [eeb5023] | 173 |         bool notify_one( condition_variable(L) & this );
 | 
|---|
 | 174 |         bool notify_all( condition_variable(L) & this );
 | 
|---|
| [848439f] | 175 | 
 | 
|---|
| [eeb5023] | 176 |         uintptr_t front( condition_variable(L) & this );
 | 
|---|
| [848439f] | 177 | 
 | 
|---|
| [eeb5023] | 178 |         bool empty( condition_variable(L) & this );
 | 
|---|
 | 179 |         int counter( condition_variable(L) & this );
 | 
|---|
| [848439f] | 180 | 
 | 
|---|
| [eeb5023] | 181 |         // TODO: look into changing timout routines to return bool showing if signalled or woken by kernel
 | 
|---|
 | 182 |         void wait( condition_variable(L) & this );
 | 
|---|
 | 183 |         void wait( condition_variable(L) & this, uintptr_t info );
 | 
|---|
 | 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 );
 | 
|---|
| [848439f] | 188 | 
 | 
|---|
| [eeb5023] | 189 |         void wait( condition_variable(L) & this, L & l );
 | 
|---|
 | 190 |         void wait( condition_variable(L) & this, L & l, uintptr_t info );
 | 
|---|
 | 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 );
 | 
|---|
 | 195 | }
 | 
|---|