| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* semaphore.d - POSIX-like semaphores |
| 6 |
* |
| 7 |
* Copyright (c) 2011 Juan Jose Garcia Ripoll |
| 8 |
* Copyright (c) 2020 Marius Gerbershagen |
| 9 |
* |
| 10 |
* See file 'LICENSE' for the copyright details. |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <ecl/ecl.h> |
| 15 |
#include <ecl/internal.h> |
| 16 |
|
| 17 |
cl_object |
| 18 |
ecl_make_semaphore(cl_object name, cl_fixnum count) |
| 19 |
{ |
| 20 |
cl_env_ptr env = ecl_process_env(); |
| 21 |
cl_object output = ecl_alloc_object(t_semaphore); |
| 22 |
output->semaphore.name = name; |
| 23 |
output->semaphore.counter = count; |
| 24 |
output->semaphore.wait_count = 0; |
| 25 |
ecl_disable_interrupts_env(env); |
| 26 |
ecl_cond_var_init(&output->semaphore.cv); |
| 27 |
ecl_mutex_init(&output->semaphore.mutex, FALSE); |
| 28 |
ecl_set_finalizer_unprotected(output, ECL_T); |
| 29 |
ecl_enable_interrupts_env(env); |
| 30 |
return output; |
| 31 |
} |
| 32 |
|
| 33 |
@(defun mp::make-semaphore (&key name (count ecl_make_fixnum(0))) |
| 34 |
@ { |
| 35 |
@(return ecl_make_semaphore(name, fixnnint(count))); |
| 36 |
} @) |
| 37 |
|
| 38 |
cl_object |
| 39 |
mp_semaphore_name(cl_object semaphore) |
| 40 |
{ |
| 41 |
cl_env_ptr env = ecl_process_env(); |
| 42 |
unlikely_if (ecl_t_of(semaphore) != t_semaphore) { |
| 43 |
FEwrong_type_only_arg(@[mp::semaphore-name], semaphore, @[mp::semaphore]); |
| 44 |
} |
| 45 |
ecl_return1(env, semaphore->semaphore.name); |
| 46 |
} |
| 47 |
|
| 48 |
cl_object |
| 49 |
mp_semaphore_count(cl_object semaphore) |
| 50 |
{ |
| 51 |
cl_env_ptr env = ecl_process_env(); |
| 52 |
unlikely_if (ecl_t_of(semaphore) != t_semaphore) { |
| 53 |
FEwrong_type_only_arg(@[mp::semaphore-count], semaphore, @[mp::semaphore]); |
| 54 |
} |
| 55 |
ecl_return1(env, ecl_make_fixnum(semaphore->semaphore.counter)); |
| 56 |
} |
| 57 |
|
| 58 |
cl_object |
| 59 |
mp_semaphore_wait_count(cl_object semaphore) |
| 60 |
{ |
| 61 |
cl_env_ptr env = ecl_process_env(); |
| 62 |
unlikely_if (ecl_t_of(semaphore) != t_semaphore) { |
| 63 |
FEwrong_type_only_arg(@[mp::semaphore-wait-count], semaphore, @[mp::semaphore]); |
| 64 |
} |
| 65 |
ecl_return1(env, ecl_make_fixnum(semaphore->semaphore.wait_count)); |
| 66 |
} |
| 67 |
|
| 68 |
@(defun mp::signal-semaphore (semaphore &optional (count ecl_make_fixnum(1))) |
| 69 |
@ { |
| 70 |
cl_fixnum n = fixnnint(count); |
| 71 |
unlikely_if (ecl_t_of(semaphore) != t_semaphore) { |
| 72 |
FEwrong_type_nth_arg(@[mp::signal-semaphore], 1, semaphore, @[mp::semaphore]); |
| 73 |
} |
| 74 |
ecl_disable_interrupts_env(the_env); |
| 75 |
ecl_mutex_lock(&semaphore->semaphore.mutex); |
| 76 |
semaphore->semaphore.counter += n; |
| 77 |
for (; n > 0; n--) { |
| 78 |
ecl_cond_var_signal(&semaphore->semaphore.cv); |
| 79 |
} |
| 80 |
ecl_mutex_unlock(&semaphore->semaphore.mutex); |
| 81 |
ecl_enable_interrupts_env(the_env); |
| 82 |
@(return); |
| 83 |
} @) |
| 84 |
|
| 85 |
static inline void |
| 86 |
semaphore_wait_unprotected(cl_object semaphore, cl_object count, cl_object timeout) |
| 87 |
{ |
| 88 |
int rc; |
| 89 |
cl_env_ptr the_env = ecl_process_env(); |
| 90 |
cl_fixnum counter = fixnnint(count); |
| 91 |
ecl_mutex_t *mutex = &semaphore->semaphore.mutex; |
| 92 |
ecl_cond_var_t *cv = &semaphore->semaphore.cv; |
| 93 |
if (timeout == ECL_NIL) { |
| 94 |
do { |
| 95 |
ECL_SETQ(the_env, ECL_INTERRUPTS_ENABLED, ECL_T); |
| 96 |
ecl_cond_var_wait(cv, mutex); |
| 97 |
ECL_SETQ(the_env, ECL_INTERRUPTS_ENABLED, ECL_NIL); |
| 98 |
} while (semaphore->semaphore.counter < counter); |
| 99 |
} else { |
| 100 |
cl_object deadline = ecl_plus(cl_get_internal_real_time(), |
| 101 |
ecl_times(timeout, ecl_make_fixnum(1000000))); |
| 102 |
double seconds = ecl_to_double(timeout); |
| 103 |
do { |
| 104 |
ecl_setq(the_env, ECL_INTERRUPTS_ENABLED, ECL_T); |
| 105 |
rc = ecl_cond_var_timedwait(cv, mutex, seconds); |
| 106 |
ecl_setq(the_env, ECL_INTERRUPTS_ENABLED, ECL_NIL); |
| 107 |
timeout = ecl_minus(deadline, cl_get_internal_real_time()); |
| 108 |
seconds = ecl_to_double(timeout); |
| 109 |
} while(semaphore->semaphore.counter < counter |
| 110 |
&& rc != ECL_MUTEX_TIMEOUT |
| 111 |
&& seconds >= 0); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
cl_object |
| 116 |
mp_semaphore_wait(cl_object semaphore, cl_object count, cl_object timeout) |
| 117 |
{ |
| 118 |
cl_env_ptr the_env = ecl_process_env(); |
| 119 |
cl_fixnum counter = fixnnint(count); |
| 120 |
volatile cl_object output; |
| 121 |
unlikely_if (ecl_t_of(semaphore) != t_semaphore) { |
| 122 |
FEwrong_type_only_arg(@[mp::semaphore-wait], semaphore, @[mp::semaphore]); |
| 123 |
} |
| 124 |
ecl_bds_bind(the_env, ECL_INTERRUPTS_ENABLED, ECL_NIL); |
| 125 |
ecl_mutex_lock(&semaphore->semaphore.mutex); |
| 126 |
if (semaphore->semaphore.counter >= counter) { |
| 127 |
output = ecl_make_fixnum(semaphore->semaphore.counter); |
| 128 |
semaphore->semaphore.counter -= counter; |
| 129 |
ecl_mutex_unlock(&semaphore->semaphore.mutex); |
| 130 |
} else if (timeout == ECL_NIL || ecl_plusp(timeout)) { |
| 131 |
semaphore->semaphore.wait_count++; |
| 132 |
ECL_UNWIND_PROTECT_BEGIN(the_env) { |
| 133 |
semaphore_wait_unprotected(semaphore, count, timeout); |
| 134 |
if (semaphore->semaphore.counter >= counter) { |
| 135 |
output = ecl_make_fixnum(semaphore->semaphore.counter); |
| 136 |
semaphore->semaphore.counter -= counter; |
| 137 |
} else { |
| 138 |
output = ECL_NIL; |
| 139 |
} |
| 140 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 141 |
semaphore->semaphore.wait_count--; |
| 142 |
ecl_mutex_unlock(&semaphore->semaphore.mutex); |
| 143 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 144 |
} else { |
| 145 |
output = ECL_NIL; |
| 146 |
ecl_mutex_unlock(&semaphore->semaphore.mutex); |
| 147 |
} |
| 148 |
ecl_bds_unwind1(the_env); |
| 149 |
ecl_check_pending_interrupts(the_env); |
| 150 |
ecl_return1(the_env, output); |
| 151 |
} |
| 152 |
|
| 153 |
@(defun mp::wait-on-semaphore (semaphore &key (count ecl_make_fixnum(1)) |
| 154 |
(timeout ECL_NIL)) |
| 155 |
@ { |
| 156 |
cl_object output = mp_semaphore_wait(semaphore, count, timeout); |
| 157 |
@(return output); |
| 158 |
} @) |
| 159 |
|
| 160 |
@(defun mp::try-get-semaphore (semaphore &optional (count ecl_make_fixnum(1))) |
| 161 |
@ { |
| 162 |
cl_object timeout = ecl_make_fixnum(0); |
| 163 |
cl_object output = mp_semaphore_wait(semaphore, count, timeout); |
| 164 |
@(return output); |
| 165 |
} @) |