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