Debian Codegraph @

src/c/threads/condition_variable.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 * condition_variable.d - condition variables
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 #define ECL_INCLUDE_MATH_H
18 #include <ecl/ecl.h>
19 #ifdef ECL_WINDOWS_THREADS
20 # include <windows.h>
21 #else
22 # include <pthread.h>
23 #endif
24 #include <ecl/internal.h>
25 #include <ecl/ecl-inl.h>
26
27 /*----------------------------------------------------------------------
28 * CONDITION VARIABLES
29 */
30
31 cl_object
32 mp_make_condition_variable(void)
33 {
34 cl_env_ptr env = ecl_process_env();
35 cl_object output = ecl_alloc_object(t_condition_variable);
36 ecl_disable_interrupts_env(env);
37 ecl_cond_var_init(&output->condition_variable.cv);
38 ecl_set_finalizer_unprotected(output, ECL_T);
39 ecl_enable_interrupts_env(env);
40 @(return output)
41 }
42
43 cl_object
44 mp_condition_variable_wait(cl_object cv, cl_object lock)
45 {
46 cl_env_ptr env = ecl_process_env();
47 int rc;
48 cl_fixnum counter;
49 cl_object owner;
50 if (ecl_unlikely(ecl_t_of(cv) != t_condition_variable)) {
51 FEwrong_type_nth_arg(@[mp::condition-variable-wait], 1, cv,
52 @[mp::condition-variable]);
53 }
54 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
55 FEwrong_type_nth_arg(@[mp::condition-variable-wait], 2, lock,
56 @[mp::lock]);
57 }
58 if (ecl_unlikely(lock->lock.recursive)) {
59 FEerror("mp:condition-variable-wait can not be used with recursive"
60 " locks:~%~S", 1, lock);
61 }
62 if (ecl_unlikely(lock->lock.owner != env->own_process)) {
63 FEerror("Attempt to wait on a condition variable using lock~%~S"
64 "~%which is not owned by process~%~S", 2, lock, env->own_process);
65 }
66 ecl_disable_interrupts_env(env);
67 counter = lock->lock.counter;
68 owner = lock->lock.owner;
69 lock->lock.counter = 0;
70 lock->lock.owner = ECL_NIL;
71 ecl_enable_interrupts_env(env);
72 rc = ecl_cond_var_wait(&cv->condition_variable.cv, &lock->lock.mutex);
73 ecl_disable_interrupts_env(env);
74 lock->lock.owner = owner;
75 lock->lock.counter = counter;
76 ecl_enable_interrupts_env(env);
77 if (ecl_unlikely(rc != ECL_MUTEX_SUCCESS)) {
78 if (rc == ECL_MUTEX_NOT_OWNED) {
79 FEerror_not_owned(lock);
80 } else {
81 FEunknown_lock_error(lock);
82 }
83 }
84 @(return ECL_T)
85 }
86
87 cl_object
88 mp_condition_variable_timedwait(cl_object cv, cl_object lock, cl_object seconds)
89 {
90 cl_env_ptr env = ecl_process_env();
91 int rc;
92 cl_fixnum counter;
93 cl_object owner;
94
95 if (ecl_unlikely(ecl_t_of(cv) != t_condition_variable)) {
96 FEwrong_type_nth_arg(@[mp::condition-variable-timedwait],
97 1, cv, @[mp::condition-variable]);
98 }
99 if (ecl_unlikely(ecl_t_of(lock) != t_lock)) {
100 FEwrong_type_nth_arg(@[mp::condition-variable-timedwait],
101 2, lock, @[mp::lock]);
102 }
103 if (ecl_unlikely(lock->lock.recursive)) {
104 FEerror("mp:condition-variable-timedwait can not be used with recursive"
105 " locks:~%~S", 1, lock);
106 }
107 if (ecl_unlikely(lock->lock.owner != env->own_process)) {
108 FEerror("Attempt to wait on a condition variable using lock~%~S"
109 "~%which is not owned by process~%~S", 2, lock, env->own_process);
110 }
111 /* INV: ecl_minusp() makes sure `seconds' is real */
112 if (ecl_unlikely(ecl_minusp(seconds))) {
113 cl_error(9, @'simple-type-error', @':format-control',
114 make_constant_base_string("Not a non-negative number ~S"),
115 @':format-arguments', cl_list(1, seconds),
116 @':expected-type', @'real', @':datum', seconds);
117 }
118 ecl_disable_interrupts_env(env);
119 counter = lock->lock.counter;
120 owner = lock->lock.owner;
121 lock->lock.counter = 0;
122 lock->lock.owner = ECL_NIL;
123 ecl_enable_interrupts_env(env);
124
125 rc = ecl_cond_var_timedwait(&cv->condition_variable.cv, &lock->lock.mutex, ecl_to_double(seconds));
126
127 ecl_disable_interrupts_env(env);
128 lock->lock.owner = owner;
129 lock->lock.counter = counter;
130 ecl_enable_interrupts_env(env);
131
132 if (ecl_unlikely(rc != ECL_MUTEX_SUCCESS && rc != ECL_MUTEX_TIMEOUT)) {
133 if (rc == ECL_MUTEX_NOT_OWNED) {
134 FEerror_not_owned(lock);
135 } else {
136 FEunknown_lock_error(lock);
137 }
138 }
139 @(return (rc == ECL_MUTEX_SUCCESS ? ECL_T : ECL_NIL))
140 }
141
142 cl_object
143 mp_condition_variable_signal(cl_object cv)
144 {
145 if (ecl_unlikely(ecl_t_of(cv) != t_condition_variable)) {
146 FEwrong_type_only_arg(@[mp::condition-variable-signal],
147 cv, @[mp::condition-variable]);
148 }
149 ecl_cond_var_signal(&cv->condition_variable.cv);
150 @(return ECL_T)
151 }
152
153 cl_object
154 mp_condition_variable_broadcast(cl_object cv)
155 {
156 if (ecl_unlikely(ecl_t_of(cv) != t_condition_variable)) {
157 FEwrong_type_only_arg(@[mp::condition-variable-broadcast],
158 cv, @[mp::condition-variable]);
159 }
160 ecl_cond_var_broadcast(&cv->condition_variable.cv);
161 @(return ECL_T)
162 }
163