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 listed; // true if info_thread is on queue, false otherwise; |
---|
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 | bool listed( info_thread(L) * this ); |
---|
49 | } |
---|
50 | |
---|
51 | /////////////////////////////////////////////////////////////////// |
---|
52 | //// Blocking Locks |
---|
53 | /////////////////////////////////////////////////////////////////// |
---|
54 | |
---|
55 | // struct lock_thread { |
---|
56 | // struct $thread * t; |
---|
57 | // lock_thread * next; |
---|
58 | // }; |
---|
59 | |
---|
60 | // void ?{}( lock_thread & this, struct $thread * thd ); |
---|
61 | // void ^?{}( lock_thread & this ); |
---|
62 | |
---|
63 | // lock_thread *& get_next( lock_thread & ); |
---|
64 | |
---|
65 | struct blocking_lock { |
---|
66 | // Spin lock used for mutual exclusion |
---|
67 | __spinlock_t lock; |
---|
68 | |
---|
69 | // List of blocked threads |
---|
70 | Sequence( $thread ) blocked_threads; |
---|
71 | |
---|
72 | // Count of current blocked threads |
---|
73 | size_t wait_count; |
---|
74 | |
---|
75 | // Flag if the lock allows multiple acquisition |
---|
76 | bool multi_acquisition; |
---|
77 | |
---|
78 | // Flag if lock can be released by non owner |
---|
79 | bool strict_owner; |
---|
80 | |
---|
81 | // Current thread owning the lock |
---|
82 | struct $thread * owner; |
---|
83 | |
---|
84 | // Number of recursion level |
---|
85 | size_t recursion_count; |
---|
86 | }; |
---|
87 | |
---|
88 | struct single_acquisition_lock { |
---|
89 | inline blocking_lock; |
---|
90 | }; |
---|
91 | |
---|
92 | struct owner_lock { |
---|
93 | inline blocking_lock; |
---|
94 | }; |
---|
95 | |
---|
96 | struct multiple_acquisition_lock { |
---|
97 | inline blocking_lock; |
---|
98 | }; |
---|
99 | |
---|
100 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ); |
---|
101 | void ^?{}( blocking_lock & this ); |
---|
102 | |
---|
103 | void ?{}( single_acquisition_lock & this ); |
---|
104 | void ^?{}( single_acquisition_lock & this ); |
---|
105 | |
---|
106 | void ?{}( owner_lock & this ); |
---|
107 | void ^?{}( owner_lock & this ); |
---|
108 | |
---|
109 | void ?{}( multiple_acquisition_lock & this ); |
---|
110 | void ^?{}( multiple_acquisition_lock & this ); |
---|
111 | |
---|
112 | void lock( blocking_lock & this ); |
---|
113 | bool try_lock( blocking_lock & this ); |
---|
114 | void unlock( blocking_lock & this ); |
---|
115 | void add_( blocking_lock & this, struct $thread * t ); |
---|
116 | void remove_( blocking_lock & this ); |
---|
117 | size_t wait_count( blocking_lock & this ); |
---|
118 | void set_recursion_count( blocking_lock & this, size_t recursion ); |
---|
119 | size_t get_recursion_count( blocking_lock & this ); |
---|
120 | |
---|
121 | void lock( single_acquisition_lock & this ); |
---|
122 | void unlock( single_acquisition_lock & this ); |
---|
123 | void add_( single_acquisition_lock & this, struct $thread * t ); |
---|
124 | void remove_( single_acquisition_lock & this ); |
---|
125 | void set_recursion_count( single_acquisition_lock & this, size_t recursion ); |
---|
126 | size_t get_recursion_count( single_acquisition_lock & this ); |
---|
127 | |
---|
128 | void lock( owner_lock & this ); |
---|
129 | void unlock( owner_lock & this ); |
---|
130 | void add_( owner_lock & this, struct $thread * t ); |
---|
131 | void remove_( owner_lock & this ); |
---|
132 | void set_recursion_count( owner_lock & this, size_t recursion ); |
---|
133 | size_t get_recursion_count( owner_lock & this ); |
---|
134 | |
---|
135 | void lock( multiple_acquisition_lock & this ); |
---|
136 | void unlock( multiple_acquisition_lock & this ); |
---|
137 | void add_( multiple_acquisition_lock & this, struct $thread * t ); |
---|
138 | void remove_( multiple_acquisition_lock & this ); |
---|
139 | void set_recursion_count( multiple_acquisition_lock & this, size_t recursion ); |
---|
140 | size_t get_recursion_count( multiple_acquisition_lock & this ); |
---|
141 | |
---|
142 | /////////////////////////////////////////////////////////////////// |
---|
143 | //// Synchronization Locks |
---|
144 | /////////////////////////////////////////////////////////////////// |
---|
145 | forall(dtype L | is_blocking_lock(L)) { |
---|
146 | struct condition_variable { |
---|
147 | // Spin lock used for mutual exclusion |
---|
148 | __spinlock_t lock; |
---|
149 | |
---|
150 | info_thread(L) * last_thread; |
---|
151 | |
---|
152 | // List of blocked threads |
---|
153 | Sequence( info_thread(L) ) blocked_threads; |
---|
154 | |
---|
155 | // Count of current blocked threads |
---|
156 | int count; |
---|
157 | }; |
---|
158 | |
---|
159 | void ?{}( condition_variable(L) & this ); |
---|
160 | void ^?{}( condition_variable(L) & this ); |
---|
161 | |
---|
162 | struct alarm_node_wrap { |
---|
163 | alarm_node_t alarm_node; |
---|
164 | |
---|
165 | condition_variable(L) * cond; |
---|
166 | |
---|
167 | info_thread(L) * i; |
---|
168 | }; |
---|
169 | |
---|
170 | void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback ); |
---|
171 | void ^?{}( alarm_node_wrap(L) & this ); |
---|
172 | |
---|
173 | void alarm_node_callback( alarm_node_wrap(L) & this ); |
---|
174 | |
---|
175 | void alarm_node_wrap_cast( alarm_node_t & a ); |
---|
176 | |
---|
177 | bool notify_one( condition_variable(L) & this ); |
---|
178 | bool notify_all( condition_variable(L) & this ); |
---|
179 | |
---|
180 | uintptr_t front( condition_variable(L) & this ); |
---|
181 | |
---|
182 | bool empty( condition_variable(L) & this ); |
---|
183 | int counter( condition_variable(L) & this ); |
---|
184 | |
---|
185 | // TODO: look into changing timout routines to return bool showing if signalled or woken by kernel |
---|
186 | void wait( condition_variable(L) & this ); |
---|
187 | void wait( condition_variable(L) & this, uintptr_t info ); |
---|
188 | void wait( condition_variable(L) & this, Duration duration ); |
---|
189 | void wait( condition_variable(L) & this, uintptr_t info, Duration duration ); |
---|
190 | void wait( condition_variable(L) & this, Time time ); |
---|
191 | void wait( condition_variable(L) & this, uintptr_t info, Time time ); |
---|
192 | |
---|
193 | void wait( condition_variable(L) & this, L & l ); |
---|
194 | void wait( condition_variable(L) & this, L & l, uintptr_t info ); |
---|
195 | void wait( condition_variable(L) & this, L & l, Duration duration ); |
---|
196 | void wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ); |
---|
197 | void wait( condition_variable(L) & this, L & l, Time time ); |
---|
198 | void wait( condition_variable(L) & this, L & l, uintptr_t info, Time time ); |
---|
199 | } |
---|