1 | #pragma once |
---|
2 | |
---|
3 | #include <stdbool.h> |
---|
4 | |
---|
5 | #include "bits/algorithm.hfa" |
---|
6 | #include "bits/locks.hfa" |
---|
7 | #include "bits/sequence.hfa" |
---|
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> |
---|
15 | #include "alarm.hfa" |
---|
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 { |
---|
34 | inline Seqable; |
---|
35 | struct $thread * t; |
---|
36 | uintptr_t info; |
---|
37 | L * lock; |
---|
38 | bool signalled; // true when signalled and false when timeout wakes thread |
---|
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 | info_thread(L) *& Back( info_thread(L) * this ); |
---|
47 | info_thread(L) *& Next( info_thread(L) * this ); |
---|
48 | } |
---|
49 | |
---|
50 | /////////////////////////////////////////////////////////////////// |
---|
51 | //// Blocking Locks |
---|
52 | /////////////////////////////////////////////////////////////////// |
---|
53 | |
---|
54 | struct blocking_lock { |
---|
55 | // Spin lock used for mutual exclusion |
---|
56 | __spinlock_t lock; |
---|
57 | |
---|
58 | // List of blocked threads |
---|
59 | Sequence( $thread ) blocked_threads; |
---|
60 | |
---|
61 | // Count of current blocked threads |
---|
62 | size_t wait_count; |
---|
63 | |
---|
64 | // Flag if the lock allows multiple acquisition |
---|
65 | bool multi_acquisition; |
---|
66 | |
---|
67 | // Flag if lock can be released by non owner |
---|
68 | bool strict_owner; |
---|
69 | |
---|
70 | // Current thread owning the lock |
---|
71 | struct $thread * owner; |
---|
72 | |
---|
73 | // Number of recursion level |
---|
74 | size_t recursion_count; |
---|
75 | }; |
---|
76 | |
---|
77 | struct single_acquisition_lock { |
---|
78 | inline blocking_lock; |
---|
79 | }; |
---|
80 | |
---|
81 | struct owner_lock { |
---|
82 | inline blocking_lock; |
---|
83 | }; |
---|
84 | |
---|
85 | struct multiple_acquisition_lock { |
---|
86 | inline blocking_lock; |
---|
87 | }; |
---|
88 | |
---|
89 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ); |
---|
90 | void ^?{}( blocking_lock & this ); |
---|
91 | |
---|
92 | void ?{}( single_acquisition_lock & this ); |
---|
93 | void ^?{}( single_acquisition_lock & this ); |
---|
94 | |
---|
95 | void ?{}( owner_lock & this ); |
---|
96 | void ^?{}( owner_lock & this ); |
---|
97 | |
---|
98 | void ?{}( multiple_acquisition_lock & this ); |
---|
99 | void ^?{}( multiple_acquisition_lock & this ); |
---|
100 | |
---|
101 | void lock( blocking_lock & this ); |
---|
102 | bool try_lock( blocking_lock & this ); |
---|
103 | void unlock( blocking_lock & this ); |
---|
104 | void add_( blocking_lock & this, struct $thread * t ); |
---|
105 | void remove_( blocking_lock & this ); |
---|
106 | size_t wait_count( blocking_lock & this ); |
---|
107 | void set_recursion_count( blocking_lock & this, size_t recursion ); |
---|
108 | size_t get_recursion_count( blocking_lock & this ); |
---|
109 | |
---|
110 | void lock( single_acquisition_lock & this ); |
---|
111 | void unlock( single_acquisition_lock & this ); |
---|
112 | void add_( single_acquisition_lock & this, struct $thread * t ); |
---|
113 | void remove_( single_acquisition_lock & this ); |
---|
114 | void set_recursion_count( single_acquisition_lock & this, size_t recursion ); |
---|
115 | size_t get_recursion_count( single_acquisition_lock & this ); |
---|
116 | |
---|
117 | void lock( owner_lock & this ); |
---|
118 | void unlock( owner_lock & this ); |
---|
119 | void add_( owner_lock & this, struct $thread * t ); |
---|
120 | void remove_( owner_lock & this ); |
---|
121 | void set_recursion_count( owner_lock & this, size_t recursion ); |
---|
122 | size_t get_recursion_count( owner_lock & this ); |
---|
123 | |
---|
124 | void lock( multiple_acquisition_lock & this ); |
---|
125 | void unlock( multiple_acquisition_lock & this ); |
---|
126 | void add_( multiple_acquisition_lock & this, struct $thread * t ); |
---|
127 | void remove_( multiple_acquisition_lock & this ); |
---|
128 | void set_recursion_count( multiple_acquisition_lock & this, size_t recursion ); |
---|
129 | size_t get_recursion_count( multiple_acquisition_lock & this ); |
---|
130 | |
---|
131 | /////////////////////////////////////////////////////////////////// |
---|
132 | //// Synchronization Locks |
---|
133 | /////////////////////////////////////////////////////////////////// |
---|
134 | forall(dtype L | is_blocking_lock(L)) { |
---|
135 | struct condition_variable { |
---|
136 | // Spin lock used for mutual exclusion |
---|
137 | __spinlock_t lock; |
---|
138 | |
---|
139 | info_thread(L) * last_thread; |
---|
140 | |
---|
141 | // List of blocked threads |
---|
142 | Sequence( info_thread(L) ) blocked_threads; |
---|
143 | |
---|
144 | // Count of current blocked threads |
---|
145 | int count; |
---|
146 | }; |
---|
147 | |
---|
148 | void ?{}( condition_variable(L) & this ); |
---|
149 | void ^?{}( condition_variable(L) & this ); |
---|
150 | |
---|
151 | struct alarm_node_wrap { |
---|
152 | alarm_node_t alarm_node; |
---|
153 | |
---|
154 | condition_variable(L) * cond; |
---|
155 | |
---|
156 | info_thread(L) * i; |
---|
157 | }; |
---|
158 | |
---|
159 | void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback ); |
---|
160 | void ^?{}( alarm_node_wrap(L) & this ); |
---|
161 | |
---|
162 | void alarm_node_callback( alarm_node_wrap(L) & this ); |
---|
163 | |
---|
164 | void alarm_node_wrap_cast( alarm_node_t & a ); |
---|
165 | |
---|
166 | bool notify_one( condition_variable(L) & this ); |
---|
167 | bool notify_all( condition_variable(L) & this ); |
---|
168 | |
---|
169 | uintptr_t front( condition_variable(L) & this ); |
---|
170 | |
---|
171 | bool empty( condition_variable(L) & this ); |
---|
172 | int counter( condition_variable(L) & this ); |
---|
173 | |
---|
174 | void wait( condition_variable(L) & this ); |
---|
175 | void wait( condition_variable(L) & this, uintptr_t info ); |
---|
176 | bool wait( condition_variable(L) & this, Duration duration ); |
---|
177 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ); |
---|
178 | bool wait( condition_variable(L) & this, Time time ); |
---|
179 | bool wait( condition_variable(L) & this, uintptr_t info, Time time ); |
---|
180 | |
---|
181 | void wait( condition_variable(L) & this, L & l ); |
---|
182 | void wait( condition_variable(L) & this, L & l, uintptr_t info ); |
---|
183 | bool wait( condition_variable(L) & this, L & l, Duration duration ); |
---|
184 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ); |
---|
185 | bool wait( condition_variable(L) & this, L & l, Time time ); |
---|
186 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time ); |
---|
187 | } |
---|