Debian Codegraph @

src/c/threads/mutex.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * mutex.d - mutually exclusive locks.
6 *
7 * Copyright (c) 2003, Juan Jose Garcia Ripoll
8 * Copyright (c) 2020, Marius Gerbershagen
9 *
10 * See file 'LICENSE' for the copyright details.
11 *
12 */
13
14 #ifndef __sun__ /* See unixinit.d for this */
15 #define _XOPEN_SOURCE 600 /* For pthread mutex attributes */
16 #endif
17 #include <errno.h>
18 #include <ecl/ecl.h>
19 #include <ecl/internal.h>
20
21 /*----------------------------------------------------------------------
22 * LOCKS or MUTEX
23 */
24
25
26 /* THREAD SAFETY
27 *
28 * mp:lock-owner, mp:holding-lock-p and mp:lock-count will return
29 * wrong values in the following scenarios:
30 * 1. Another thread is in the process of locking/unlocking the mutex.
31 * This in unavoidable since count and owner cannot both be stored
32 * atomically.
33 * 2. A call to mp:get-lock-wait is interrupted after the mutex has
34 * been locked but before count and owner have been set. In this
35 * case, the mutex will appear to be unlocked even though it is
36 * already locked. If the interrupting code performs a nonlocal
37 * jump up the call stack, this will persist even after the
38 * interrupt. However, the mutex can still be unlocked by
39 * mp:giveup-lock since the check whether the mutex is locked or
40 * not is done by OS level functions.
41 * 3. A call to mp:condition-variable-(timed)wait is interrupted after
42 * the mutex has been unlocked/relocked but after/before count and
43 * owner have been set. The consequences are equivalent to scenario 2.
44 * In summary, owner can be nil and count 0 even though the mutex is
45 * locked but the converse (owner != nil and count != 0 when the mutex
46 * is unlocked) cannot happen.
47 *
48 */
49
50 static void FEerror_not_a_recursive_lock(cl_object lock) ecl_attr_noreturn;
51
52 static void
53 FEerror_not_a_recursive_lock(cl_object lock)
54 {
55 FEerror("Attempted to recursively lock ~S which is already owned by ~S",
56 2, lock, lock->lock.owner);
57 }
58
59 cl_object
60 ecl_make_lock(cl_object name, bool recursive)
61 {
62 cl_env_ptr env = ecl_process_env();
63 cl_object output = ecl_alloc_object(t_lock);
64 output->lock.name = name;
65 output->lock.owner = ECL_NIL;
66 output->lock.counter = 0;
67 output->lock.recursive = recursive;
68 ecl_disable_interrupts_env(env);
69 ecl_mutex_init(&output->lock.mutex, recursive);
70 ecl_set_finalizer_unprotected(output, ECL_T);
71 ecl_enable_interrupts_env(env);
72 return output;
73 }
74
75 @(defun mp::make-lock (&key name (recursive ECL_NIL))
76 @
77 @(return ecl_make_lock(name, !Null(recursive)))
78 @)
79
80 cl_object
81 mp_recursive_lock_p(cl_object lock)
82 {
83 cl_env_ptr env = ecl_process_env();
84 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
85 FEwrong_type_only_arg(@[mp::recursive-lock-p], lock, @[mp::lock]);
86 }
87 ecl_return1(env, lock->lock.recursive? ECL_T : ECL_NIL);
88 }
89
90 cl_object
91 mp_lock_name(cl_object lock)
92 {
93 cl_env_ptr env = ecl_process_env();
94 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
95 FEwrong_type_only_arg(@[mp::lock-name], lock, @[mp::lock]);
96 }
97 ecl_return1(env, lock->lock.name);
98 }
99
100 cl_object
101 mp_lock_owner(cl_object lock)
102 {
103 cl_env_ptr env = ecl_process_env();
104 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
105 FEwrong_type_only_arg(@[mp::lock-owner], lock, @[mp::lock]);
106 }
107 ecl_return1(env, lock->lock.owner);
108 }
109
110 cl_object
111 mp_holding_lock_p(cl_object lock)
112 {
113 cl_env_ptr env = ecl_process_env();
114 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
115 FEwrong_type_only_arg(@[mp::holding-lock-p], lock, @[mp::lock]);
116 }
117 ecl_return1(env, (lock->lock.owner == mp_current_process())? ECL_T : ECL_NIL);
118 }
119
120 cl_object
121 mp_lock_count(cl_object lock)
122 {
123 cl_env_ptr env = ecl_process_env();
124 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
125 FEwrong_type_only_arg(@[mp::lock-count], lock, @[mp::lock]);
126 }
127 ecl_return1(env, ecl_make_fixnum(lock->lock.counter));
128 }
129
130 cl_object
131 mp_giveup_lock(cl_object lock)
132 {
133 cl_env_ptr env = ecl_process_env();
134 int rc;
135 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
136 FEwrong_type_only_arg(@[mp::giveup-lock], lock, @[mp::lock]);
137 }
138 ecl_disable_interrupts_env(env);
139 if ((lock->lock.counter > 0 ? --lock->lock.counter : 0) == 0) {
140 lock->lock.owner = ECL_NIL;
141 }
142 rc = ecl_mutex_unlock(&lock->lock.mutex);
143 ecl_enable_interrupts_env(env);
144 if (ecl_likely(rc == ECL_MUTEX_SUCCESS)) {
145 ecl_return1(env, ECL_T);
146 } else if (rc == ECL_MUTEX_NOT_OWNED) {
147 FEerror_not_owned(lock);
148 } else {
149 FEunknown_lock_error(lock);
150 }
151 }
152
153 cl_object
154 mp_get_lock_nowait(cl_object lock)
155 {
156 cl_env_ptr env = ecl_process_env();
157 cl_object own_process = env->own_process;
158 int rc;
159 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
160 FEwrong_type_nth_arg(@[mp::get-lock], 1, lock, @[mp::lock]);
161 }
162 #if !defined(ECL_MUTEX_DEADLOCK)
163 if (ecl_unlikely(lock->lock.owner == own_process && !lock->lock.recursive)) {
164 /* INV: owner != nil only if the mutex is locked */
165 FEerror_not_a_recursive_lock(lock);
166 }
167 #endif
168 ecl_disable_interrupts_env(env);
169 if ((rc = ecl_mutex_trylock(&lock->lock.mutex)) == ECL_MUTEX_SUCCESS) {
170 lock->lock.counter++;
171 lock->lock.owner = own_process;
172 }
173 ecl_enable_interrupts_env(env);
174 if (rc == ECL_MUTEX_SUCCESS) {
175 ecl_return1(env, ECL_T);
176 } else if (rc == ECL_MUTEX_LOCKED) {
177 ecl_return1(env, ECL_NIL);
178 #if defined(ECL_MUTEX_DEADLOCK)
179 } else if (ecl_unlikely(rc == ECL_MUTEX_DEADLOCK)) {
180 FEerror_not_a_recursive_lock(lock);
181 #endif
182 } else {
183 FEunknown_lock_error(lock);
184 }
185 }
186
187 cl_object
188 mp_get_lock_wait(cl_object lock)
189 {
190 cl_env_ptr env = ecl_process_env();
191 cl_object own_process = env->own_process;
192 int rc;
193 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
194 FEwrong_type_nth_arg(@[mp::get-lock], 1, lock, @[mp::lock]);
195 }
196 #if !defined(ECL_MUTEX_DEADLOCK)
197 if (ecl_unlikely(lock->lock.owner == own_process && !lock->lock.recursive)) {
198 /* INV: owner != nil only if the mutex is locked */
199 FEerror_not_a_recursive_lock(lock);
200 }
201 #endif
202 rc = ecl_mutex_lock(&lock->lock.mutex);
203 if (ecl_likely(rc == ECL_MUTEX_SUCCESS)) {
204 ecl_disable_interrupts_env(env);
205 lock->lock.counter++;
206 lock->lock.owner = own_process;
207 ecl_enable_interrupts_env(env);
208 ecl_return1(env, ECL_T);
209 #if defined(ECL_MUTEX_DEADLOCK)
210 } else if (ecl_unlikely(rc == ECL_MUTEX_DEADLOCK)) {
211 FEerror_not_a_recursive_lock(lock);
212 #endif
213 } else {
214 FEunknown_lock_error(lock);
215 }
216 }
217
218 static cl_object
219 si_abort_wait_on_mutex(cl_narg narg, ...)
220 {
221 const cl_env_ptr the_env = ecl_process_env();
222 cl_object env = the_env->function->cclosure.env;
223 cl_object lock = CAR(env);
224 if (ECL_SYM_VAL(the_env, @'si::mutex-timeout') == lock) {
225 ECL_SETQ(the_env, @'si::mutex-timeout', ECL_T);
226 cl_throw(@'si::mutex-timeout');
227 }
228 @(return)
229 }
230
231 cl_object
232 si_mutex_timeout(cl_object process, cl_object lock, cl_object timeout)
233 {
234 const cl_env_ptr the_env = ecl_process_env();
235 if (cl_plusp(timeout)) {
236 cl_sleep(timeout);
237 }
238 ECL_WITH_NATIVE_LOCK_BEGIN(the_env, &process->process.start_stop_lock) {
239 if (ecl_likely(mp_process_active_p(process) != ECL_NIL)) {
240 ecl_interrupt_process(process,
241 ecl_make_cclosure_va(si_abort_wait_on_mutex,
242 cl_list(1, lock),
243 @'si::mutex-timeout',
244 0));
245 }
246 } ECL_WITH_NATIVE_LOCK_END;
247 @(return)
248 }
249
250 cl_object
251 mp_get_lock_timedwait(cl_object lock, cl_object timeout)
252 {
253 cl_env_ptr env = ecl_process_env();
254 cl_object own_process = env->own_process;
255 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
256 FEwrong_type_nth_arg(@[mp::get-lock], 1, lock, @[mp::lock]);
257 }
258 #if !defined(ECL_MUTEX_DEADLOCK)
259 if (ecl_unlikely(lock->lock.owner == own_process && !lock->lock.recursive)) {
260 /* INV: owner != nil only if the mutex is locked */
261 FEerror_not_a_recursive_lock(lock);
262 }
263 #endif
264 #if defined(ECL_WINDOWS_THREADS) || defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK)
265 int rc = ecl_mutex_timedlock(&lock->lock.mutex, ecl_to_double(timeout));
266 #else
267 /* If we don't have pthread_mutex_timedlock available, we create a
268 * timer thread which interrupts our thread after the specified
269 * timeout. si::mutex-timeout serves a dual purpose below: the
270 * symbol itself denotes a catchpoint and its value is used to
271 * determine a) if the catchpoint is active and b) if the timer has
272 * fired. */
273 volatile int rc;
274 volatile cl_object timer_thread;
275 ecl_bds_bind(env, @'si::mutex-timeout', lock);
276 ECL_CATCH_BEGIN(env, @'si::mutex-timeout') {
277 timer_thread = mp_process_run_function(5, @'si::mutex-timeout',
278 @'si::mutex-timeout',
279 env->own_process,
280 lock,
281 timeout);
282 rc = ecl_mutex_lock(&lock->lock.mutex);
283 ECL_SETQ(env, @'si::mutex-timeout', ECL_NIL);
284 } ECL_CATCH_END;
285 ECL_WITH_NATIVE_LOCK_BEGIN(env, &timer_thread->process.start_stop_lock) {
286 if (mp_process_active_p(timer_thread)) {
287 ecl_interrupt_process(timer_thread, @'mp::exit-process');
288 }
289 } ECL_WITH_NATIVE_LOCK_END;
290 if (ECL_SYM_VAL(env, @'si::mutex-timeout') == ECL_T) {
291 rc = ECL_MUTEX_TIMEOUT;
292 /* The mutex might have been locked before we could kill the timer
293 * thread. Therefore, we unconditionally try to unlock the mutex
294 * again and treat the operation as having timed out. */
295 ecl_mutex_unlock(&lock->lock.mutex);
296 }
297 ecl_bds_unwind1(env);
298 #endif
299 if (rc == ECL_MUTEX_SUCCESS) {
300 ecl_disable_interrupts_env(env);
301 lock->lock.counter++;
302 lock->lock.owner = own_process;
303 ecl_enable_interrupts_env(env);
304 ecl_return1(env, ECL_T);
305 } else if (rc == ECL_MUTEX_TIMEOUT) {
306 ecl_return1(env, ECL_NIL);
307 #if defined(ECL_MUTEX_DEADLOCK)
308 } else if (ecl_unlikely(rc == ECL_MUTEX_DEADLOCK)) {
309 FEerror_not_a_recursive_lock(lock);
310 #endif
311 } else {
312 FEunknown_lock_error(lock);
313 }
314 }
315
316 @(defun mp::get-lock (lock &optional (wait ECL_T))
317 @
318 if (Null(wait)) {
319 return mp_get_lock_nowait(lock);
320 } else if (ecl_realp(wait)) {
321 return mp_get_lock_timedwait(lock, wait);
322 } else {
323 return mp_get_lock_wait(lock);
324 }
325 @)