| 1 |
/* |
| 2 |
* mutex.c - Scheme-level synchronization devices |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#define LIBGAUCHE_BODY |
| 35 |
#include <math.h> |
| 36 |
#include "gauche.h" |
| 37 |
#include "gauche/priv/configP.h" |
| 38 |
|
| 39 |
/*===================================================== |
| 40 |
* Mutex |
| 41 |
*/ |
| 42 |
|
| 43 |
static ScmObj mutex_allocate(ScmClass *klass, ScmObj initargs); |
| 44 |
static void mutex_print(ScmObj mutex, ScmPort *port, ScmWriteContext *ctx); |
| 45 |
|
| 46 |
static ScmClass *default_cpl[] = { |
| 47 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), NULL |
| 48 |
}; |
| 49 |
|
| 50 |
SCM_DEFINE_BASE_CLASS(Scm_MutexClass, ScmMutex, |
| 51 |
mutex_print, NULL, NULL, mutex_allocate, |
| 52 |
default_cpl); |
| 53 |
|
| 54 |
static void mutex_finalize(ScmObj obj, void* data SCM_UNUSED) |
| 55 |
{ |
| 56 |
ScmMutex *mutex = SCM_MUTEX(obj); |
| 57 |
SCM_INTERNAL_MUTEX_DESTROY(mutex->mutex); |
| 58 |
SCM_INTERNAL_COND_DESTROY(mutex->cv); |
| 59 |
} |
| 60 |
|
| 61 |
static ScmObj mutex_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED) |
| 62 |
{ |
| 63 |
ScmMutex *mutex = SCM_NEW_INSTANCE(ScmMutex, klass); |
| 64 |
SCM_INTERNAL_MUTEX_INIT(mutex->mutex); |
| 65 |
SCM_INTERNAL_COND_INIT(mutex->cv); |
| 66 |
Scm_RegisterFinalizer(SCM_OBJ(mutex), mutex_finalize, NULL); |
| 67 |
mutex->name = SCM_FALSE; |
| 68 |
mutex->specific = SCM_UNDEFINED; |
| 69 |
mutex->locked = FALSE; |
| 70 |
mutex->owner = NULL; |
| 71 |
mutex->locker_proc = mutex->unlocker_proc = SCM_FALSE; |
| 72 |
return SCM_OBJ(mutex); |
| 73 |
} |
| 74 |
|
| 75 |
static void mutex_print(ScmObj obj, ScmPort *port, ScmWriteContext *ctx SCM_UNUSED) |
| 76 |
{ |
| 77 |
ScmMutex *mutex = SCM_MUTEX(obj); |
| 78 |
|
| 79 |
(void)SCM_INTERNAL_MUTEX_LOCK(mutex->mutex); |
| 80 |
int locked = mutex->locked; |
| 81 |
ScmVM *vm = mutex->owner; |
| 82 |
ScmObj name = mutex->name; |
| 83 |
(void)SCM_INTERNAL_MUTEX_UNLOCK(mutex->mutex); |
| 84 |
|
| 85 |
if (SCM_FALSEP(name)) Scm_Printf(port, "#<mutex %p ", mutex); |
| 86 |
else Scm_Printf(port, "#<mutex %S ", name); |
| 87 |
if (locked) { |
| 88 |
if (vm) { |
| 89 |
if (vm->state == SCM_VM_TERMINATED) { |
| 90 |
Scm_Printf(port, "unlocked/abandoned>"); |
| 91 |
} else { |
| 92 |
Scm_Printf(port, "locked/owned by %S>", vm); |
| 93 |
} |
| 94 |
} else { |
| 95 |
Scm_Printf(port, "locked/not-owned>"); |
| 96 |
} |
| 97 |
} else { |
| 98 |
Scm_Printf(port, "unlocked/not-abandoned>"); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
* Accessors |
| 104 |
*/ |
| 105 |
static ScmObj sym_not_owned; |
| 106 |
static ScmObj sym_abandoned; |
| 107 |
static ScmObj sym_not_abandoned; |
| 108 |
|
| 109 |
static ScmObj mutex_state_get(ScmMutex *mutex) |
| 110 |
{ |
| 111 |
ScmObj r; |
| 112 |
(void)SCM_INTERNAL_MUTEX_LOCK(mutex->mutex); |
| 113 |
if (mutex->locked) { |
| 114 |
if (mutex->owner) { |
| 115 |
if (mutex->owner->state == SCM_VM_TERMINATED) r = sym_abandoned; |
| 116 |
else r = SCM_OBJ(mutex->owner); |
| 117 |
} else { |
| 118 |
r = sym_not_owned; |
| 119 |
} |
| 120 |
} else { |
| 121 |
r = sym_not_abandoned; |
| 122 |
} |
| 123 |
(void)SCM_INTERNAL_MUTEX_UNLOCK(mutex->mutex); |
| 124 |
return r; |
| 125 |
} |
| 126 |
|
| 127 |
static ScmObj mutex_name_get(ScmMutex *mutex) |
| 128 |
{ |
| 129 |
return mutex->name; |
| 130 |
} |
| 131 |
|
| 132 |
static void mutex_name_set(ScmMutex *mutex, ScmObj name) |
| 133 |
{ |
| 134 |
mutex->name = name; |
| 135 |
} |
| 136 |
|
| 137 |
static ScmObj mutex_specific_get(ScmMutex *mutex) |
| 138 |
{ |
| 139 |
return mutex->specific; |
| 140 |
} |
| 141 |
|
| 142 |
static void mutex_specific_set(ScmMutex *mutex, ScmObj value) |
| 143 |
{ |
| 144 |
mutex->specific = value; |
| 145 |
} |
| 146 |
|
| 147 |
static ScmClassStaticSlotSpec mutex_slots[] = { |
| 148 |
SCM_CLASS_SLOT_SPEC("name", mutex_name_get, mutex_name_set), |
| 149 |
SCM_CLASS_SLOT_SPEC("state", mutex_state_get, NULL), |
| 150 |
SCM_CLASS_SLOT_SPEC("specific", mutex_specific_get, mutex_specific_set), |
| 151 |
SCM_CLASS_SLOT_SPEC_END() |
| 152 |
}; |
| 153 |
|
| 154 |
/* |
| 155 |
* Make mutex |
| 156 |
*/ |
| 157 |
ScmObj Scm_MakeMutex(ScmObj name) |
| 158 |
{ |
| 159 |
ScmObj m = mutex_allocate(SCM_CLASS_MUTEX, SCM_NIL); |
| 160 |
SCM_MUTEX(m)->name = name; |
| 161 |
return m; |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* Lock and unlock mutex |
| 166 |
*/ |
| 167 |
|
| 168 |
ScmObj Scm_MutexLock(ScmMutex *mutex, ScmObj timeout, ScmVM *owner) |
| 169 |
{ |
| 170 |
ScmTimeSpec ts; |
| 171 |
ScmObj r = SCM_TRUE; |
| 172 |
ScmVM * volatile abandoned = NULL; |
| 173 |
volatile int intr = FALSE; |
| 174 |
|
| 175 |
ScmTimeSpec *pts = Scm_GetTimeSpec(timeout, &ts); |
| 176 |
SCM_INTERNAL_MUTEX_SAFE_LOCK_BEGIN(mutex->mutex); |
| 177 |
while (mutex->locked) { |
| 178 |
if (mutex->owner && mutex->owner->state == SCM_VM_TERMINATED) { |
| 179 |
abandoned = mutex->owner; |
| 180 |
mutex->locked = FALSE; |
| 181 |
break; |
| 182 |
} |
| 183 |
if (pts) { |
| 184 |
int tr = SCM_INTERNAL_COND_TIMEDWAIT(mutex->cv, mutex->mutex, pts); |
| 185 |
if (tr == SCM_INTERNAL_COND_TIMEDOUT) { r = SCM_FALSE; break; } |
| 186 |
else if (tr == SCM_INTERNAL_COND_INTR) { intr = TRUE; break; } |
| 187 |
} else { |
| 188 |
SCM_INTERNAL_COND_WAIT(mutex->cv, mutex->mutex); |
| 189 |
} |
| 190 |
} |
| 191 |
if (SCM_TRUEP(r)) { |
| 192 |
mutex->locked = TRUE; |
| 193 |
mutex->owner = owner; |
| 194 |
} |
| 195 |
SCM_INTERNAL_MUTEX_SAFE_LOCK_END(); |
| 196 |
if (intr) Scm_SigCheck(Scm_VM()); |
| 197 |
if (abandoned) { |
| 198 |
ScmObj exc |
| 199 |
= Scm_MakeThreadException(SCM_CLASS_ABANDONED_MUTEX_EXCEPTION, |
| 200 |
(ScmVM*)abandoned); |
| 201 |
SCM_THREAD_EXCEPTION(exc)->data = SCM_OBJ(mutex); |
| 202 |
r = Scm_Raise(exc, 0); |
| 203 |
} |
| 204 |
return r; |
| 205 |
} |
| 206 |
|
| 207 |
ScmObj Scm_MutexUnlock(ScmMutex *mutex, ScmConditionVariable *cv, ScmObj timeout) |
| 208 |
{ |
| 209 |
volatile ScmObj r = SCM_TRUE; |
| 210 |
ScmTimeSpec ts; |
| 211 |
volatile int intr = FALSE; |
| 212 |
|
| 213 |
ScmTimeSpec *pts = Scm_GetTimeSpec(timeout, &ts); |
| 214 |
SCM_INTERNAL_MUTEX_SAFE_LOCK_BEGIN(mutex->mutex); |
| 215 |
mutex->locked = FALSE; |
| 216 |
mutex->owner = NULL; |
| 217 |
SCM_INTERNAL_COND_SIGNAL(mutex->cv); |
| 218 |
if (cv) { |
| 219 |
if (pts) { |
| 220 |
int tr = SCM_INTERNAL_COND_TIMEDWAIT(cv->cv, mutex->mutex, pts); |
| 221 |
if (tr == SCM_INTERNAL_COND_TIMEDOUT) { r = SCM_FALSE; } |
| 222 |
else if (tr == SCM_INTERNAL_COND_INTR) { intr = TRUE; } |
| 223 |
} else { |
| 224 |
SCM_INTERNAL_COND_WAIT(cv->cv, mutex->mutex); |
| 225 |
} |
| 226 |
} |
| 227 |
SCM_INTERNAL_MUTEX_SAFE_LOCK_END(); |
| 228 |
if (intr) Scm_SigCheck(Scm_VM()); |
| 229 |
return r; |
| 230 |
} |
| 231 |
|
| 232 |
static ScmObj mutex_locker(ScmObj *args SCM_UNUSED, |
| 233 |
int argc SCM_UNUSED, |
| 234 |
void *mutex) |
| 235 |
{ |
| 236 |
return Scm_MutexLock((ScmMutex*)mutex, SCM_FALSE, Scm_VM()); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
ScmObj Scm_MutexLocker(ScmMutex *mutex) |
| 241 |
{ |
| 242 |
ScmObj p = mutex->locker_proc; |
| 243 |
if (SCM_FALSEP(p)) { |
| 244 |
/* safe; race is ok here */ |
| 245 |
p = Scm_MakeSubr(mutex_locker, (void*)mutex, 0, 0, SCM_FALSE); |
| 246 |
mutex->locker_proc = p; |
| 247 |
} |
| 248 |
return p; |
| 249 |
} |
| 250 |
|
| 251 |
static ScmObj mutex_unlocker(ScmObj *args SCM_UNUSED, |
| 252 |
int argc SCM_UNUSED, |
| 253 |
void *mutex) |
| 254 |
{ |
| 255 |
return Scm_MutexUnlock((ScmMutex*)mutex, NULL, SCM_FALSE); |
| 256 |
} |
| 257 |
|
| 258 |
ScmObj Scm_MutexUnlocker(ScmMutex *mutex) |
| 259 |
{ |
| 260 |
ScmObj p = mutex->unlocker_proc; |
| 261 |
if (SCM_FALSEP(p)) { |
| 262 |
/* safe; race is ok here */ |
| 263 |
p = Scm_MakeSubr(mutex_unlocker, (void*)mutex, 0, 0, SCM_FALSE); |
| 264 |
mutex->unlocker_proc = p; |
| 265 |
} |
| 266 |
return p; |
| 267 |
} |
| 268 |
|
| 269 |
/*===================================================== |
| 270 |
* Condition variable |
| 271 |
*/ |
| 272 |
|
| 273 |
static ScmObj cv_allocate(ScmClass *klass, ScmObj initargs); |
| 274 |
static void cv_print(ScmObj cv, ScmPort *port, ScmWriteContext *ctx); |
| 275 |
|
| 276 |
SCM_DEFINE_BASE_CLASS(Scm_ConditionVariableClass, ScmConditionVariable, |
| 277 |
cv_print, NULL, NULL, cv_allocate, |
| 278 |
default_cpl); |
| 279 |
|
| 280 |
static void cv_finalize(ScmObj obj, void *data SCM_UNUSED) |
| 281 |
{ |
| 282 |
ScmConditionVariable *cv = SCM_CONDITION_VARIABLE(obj); |
| 283 |
SCM_INTERNAL_COND_DESTROY(cv->cv); |
| 284 |
} |
| 285 |
|
| 286 |
static ScmObj cv_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED) |
| 287 |
{ |
| 288 |
ScmConditionVariable *cv = SCM_NEW_INSTANCE(ScmConditionVariable, klass); |
| 289 |
SCM_INTERNAL_COND_INIT(cv->cv); |
| 290 |
Scm_RegisterFinalizer(SCM_OBJ(cv), cv_finalize, NULL); |
| 291 |
cv->name = SCM_FALSE; |
| 292 |
cv->specific = SCM_UNDEFINED; |
| 293 |
return SCM_OBJ(cv); |
| 294 |
} |
| 295 |
|
| 296 |
static void cv_print(ScmObj obj, ScmPort *port, ScmWriteContext *ctx SCM_UNUSED) |
| 297 |
{ |
| 298 |
ScmConditionVariable *cv = SCM_CONDITION_VARIABLE(obj); |
| 299 |
ScmObj name = cv->name; |
| 300 |
if (SCM_FALSEP(name)) Scm_Printf(port, "#<condition-variable %p>", cv); |
| 301 |
else Scm_Printf(port, "#<condition-variable %S>", name); |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
* Accessors |
| 306 |
*/ |
| 307 |
|
| 308 |
static ScmObj cv_name_get(ScmConditionVariable *cv) |
| 309 |
{ |
| 310 |
return cv->name; |
| 311 |
} |
| 312 |
|
| 313 |
static void cv_name_set(ScmConditionVariable *cv, ScmObj name) |
| 314 |
{ |
| 315 |
cv->name = name; |
| 316 |
} |
| 317 |
|
| 318 |
static ScmObj cv_specific_get(ScmConditionVariable *cv) |
| 319 |
{ |
| 320 |
return cv->specific; |
| 321 |
} |
| 322 |
|
| 323 |
static void cv_specific_set(ScmConditionVariable *cv, ScmObj val) |
| 324 |
{ |
| 325 |
cv->specific = val; |
| 326 |
} |
| 327 |
|
| 328 |
static ScmClassStaticSlotSpec cv_slots[] = { |
| 329 |
SCM_CLASS_SLOT_SPEC("name", cv_name_get, cv_name_set), |
| 330 |
SCM_CLASS_SLOT_SPEC("specific", cv_specific_get, cv_specific_set), |
| 331 |
SCM_CLASS_SLOT_SPEC_END() |
| 332 |
}; |
| 333 |
|
| 334 |
/* |
| 335 |
* Make condition variable |
| 336 |
*/ |
| 337 |
ScmObj Scm_MakeConditionVariable(ScmObj name) |
| 338 |
{ |
| 339 |
ScmObj cv = cv_allocate(SCM_CLASS_CONDITION_VARIABLE, SCM_NIL); |
| 340 |
SCM_CONDITION_VARIABLE(cv)->name = name; |
| 341 |
return cv; |
| 342 |
} |
| 343 |
|
| 344 |
ScmObj Scm_ConditionVariableSignal(ScmConditionVariable *cond) |
| 345 |
{ |
| 346 |
SCM_INTERNAL_COND_SIGNAL(cond->cv); |
| 347 |
return SCM_UNDEFINED; |
| 348 |
} |
| 349 |
|
| 350 |
ScmObj Scm_ConditionVariableBroadcast(ScmConditionVariable *cond) |
| 351 |
{ |
| 352 |
SCM_INTERNAL_COND_BROADCAST(cond->cv); |
| 353 |
return SCM_UNDEFINED; |
| 354 |
} |
| 355 |
|
| 356 |
/* |
| 357 |
* Initialization |
| 358 |
*/ |
| 359 |
|
| 360 |
void Scm__InitMutex() |
| 361 |
{ |
| 362 |
ScmModule *mod = SCM_FIND_MODULE("gauche.threads", SCM_FIND_MODULE_CREATE); |
| 363 |
sym_not_owned = SCM_INTERN("not-owned"); |
| 364 |
sym_abandoned = SCM_INTERN("abandoned"); |
| 365 |
sym_not_abandoned = SCM_INTERN("not-abandoned"); |
| 366 |
Scm_InitStaticClass(&Scm_MutexClass, "<mutex>", mod, mutex_slots, 0); |
| 367 |
Scm_InitStaticClass(&Scm_ConditionVariableClass, "<condition-variable>", mod, cv_slots, 0); |
| 368 |
} |