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