[f4e35326] | 1 | #pragma once |
---|
| 2 | |
---|
[848439f] | 3 | #include <stdbool.h> |
---|
| 4 | |
---|
| 5 | #include "bits/locks.hfa" |
---|
[cad1df1] | 6 | #include "bits/sequence.hfa" |
---|
[848439f] | 7 | |
---|
| 8 | #include "invoke.h" |
---|
| 9 | |
---|
| 10 | #include "time_t.hfa" |
---|
| 11 | #include "time.hfa" |
---|
| 12 | |
---|
[ac5816d] | 13 | //----------------------------------------------------------------------------- |
---|
| 14 | // is_blocking_lock |
---|
[848439f] | 15 | trait is_blocking_lock(dtype L | sized(L)) { |
---|
[ac5816d] | 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 & ); |
---|
[848439f] | 21 | |
---|
[ac5816d] | 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 ); |
---|
| 27 | }; |
---|
[848439f] | 28 | |
---|
[ac5816d] | 29 | //----------------------------------------------------------------------------- |
---|
| 30 | // info_thread |
---|
[797a193] | 31 | // the info thread is a wrapper around a thread used |
---|
| 32 | // to store extra data for use in the condition variable |
---|
[848439f] | 33 | forall(dtype L | is_blocking_lock(L)) { |
---|
[ac5816d] | 34 | struct info_thread; |
---|
[c131a02] | 35 | |
---|
[797a193] | 36 | // for use by sequence |
---|
[c131a02] | 37 | info_thread(L) *& Back( info_thread(L) * this ); |
---|
| 38 | info_thread(L) *& Next( info_thread(L) * this ); |
---|
[848439f] | 39 | } |
---|
| 40 | |
---|
[ac5816d] | 41 | //----------------------------------------------------------------------------- |
---|
| 42 | // Blocking Locks |
---|
[848439f] | 43 | struct blocking_lock { |
---|
| 44 | // Spin lock used for mutual exclusion |
---|
| 45 | __spinlock_t lock; |
---|
| 46 | |
---|
| 47 | // List of blocked threads |
---|
[c131a02] | 48 | Sequence( $thread ) blocked_threads; |
---|
[848439f] | 49 | |
---|
| 50 | // Count of current blocked threads |
---|
| 51 | size_t wait_count; |
---|
| 52 | |
---|
| 53 | // Flag if the lock allows multiple acquisition |
---|
| 54 | bool multi_acquisition; |
---|
| 55 | |
---|
| 56 | // Flag if lock can be released by non owner |
---|
| 57 | bool strict_owner; |
---|
| 58 | |
---|
| 59 | // Current thread owning the lock |
---|
| 60 | struct $thread * owner; |
---|
| 61 | |
---|
| 62 | // Number of recursion level |
---|
| 63 | size_t recursion_count; |
---|
| 64 | }; |
---|
| 65 | |
---|
[6a8882c] | 66 | struct single_acquisition_lock { |
---|
[848439f] | 67 | inline blocking_lock; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | struct owner_lock { |
---|
| 71 | inline blocking_lock; |
---|
| 72 | }; |
---|
| 73 | |
---|
[6a8882c] | 74 | struct multiple_acquisition_lock { |
---|
[848439f] | 75 | inline blocking_lock; |
---|
| 76 | }; |
---|
| 77 | |
---|
[ac5816d] | 78 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ); |
---|
[848439f] | 79 | void ^?{}( blocking_lock & this ); |
---|
| 80 | |
---|
[ac5816d] | 81 | void ?{}( single_acquisition_lock & this ); |
---|
[6a8882c] | 82 | void ^?{}( single_acquisition_lock & this ); |
---|
[848439f] | 83 | |
---|
[ac5816d] | 84 | void ?{}( owner_lock & this ); |
---|
[848439f] | 85 | void ^?{}( owner_lock & this ); |
---|
| 86 | |
---|
[ac5816d] | 87 | void ?{}( multiple_acquisition_lock & this ); |
---|
[6a8882c] | 88 | void ^?{}( multiple_acquisition_lock & this ); |
---|
[848439f] | 89 | |
---|
| 90 | void lock( blocking_lock & this ); |
---|
| 91 | bool try_lock( blocking_lock & this ); |
---|
| 92 | void unlock( blocking_lock & this ); |
---|
[797a193] | 93 | void on_notify( blocking_lock & this, struct $thread * t ); |
---|
| 94 | void on_wait( blocking_lock & this ); |
---|
[848439f] | 95 | size_t wait_count( blocking_lock & this ); |
---|
| 96 | void set_recursion_count( blocking_lock & this, size_t recursion ); |
---|
| 97 | size_t get_recursion_count( blocking_lock & this ); |
---|
| 98 | |
---|
[6a8882c] | 99 | void lock( single_acquisition_lock & this ); |
---|
| 100 | void unlock( single_acquisition_lock & this ); |
---|
[797a193] | 101 | void on_notify( single_acquisition_lock & this, struct $thread * t ); |
---|
| 102 | void on_wait( single_acquisition_lock & this ); |
---|
[6a8882c] | 103 | void set_recursion_count( single_acquisition_lock & this, size_t recursion ); |
---|
| 104 | size_t get_recursion_count( single_acquisition_lock & this ); |
---|
| 105 | |
---|
| 106 | void lock( owner_lock & this ); |
---|
| 107 | void unlock( owner_lock & this ); |
---|
[797a193] | 108 | void on_notify( owner_lock & this, struct $thread * t ); |
---|
| 109 | void on_wait( owner_lock & this ); |
---|
[6a8882c] | 110 | void set_recursion_count( owner_lock & this, size_t recursion ); |
---|
| 111 | size_t get_recursion_count( owner_lock & this ); |
---|
| 112 | |
---|
| 113 | void lock( multiple_acquisition_lock & this ); |
---|
| 114 | void unlock( multiple_acquisition_lock & this ); |
---|
[797a193] | 115 | void on_notify( multiple_acquisition_lock & this, struct $thread * t ); |
---|
| 116 | void on_wait( multiple_acquisition_lock & this ); |
---|
[6a8882c] | 117 | void set_recursion_count( multiple_acquisition_lock & this, size_t recursion ); |
---|
| 118 | size_t get_recursion_count( multiple_acquisition_lock & this ); |
---|
[848439f] | 119 | |
---|
[ac5816d] | 120 | //----------------------------------------------------------------------------- |
---|
| 121 | // Synchronization Locks |
---|
[848439f] | 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 |
---|
[cad1df1] | 128 | Sequence( info_thread(L) ) blocked_threads; |
---|
[848439f] | 129 | |
---|
| 130 | // Count of current blocked threads |
---|
| 131 | int count; |
---|
| 132 | }; |
---|
| 133 | |
---|
[ac5816d] | 134 | void ?{}( condition_variable(L) & this ); |
---|
[848439f] | 135 | void ^?{}( condition_variable(L) & this ); |
---|
| 136 | |
---|
[eeb5023] | 137 | bool notify_one( condition_variable(L) & this ); |
---|
| 138 | bool notify_all( condition_variable(L) & this ); |
---|
[848439f] | 139 | |
---|
[eeb5023] | 140 | uintptr_t front( condition_variable(L) & this ); |
---|
[848439f] | 141 | |
---|
[ac5816d] | 142 | bool empty ( condition_variable(L) & this ); |
---|
| 143 | int counter( condition_variable(L) & this ); |
---|
[848439f] | 144 | |
---|
[eeb5023] | 145 | void wait( condition_variable(L) & this ); |
---|
| 146 | void wait( condition_variable(L) & this, uintptr_t info ); |
---|
[dff1fd1] | 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 ); |
---|
[848439f] | 151 | |
---|
[eeb5023] | 152 | void wait( condition_variable(L) & this, L & l ); |
---|
| 153 | void wait( condition_variable(L) & this, L & l, uintptr_t info ); |
---|
[dff1fd1] | 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 ); |
---|
[eeb5023] | 158 | } |
---|