mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 11:50:28 +02:00
Futures: Avoid creating the worker pool more than once.
* module/ice-9/futures.scm (%create-workers!): Use 'with-mutex' in case an exception is thrown. Within the critical section, check to make sure the worker pool hasn't already been created by another thread.
This commit is contained in:
parent
92fac8c056
commit
be05b33609
1 changed files with 15 additions and 9 deletions
|
@ -19,6 +19,7 @@
|
||||||
(define-module (ice-9 futures)
|
(define-module (ice-9 futures)
|
||||||
#:use-module (srfi srfi-1)
|
#:use-module (srfi srfi-1)
|
||||||
#:use-module (srfi srfi-9)
|
#:use-module (srfi srfi-9)
|
||||||
|
#:use-module (ice-9 threads)
|
||||||
#:use-module (ice-9 q)
|
#:use-module (ice-9 q)
|
||||||
#:export (future make-future future? touch))
|
#:export (future make-future future? touch))
|
||||||
|
|
||||||
|
@ -157,15 +158,20 @@ touched."
|
||||||
(define %workers '())
|
(define %workers '())
|
||||||
|
|
||||||
(define (%create-workers!)
|
(define (%create-workers!)
|
||||||
(lock-mutex %futures-mutex)
|
(with-mutex
|
||||||
|
%futures-mutex
|
||||||
|
;; Setting 'create-workers!' to a no-op is an optimization, but it is
|
||||||
|
;; still possible for '%create-workers!' to be called more than once
|
||||||
|
;; from different threads. Therefore, to avoid creating %workers more
|
||||||
|
;; than once (and thus creating too many threads), we check to make
|
||||||
|
;; sure %workers is empty within the critical section.
|
||||||
|
(when (null? %workers)
|
||||||
(set! %workers
|
(set! %workers
|
||||||
(unfold (lambda (i) (>= i %worker-count))
|
(unfold (lambda (i) (>= i %worker-count))
|
||||||
(lambda (i)
|
(lambda (i) (call-with-new-thread process-futures))
|
||||||
(call-with-new-thread process-futures))
|
|
||||||
1+
|
1+
|
||||||
0))
|
0))
|
||||||
(set! create-workers! (lambda () #t))
|
(set! create-workers! (lambda () #t)))))
|
||||||
(unlock-mutex %futures-mutex))
|
|
||||||
|
|
||||||
(define create-workers!
|
(define create-workers!
|
||||||
(lambda () (%create-workers!)))
|
(lambda () (%create-workers!)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue