| 1 |
;;; |
| 2 |
;;; threads.scm - thread related procedures. |
| 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 |
(declare) ;; a dummy form to suppress generation of "sci" file |
| 35 |
(define-module gauche.threads |
| 36 |
(export gauche-thread-type |
| 37 |
current-thread ;re-exporting the builtin |
| 38 |
|
| 39 |
;; these are defeind in libexc.scm |
| 40 |
<thread-exception> &thread make-thread-condition thread-condition? |
| 41 |
<uncaught-exception> &uncaught-exception |
| 42 |
make-uncaught-exception-condition |
| 43 |
uncaught-exception-condition? uncaught-exception-condition-reason |
| 44 |
<terminated-thread-exception> &thread-already-terminated |
| 45 |
make-thread-already-terminated-condition |
| 46 |
thread-already-terminated-condition? |
| 47 |
<join-timeout-exception> &thread-timeout |
| 48 |
make-thread-timeout-condition thread-timeout-condition? |
| 49 |
<abandoned-mutex-exception> &thread-abandoned-mutex |
| 50 |
make-thread-abandoned-mutex-condition |
| 51 |
thread-abandoned-mutex-condition? |
| 52 |
<concurrent-modification-violation> &concurrent-modification |
| 53 |
make-concurrent-modification-violation |
| 54 |
concurrent-modification-violation? |
| 55 |
|
| 56 |
thread? make-thread thread-name thread-specific-set! thread-specific |
| 57 |
thread-state thread-start! thread-try-start! |
| 58 |
thread-yield! thread-sleep! |
| 59 |
thread-join! thread-terminate! thread-schedule-terminate! |
| 60 |
thread-stop! thread-cont! |
| 61 |
run-once |
| 62 |
|
| 63 |
<mutex> mutex? |
| 64 |
make-mutex mutex-name mutex-state |
| 65 |
mutex-specific-set! mutex-specific |
| 66 |
with-locking-mutex mutex-lock! mutex-unlock! |
| 67 |
mutex-locker mutex-unlocker |
| 68 |
|
| 69 |
<condition-variable> condition-variable? |
| 70 |
make-condition-variable condition-variable-name |
| 71 |
condition-variable-specific condition-variable-specific-set! |
| 72 |
condition-variable-signal! condition-variable-broadcast! |
| 73 |
|
| 74 |
<thread-local> thread-local? |
| 75 |
make-thread-local tlref tlset! |
| 76 |
|
| 77 |
current-time time? time->seconds seconds->time |
| 78 |
|
| 79 |
join-timeout-exception? abandoned-mutex-exception? |
| 80 |
terminated-thread-exception? uncaught-exception? |
| 81 |
uncaught-exception-reason |
| 82 |
|
| 83 |
atom atom? atom-ref atomic atomic-update! |
| 84 |
|
| 85 |
<semaphore> make-semaphore semaphore? |
| 86 |
semaphore-acquire! semaphore-release! |
| 87 |
<latch> make-latch latch? |
| 88 |
latch-dec! latch-clear! latch-await |
| 89 |
<barrier> make-barrier barrier? |
| 90 |
barrier-reset! barrier-await barrier-broken? |
| 91 |
)) |
| 92 |
(select-module gauche.threads) |
| 93 |
|
| 94 |
(inline-stub |
| 95 |
(.include "gauche/priv/configP.h" |
| 96 |
"gauche/parameter.h") |
| 97 |
) |
| 98 |
|
| 99 |
;;=============================================================== |
| 100 |
;; System query |
| 101 |
;; |
| 102 |
|
| 103 |
;; TODO: for the consistency of ./configure --enable-threads, |
| 104 |
;; it should return 'pthreads' instead of 'pthread' on pthreads platform. |
| 105 |
(inline-stub |
| 106 |
(define-cproc gauche-thread-type () |
| 107 |
(.cond [(defined GAUCHE_USE_PTHREADS) (return 'pthread)] |
| 108 |
[(defined GAUCHE_USE_WTHREADS) (return 'win32)] |
| 109 |
[else (Scm_Panic "Can't determine thread type. Configuration error?")]))) |
| 110 |
|
| 111 |
;;=============================================================== |
| 112 |
;; Thread |
| 113 |
;; |
| 114 |
|
| 115 |
(define (thread? obj) (is-a? obj <thread>)) |
| 116 |
|
| 117 |
(define (thread-name thread) |
| 118 |
(assume-type thread <thread>) |
| 119 |
(slot-ref thread 'name)) |
| 120 |
|
| 121 |
(define (thread-specific-set! thread value) |
| 122 |
(assume-type thread <thread>) |
| 123 |
(slot-set! thread 'specific value)) |
| 124 |
|
| 125 |
(define thread-specific |
| 126 |
(getter-with-setter |
| 127 |
(^[thread] |
| 128 |
(assume-type thread <thread>) |
| 129 |
(slot-ref thread 'specific)) |
| 130 |
thread-specific-set!)) |
| 131 |
|
| 132 |
(define (make-thread thunk :optional (name #f)) |
| 133 |
(rlet1 t (%make-thread thunk name) |
| 134 |
((with-module gauche.internal %vm-custom-error-reporter-set!) t (^e #f)))) |
| 135 |
|
| 136 |
(inline-stub |
| 137 |
(define-cproc thread-state (vm::<thread>) |
| 138 |
(case (-> vm state) |
| 139 |
[(SCM_VM_NEW) (return 'new)] |
| 140 |
[(SCM_VM_RUNNABLE) (return 'runnable)] |
| 141 |
[(SCM_VM_STOPPED) (return 'stopped)] |
| 142 |
[(SCM_VM_TERMINATED)(return 'terminated)] |
| 143 |
[else (Scm_Error "[internal] thread state has invalid value: %d" |
| 144 |
(-> vm state)) |
| 145 |
(return SCM_UNDEFINED)])) ;dummy |
| 146 |
|
| 147 |
(define-cproc %make-thread (thunk::<procedure> name) Scm_MakeThread) |
| 148 |
|
| 149 |
(define-cproc thread-start! (vm::<thread>) |
| 150 |
(return (Scm_ThreadStart vm 0))) |
| 151 |
|
| 152 |
(define-cproc thread-try-start! (vm::<thread>) |
| 153 |
(return (Scm_ThreadStart vm SCM_THREAD_START_TRYSTART))) |
| 154 |
|
| 155 |
(define-cproc thread-yield! () ::<void> Scm_YieldCPU) |
| 156 |
|
| 157 |
(define-cproc thread-sleep! (timeout) Scm_ThreadSleep) |
| 158 |
|
| 159 |
(define-cproc thread-join! (vm::<thread> :optional (timeout #f) timeout-val) |
| 160 |
Scm_ThreadJoin) |
| 161 |
|
| 162 |
(define-cproc thread-terminate! (vm::<thread> :key (force #f)) |
| 163 |
(let* ([flags::u_long 0]) |
| 164 |
(when (not (SCM_FALSEP force)) |
| 165 |
(set! flags SCM_THREAD_TERMINATE_FORCIBLE)) |
| 166 |
(return (Scm_ThreadTerminate vm flags)))) |
| 167 |
|
| 168 |
(define-cproc thread-schedule-terminate! (vm::<thread>) ::<void> ;SRFI-226 |
| 169 |
(Scm_ThreadTerminate vm SCM_THREAD_TERMINATE_SCHEDULE)) |
| 170 |
|
| 171 |
(define-cproc thread-stop! (target::<thread> |
| 172 |
:optional (timeout #f) (timeout-val #f)) |
| 173 |
Scm_ThreadStop) |
| 174 |
|
| 175 |
(define-cproc thread-cont! (target::<thread>) Scm_ThreadCont) |
| 176 |
) |
| 177 |
|
| 178 |
;;=============================================================== |
| 179 |
;; Mutex |
| 180 |
;; |
| 181 |
|
| 182 |
(define (mutex? obj) (is-a? obj <mutex>)) |
| 183 |
|
| 184 |
(define (mutex-name mutex) |
| 185 |
(assume-type mutex <mutex>) |
| 186 |
(slot-ref mutex 'name)) |
| 187 |
|
| 188 |
(define (mutex-state mutex) |
| 189 |
(assume-type mutex <mutex>) |
| 190 |
(slot-ref mutex 'state)) |
| 191 |
|
| 192 |
(define (mutex-specific-set! mutex value) |
| 193 |
(assume-type mutex <mutex>) |
| 194 |
(slot-set! mutex 'specific value)) |
| 195 |
|
| 196 |
(define mutex-specific |
| 197 |
(getter-with-setter |
| 198 |
(^[mutex] |
| 199 |
(assume-type mutex <mutex>) |
| 200 |
(slot-ref mutex 'specific)) |
| 201 |
mutex-specific-set!)) |
| 202 |
|
| 203 |
(define-inline (with-locking-mutex mutex thunk) |
| 204 |
(dynamic-wind |
| 205 |
(mutex-locker mutex) |
| 206 |
thunk |
| 207 |
(mutex-unlocker mutex))) |
| 208 |
|
| 209 |
(inline-stub |
| 210 |
(define-cproc make-mutex (:optional (name #f)) Scm_MakeMutex) |
| 211 |
|
| 212 |
(define-cise-stmt with-mutex |
| 213 |
[(_ mutex . form) |
| 214 |
`(begin |
| 215 |
(cast void (SCM_INTERNAL_MUTEX_LOCK ,mutex)) |
| 216 |
,@form |
| 217 |
(cast void (SCM_INTERNAL_MUTEX_UNLOCK ,mutex)))]) |
| 218 |
|
| 219 |
(define-cproc mutex-lock! (mutex::<mutex> :optional (timeout #f) thread) |
| 220 |
(let* ([owner::ScmVM* NULL]) |
| 221 |
(cond [(SCM_VMP thread) (set! owner (SCM_VM thread))] |
| 222 |
[(SCM_UNBOUNDP thread) (set! owner (Scm_VM))] |
| 223 |
[(not (SCM_FALSEP thread)) (SCM_TYPE_ERROR thread "thread or #f")]) |
| 224 |
(return (Scm_MutexLock mutex timeout owner)))) |
| 225 |
|
| 226 |
(define-cproc mutex-unlock! (mutex::<mutex> :optional (cv #f) (timeout #f)) |
| 227 |
(let* ([cond::ScmConditionVariable* NULL]) |
| 228 |
(cond [(SCM_CONDITION_VARIABLE_P cv) (set! cond (SCM_CONDITION_VARIABLE cv))] |
| 229 |
[(not (SCM_FALSEP cv)) (SCM_TYPE_ERROR cv "condition variale or #f")]) |
| 230 |
(return (Scm_MutexUnlock mutex cond timeout)))) |
| 231 |
|
| 232 |
(define-cproc mutex-locker (mutex::<mutex>) Scm_MutexLocker) |
| 233 |
(define-cproc mutex-unlocker (mutex::<mutex>) Scm_MutexUnlocker) |
| 234 |
) |
| 235 |
|
| 236 |
;; (run-once expr ...) |
| 237 |
;; The first thread that evaluates this form evaluate expr and memoize |
| 238 |
;; its result(s). The rest of the threads returns the memoized result(s). |
| 239 |
;; NB: This inserts literal mutex and box in the code. |
| 240 |
|
| 241 |
(define-syntax run-once |
| 242 |
(er-macro-transformer |
| 243 |
(^[f r c] |
| 244 |
(if (null? (cdr f)) |
| 245 |
(error "Malformed run-once:" f) |
| 246 |
(let ([exprs (cdr f)] |
| 247 |
[mutex (make-mutex)] |
| 248 |
[results (box #f)]) ;list if evaluated |
| 249 |
(quasirename r |
| 250 |
`(with-locking-mutex ,mutex |
| 251 |
(^[] (unless (unbox ,results) |
| 252 |
(receive xs (begin ,@exprs) |
| 253 |
(set-box! ,results xs))) |
| 254 |
(apply values (unbox ,results)))))))))) |
| 255 |
|
| 256 |
;;=============================================================== |
| 257 |
;; Condition variable |
| 258 |
;; |
| 259 |
|
| 260 |
(define (condition-variable? obj) (is-a? obj <condition-variable>)) |
| 261 |
|
| 262 |
(define (condition-variable-name cv) |
| 263 |
(assume-type cv <condition-variable>) |
| 264 |
(slot-ref cv 'name)) |
| 265 |
|
| 266 |
(define (condition-variable-specific-set! cv value) |
| 267 |
(assume-type cv <condition-variable>) |
| 268 |
(slot-set! cv 'specific value)) |
| 269 |
|
| 270 |
(define condition-variable-specific |
| 271 |
(getter-with-setter |
| 272 |
(lambda (cv) |
| 273 |
(assume-type cv <condition-variable>) |
| 274 |
(slot-ref cv 'specific)) |
| 275 |
condition-variable-specific-set!)) |
| 276 |
|
| 277 |
(inline-stub |
| 278 |
(define-cproc make-condition-variable (:optional (name #f)) |
| 279 |
Scm_MakeConditionVariable) |
| 280 |
|
| 281 |
(define-cproc condition-variable-signal! (cv::<condition-variable>) |
| 282 |
Scm_ConditionVariableSignal) |
| 283 |
|
| 284 |
(define-cproc condition-variable-broadcast! (cv::<condition-variable>) |
| 285 |
Scm_ConditionVariableBroadcast) |
| 286 |
) |
| 287 |
|
| 288 |
;;=============================================================== |
| 289 |
;; Thread locals (SRFI-226) |
| 290 |
;; |
| 291 |
|
| 292 |
(define-cproc make-thread-local (initval :optional (inheritable? #f) (name #f)) |
| 293 |
(return |
| 294 |
(SCM_OBJ (Scm_MakeThreadLocal name initval |
| 295 |
(?: (SCM_FALSEP inheritable?) |
| 296 |
0 |
| 297 |
SCM_THREAD_LOCAL_INHERITABLE))))) |
| 298 |
|
| 299 |
(define-cproc thread-local? (obj) ::<boolean> |
| 300 |
(return (SCM_THREAD_LOCAL_P obj))) |
| 301 |
|
| 302 |
(define-cproc tlset! (tl::<thread-local> obj) ::<void> |
| 303 |
(Scm_ThreadLocalSet (Scm_VM) tl obj)) |
| 304 |
|
| 305 |
(define-cproc tlref (tl::<thread-local>) |
| 306 |
(setter tlset!) |
| 307 |
(return (Scm_ThreadLocalRef (Scm_VM) tl))) |
| 308 |
|
| 309 |
|
| 310 |
;;=============================================================== |
| 311 |
;; Exceptions |
| 312 |
;; |
| 313 |
|
| 314 |
(define (join-timeout-exception? obj) |
| 315 |
(is-a? obj <join-timeout-exception>)) |
| 316 |
|
| 317 |
(define (abandoned-mutex-exception? obj) |
| 318 |
(is-a? obj <abandoned-mutex-exception>)) |
| 319 |
|
| 320 |
(define (terminated-thread-exception? obj) |
| 321 |
(is-a? obj <terminated-thread-exception>)) |
| 322 |
|
| 323 |
(define (uncaught-exception? obj) |
| 324 |
(is-a? obj <uncaught-exception>)) |
| 325 |
|
| 326 |
(define (uncaught-exception-reason exc) |
| 327 |
(assume-type exc <uncaught-exception>) |
| 328 |
(slot-ref exc 'reason)) |
| 329 |
|
| 330 |
;;=============================================================== |
| 331 |
;; Atom |
| 332 |
;; |
| 333 |
|
| 334 |
(define-class <atom> () |
| 335 |
((applier :init-keyword :applier |
| 336 |
:immutable #t) |
| 337 |
(updater :init-keyword :updater |
| 338 |
:immutable #t))) |
| 339 |
|
| 340 |
(define (atom? obj) (is-a? obj <atom>)) |
| 341 |
|
| 342 |
(define (atom . vals) |
| 343 |
(define m (make-mutex)) |
| 344 |
(let-syntax ([with-lock |
| 345 |
(syntax-rules () |
| 346 |
[(_ timeout timeout-val timeout-vals . form) |
| 347 |
(unwind-protect |
| 348 |
(if (mutex-lock! m timeout) |
| 349 |
(begin . form) |
| 350 |
(if (null? timeout-vals) |
| 351 |
timeout-val |
| 352 |
(apply values (cons timeout-val timeout-vals)))) |
| 353 |
(when (eq? (mutex-state m) (current-thread)) |
| 354 |
(mutex-unlock! m)))])]) |
| 355 |
;; TODO: we may expand special cases like vals is 1 to 3 elements long, |
| 356 |
;; avoiding creation of lists every time updater is called. |
| 357 |
(make <atom> |
| 358 |
:applier |
| 359 |
(^[proc timeout timeout-val timeout-vals] |
| 360 |
(with-lock timeout timeout-val timeout-vals (apply proc vals))) |
| 361 |
:updater |
| 362 |
(^[proc timeout timeout-val timeout-vals] |
| 363 |
(with-lock timeout timeout-val timeout-vals |
| 364 |
(receive newvals (apply proc vals) |
| 365 |
(unless (>= (length newvals) (length vals)) |
| 366 |
(errorf "atomic-update!: procedure returned too few \ |
| 367 |
number of values (~a, while ~a expected)" |
| 368 |
(length newvals) (length vals))) |
| 369 |
(set! vals (take newvals (length vals))) |
| 370 |
(apply values newvals))))))) |
| 371 |
|
| 372 |
(define (atomic atom proc :optional (timeout #f) (timeout-val #f) :rest vals) |
| 373 |
(unless (atom? atom) (error "atom required, but got:" atom)) |
| 374 |
((~ atom'applier) proc timeout timeout-val vals)) |
| 375 |
|
| 376 |
(define (atomic-update! atom proc :optional (timeout #f) (timeout-val #f) :rest vals) |
| 377 |
(unless (atom? atom) (error "atom required, but got:" atom)) |
| 378 |
((~ atom'updater) proc timeout timeout-val vals)) |
| 379 |
|
| 380 |
(define (atom-ref atom :optional (index 0) (timeout #f) (timeout-val #f)) |
| 381 |
(unless (atom? atom) (error "atom required, but got:" atom)) |
| 382 |
((~ atom'applier) (^ xs (list-ref xs index)) timeout timeout-val '())) |
| 383 |
|
| 384 |
;;=============================================================== |
| 385 |
;; Semaphore |
| 386 |
;; |
| 387 |
|
| 388 |
(define-class <semaphore> () |
| 389 |
((name :init-keyword :name ;for information only |
| 390 |
:immutable #t) |
| 391 |
(count :init-keyword :count) |
| 392 |
(mutex :init-keyword :mutex |
| 393 |
:immutable #t) |
| 394 |
(cv :init-keyword :cv |
| 395 |
:immutable #t))) |
| 396 |
|
| 397 |
(define (make-semaphore :optional (init-value 0) (name #f)) |
| 398 |
(make <semaphore> |
| 399 |
:name name :count init-value |
| 400 |
:mutex (make-mutex) :cv (make-condition-variable))) |
| 401 |
|
| 402 |
(define (semaphore? obj) (is-a? obj <semaphore>)) |
| 403 |
|
| 404 |
(define-method write-object ((s <semaphore>) port) |
| 405 |
(format port "#<semaphore ~d" (~ s'count)) |
| 406 |
(if-let1 name (~ s'name) |
| 407 |
(format port " ~s>" name) |
| 408 |
(format port ">"))) |
| 409 |
|
| 410 |
(define (semaphore-acquire! sem :optional (timeout #f) (timeout-val #f)) |
| 411 |
(assume-type sem <semaphore>) |
| 412 |
(let loop () |
| 413 |
(mutex-lock! (~ sem'mutex)) |
| 414 |
(cond [(> (~ sem'count) 0) |
| 415 |
(dec! (~ sem'count)) |
| 416 |
(mutex-unlock! (~ sem'mutex)) |
| 417 |
#t] |
| 418 |
[(mutex-unlock! (~ sem'mutex) (~ sem'cv) timeout) (loop)] |
| 419 |
[else timeout-val]))) ;timeout |
| 420 |
|
| 421 |
;; We allow to release more than one tokens at once. |
| 422 |
;; 'all means all |
| 423 |
(define (semaphore-release! sem :optional (count 1)) |
| 424 |
(assume-type sem <semaphore>) |
| 425 |
(assume (and (exact-integer? count) (> count 0))) |
| 426 |
(mutex-lock! (~ sem'mutex)) |
| 427 |
(inc! (~ sem'count) count) |
| 428 |
(when (> (~ sem'count) 0) |
| 429 |
(if (= count 1) |
| 430 |
(condition-variable-signal! (~ sem'cv)) |
| 431 |
(condition-variable-broadcast! (~ sem'cv)))) |
| 432 |
(mutex-unlock! (~ sem'mutex))) |
| 433 |
|
| 434 |
;;=============================================================== |
| 435 |
;; Latch |
| 436 |
;; |
| 437 |
|
| 438 |
(define-class <latch> () |
| 439 |
((name :init-keyword :name ; for information only |
| 440 |
:immutable #t) |
| 441 |
(count :init-keyword :count) |
| 442 |
(mutex :init-keyword :mutex |
| 443 |
:immutable #t) |
| 444 |
(cv :init-keyword :cv |
| 445 |
:immutable #t))) |
| 446 |
|
| 447 |
(define (make-latch initial-count :optional (name #f)) |
| 448 |
(assume (and (exact-integer? initial-count) |
| 449 |
(positive? initial-count))) |
| 450 |
(make <latch> |
| 451 |
:name name :count initial-count |
| 452 |
:mutex (make-mutex) :cv (make-condition-variable))) |
| 453 |
|
| 454 |
(define (latch? obj) (is-a? obj <latch>)) |
| 455 |
|
| 456 |
(define-method write-object ((l <latch>) port) |
| 457 |
(format port "#<latch ~d" (~ l'count)) |
| 458 |
(if-let1 name (~ l'name) |
| 459 |
(format port " ~s>" name) |
| 460 |
(format port ">"))) |
| 461 |
|
| 462 |
(define (latch-dec! latch :optional (n 1)) |
| 463 |
(assume-type latch <latch>) |
| 464 |
(assume (exact-integer? n)) |
| 465 |
(mutex-lock! (~ latch'mutex)) |
| 466 |
(when (> (~ latch'count) 0) |
| 467 |
(dec! (~ latch'count) n) |
| 468 |
(when (<= (~ latch'count) 0) |
| 469 |
(condition-variable-broadcast! (~ latch'cv)))) |
| 470 |
(let1 n (~ latch'count) |
| 471 |
(mutex-unlock! (~ latch'mutex)) |
| 472 |
n)) |
| 473 |
|
| 474 |
(define (latch-clear! latch) |
| 475 |
(assume-type latch <latch>) |
| 476 |
(mutex-lock! (~ latch'mutex)) |
| 477 |
(rlet1 c (~ latch'count) |
| 478 |
(unless (zero? c) |
| 479 |
(set! (~ latch'count) 0) |
| 480 |
(condition-variable-broadcast! (~ latch'cv))) |
| 481 |
(mutex-unlock! (~ latch'mutex)))) |
| 482 |
|
| 483 |
(define (latch-await latch :optional (timeout #f) (timeout-val #f)) |
| 484 |
(assume-type latch <latch>) |
| 485 |
(let loop () |
| 486 |
(mutex-lock! (~ latch'mutex)) |
| 487 |
(cond [(<= (~ latch'count) 0) |
| 488 |
(mutex-unlock! (~ latch'mutex)) |
| 489 |
#t] |
| 490 |
[(mutex-unlock! (~ latch'mutex) (~ latch'cv) timeout) (loop)] |
| 491 |
[else timeout-val]))) ;timeout |
| 492 |
|
| 493 |
;;=============================================================== |
| 494 |
;; Barrier |
| 495 |
;; |
| 496 |
|
| 497 |
(define-class <barrier> () |
| 498 |
((name :init-keyword :name ; for information only |
| 499 |
:immutable #t) |
| 500 |
(threshold :init-keyword :threshold |
| 501 |
:immutable #t) |
| 502 |
(count :init-keyword :count) |
| 503 |
(generation :init-keyword :generation) |
| 504 |
(broken :init-keyword :broken) |
| 505 |
(action :init-keyword :action |
| 506 |
:immutable #t) |
| 507 |
(mutex :init-keyword :mutex |
| 508 |
:immutable #t) |
| 509 |
(cv :init-keyword :cv |
| 510 |
:immutable #t))) |
| 511 |
|
| 512 |
(define (make-barrier threshold :optional (action #f) (name #f)) |
| 513 |
(make <barrier> |
| 514 |
:name name :threshold threshold :count 0 :generation 0 :broken #f |
| 515 |
:action action |
| 516 |
:mutex (make-mutex) :cv (make-condition-variable))) |
| 517 |
|
| 518 |
(define (barrier? obj) (is-a? obj <barrier>)) |
| 519 |
|
| 520 |
(define-method write-object ((b <barrier>) port) |
| 521 |
(format port "#<barrier ~d/~d" (~ b'count) (~ b'threshold)) |
| 522 |
(if-let1 name (~ b'name) |
| 523 |
(format port " ~s>" name) |
| 524 |
(format port ">"))) |
| 525 |
|
| 526 |
(define (barrier-reset! barrier) |
| 527 |
(assume-type barrier <barrier>) |
| 528 |
(mutex-lock! (~ barrier'mutex)) |
| 529 |
(set! (~ barrier'broken) #f) |
| 530 |
(inc! (~ barrier'generation)) |
| 531 |
(set! (~ barrier'count) 0) |
| 532 |
(condition-variable-broadcast! (~ barrier'cv)) |
| 533 |
(mutex-unlock! (~ barrier'mutex))) |
| 534 |
|
| 535 |
(define (barrier-broken? barrier) |
| 536 |
(assume-type barrier <barrier>) |
| 537 |
(~ barrier'broken)) |
| 538 |
|
| 539 |
(define (barrier-await barrier :optional (timeout #f) (timeout-val #f)) |
| 540 |
(assume-type barrier <barrier>) |
| 541 |
(let loop ((gen #f) |
| 542 |
(place #f)) |
| 543 |
(mutex-lock! (~ barrier'mutex)) |
| 544 |
(unless gen ;first time |
| 545 |
(inc! (~ barrier'count))) |
| 546 |
(let ([gen (or gen (~ barrier'generation))] |
| 547 |
[place (or place (- (~ barrier'threshold) (~ barrier'count)))] |
| 548 |
[action-exception #f]) |
| 549 |
(cond [(~ barrier'broken) ;already broken |
| 550 |
(mutex-unlock! (~ barrier'mutex)) |
| 551 |
timeout-val] |
| 552 |
[(< gen (~ barrier'generation)) |
| 553 |
(mutex-unlock! (~ barrier'mutex)) |
| 554 |
place] |
| 555 |
[(= place 0) ;I'm the last |
| 556 |
(when (~ barrier'action) |
| 557 |
;; We run the action without holding the lock. If there're more |
| 558 |
;; threads than supposed and came after this, it gets the |
| 559 |
;; same generation and negative place (so it won't run the |
| 560 |
;; action) and wait on the same cv. |
| 561 |
(mutex-unlock! (~ barrier'mutex)) |
| 562 |
(guard (e [else (set! (~ barrier'broken) #t) |
| 563 |
(set! action-exception e)]) |
| 564 |
((~ barrier'action))) |
| 565 |
(mutex-lock! (~ barrier'mutex))) |
| 566 |
(set! (~ barrier'count) 0) |
| 567 |
(inc! (~ barrier'generation)) |
| 568 |
(condition-variable-broadcast! (~ barrier'cv)) |
| 569 |
(mutex-unlock! (~ barrier'mutex)) |
| 570 |
(when action-exception |
| 571 |
(raise action-exception)) |
| 572 |
place] |
| 573 |
[(mutex-unlock! (~ barrier'mutex) (~ barrier'cv) timeout) |
| 574 |
(loop gen place)] |
| 575 |
[else ;timeout -> broken |
| 576 |
(mutex-lock! (~ barrier'mutex)) |
| 577 |
(set! (~ barrier'broken) #t) |
| 578 |
(condition-variable-broadcast! (~ barrier'cv)) |
| 579 |
(mutex-unlock! (~ barrier'mutex)) |
| 580 |
timeout-val])))) |