Debian Codegraph @

lib/control/thread-pool.scm

raw
1 ;;;
2 ;;; control.thread-pool - thread pool
3 ;;;
4 ;;; Copyright (c) 2003-2007 Time Intermedia Corporation, All rights reserved.
5 ;;; Copyright (c) 2010-2024 Shiro Kawai <[email protected]>
6 ;;;
7 ;;; Redistribution and use in source and binary forms, with or without
8 ;;; modification, are permitted provided that the following conditions
9 ;;; are met:
10 ;;;
11 ;;; 1. Redistributions of source code must retain the above copyright
12 ;;; notice, this list of conditions and the following disclaimer.
13 ;;;
14 ;;; 2. Redistributions in binary form must reproduce the above copyright
15 ;;; notice, this list of conditions and the following disclaimer in the
16 ;;; documentation and/or other materials provided with the distribution.
17 ;;;
18 ;;; 3. Neither the name of the authors nor the names of its contributors
19 ;;; may be used to endorse or promote products derived from this
20 ;;; software without specific prior written permission.
21 ;;;
22 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 ;; Originally written as a part of Kahua project http://www.kahua.org/
35 ;; Heavily modified by Shiro Kawai to generalize, and to use new Gauche
36 ;; features.
37
38 (define-module control.thread-pool
39 (use scheme.list)
40 (use srfi.19)
41 (use data.queue)
42 (use util.match)
43 (use gauche.threads)
44 (use gauche.record)
45 (use gauche.mop.propagate)
46 (use control.job)
47 (export <thread-pool>
48 <thread-pool-shut-down>
49 make-thread-pool thread-pool-results thread-pool-shut-down?
50 add-job! wait-all terminate-all!))
51 (select-module control.thread-pool)
52
53 ;; - Thread job is queued in job queue.
54 ;; - add-job! returns a job record, with which the client can track results.
55 ;; - optionally, the client can ask to queue the finished job to result-queue.
56 ;; - while exeuting the job, thread keeps job record in its 'specific' slot.
57 ;; - graceful termination is requested by 'over in the job queue.
58
59 (define-class <thread-pool> ()
60 ((result-queue :init-form (make-mtqueue)) ; Queue Job
61 ;; the rest of slots are private
62 (pool :init-keyword :pool :init-value '()) ; [Thread]
63 (size :init-keyword :size :init-value 2)
64 (job-queue :init-form (make-mtqueue)) ; Queue (Bool . Job)
65 (max-backlog :allocation :propagated
66 :propagate '(job-queue max-length)
67 :init-keyword :max-backlog)
68 (shut-down :init-value #f) ; #t if the pool is shut down
69 )
70 :metaclass <propagate-meta>)
71
72 (define (make-thread-pool size :key (max-backlog #f))
73 (make <thread-pool> :size size :max-backlog max-backlog))
74
75 (define-method initialize ((pool <thread-pool>) initargs)
76 (next-method)
77 (set! (~ pool'pool)
78 (list-tabulate (~ pool'size)
79 (lambda (_)
80 (thread-start! (make-thread (cut worker pool)))))))
81
82 (define (thread-pool-results pool) (~ pool'result-queue))
83 (define (thread-pool-shut-down? pool) (~ pool'shut-down))
84
85 ;; This condition is raised by add-job! when the pool is
86 ;; shutting down.
87 (define-condition-type <thread-pool-shut-down> <error> (pool #f))
88 (define (%shut-down pool)
89 (error <thread-pool-shut-down> :pool pool "Thread pool has shut down"))
90
91 (define (worker pool)
92 (define self (current-thread))
93 (match (dequeue/wait! (~ pool'job-queue))
94 [(need-result . job)
95 (thread-specific-set! self job)
96 (job-run! job) ; captures errors
97 (when need-result (enqueue! (~ pool'result-queue) job))
98 (thread-specific-set! self #f)
99 (worker pool)]
100 [_ #t])) ; no more jobs
101
102 ;; Returns job if queued, #f if job queue is full
103 (define (add-job! pool thunk :optional (need-result #f) (timeout #f))
104 (when (~ pool'shut-down) (%shut-down pool))
105 (let1 job (make-job thunk :cancellable #t)
106 (job-acknowledge! job)
107 (and (enqueue/wait! (~ pool'job-queue) (cons need-result job) timeout #f)
108 (if (~ pool'shut-down)
109 (%shut-down pool)
110 job))))
111
112 ;; Note: The signature has been changed from 0.9.1, in which wait-all
113 ;; only takes check-interval optional argument. It is impossible to detect
114 ;; the old usage, since integer is a valid argument as timeout. However,
115 ;; I hope it does little harm; for example, the old code (wait-all pool #e2e9)
116 ;; to expect check-interval in 2 seconds is now interpreted as the default
117 ;; check-interval and extremely long timeout---about 60 years---which virtually
118 ;; the same as saying "forever", so all you get is slighly off check-interval.
119 ;; Smaller check-interval may be a bit serious, since it may delay response
120 ;; in some situation. But the default 0.5 seconds isn't really bad, I guess.
121 (define (wait-all pool :optional (timeout #f) (check-interval #e5e8))
122 (define abstime
123 (cond [(is-a? timeout <time>) timeout]
124 [(real? timeout)
125 (receive (subsec sec) (modf timeout)
126 (add-duration (current-time)
127 (make-time time-duration
128 (round->exact (* subsec 1e9))
129 sec)))]
130 [(not timeout) #f]
131 [else (error "timeout must be either a real number, a <time> object, \
132 or #f, but got:" timeout)]))
133 (let loop ([now (and abstime (current-time))])
134 ;; NB: We assume the caller ensures no new jobs are inserted while calling
135 ;; wait-all.
136 (cond [(and (queue-empty? (~ pool'job-queue))
137 (= (mtqueue-num-waiting-readers (~ pool'job-queue))
138 (~ pool'size)))]
139 [(and abstime (time>=? now abstime)) #f] ;timeout
140 [else (sys-nanosleep check-interval)
141 (loop (and abstime (current-time)))])))
142
143 ;; For backward compatibility, allow (terminate-all! pool force-timeout)
144 ;; the proper API is (terminate-all! pool :force-timeout force-timeout)
145 ;; (as far as I know, only Kahua is affected by this API change).
146 ;; Rewrite to simplified version once Kahua switches to the new API.
147 (define (terminate-all! pool . args)
148 (match args
149 [(val) (%terminate-all! pool :force-timeout val)]
150 [_ (apply %terminate-all! pool args)]))
151
152 (define (%terminate-all! pool :key (force-timeout #f) (cancel-queued-jobs #f))
153 (define size (~ pool'size))
154
155 ;; First, make sure no more jobs are put into the queue.
156 (set! (~ pool'shut-down) #t)
157
158 ;; If requested, cancel jobs already queued but not being executing.
159 (when cancel-queued-jobs
160 (dolist [job (dequeue-all! (~ pool'job-queue))]
161 (job-mark-killed! (cdr job) "thread pool has shut down")
162 (enqueue! (~ pool'result-queue) (cdr job))))
163
164 ;; Sends threads termination message
165 (dotimes [count size]
166 (enqueue/wait! (~ pool'job-queue) 'over))
167
168 ;; Wait for termination of threads.
169 (dolist [t (~ pool'pool)]
170 (unless (thread-join! t force-timeout #f)
171 (and-let* ([job (thread-specific t)])
172 (job-mark-killed! job "thread pool has shut down"))
173 (thread-terminate! t))))