1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix workers)
20 #:use-module (ice-9 threads)
21 #:use-module (ice-9 match)
22 #:use-module (ice-9 q)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-9)
25 #:use-module (srfi srfi-26)
26 #:use-module ((guix build syscalls) #:select (set-thread-name))
35 ;;; This module implements "worker pools". Worker pools are the low-level
36 ;;; mechanism that's behind futures: there's a fixed set of threads
37 ;;; ("workers") that one can submit work to, and one of them will eventually
38 ;;; pick the submitted tasks.
40 ;;; Unlike futures, these worker pools are meant to be used for tasks that
41 ;;; have a side-effect. Thus, we never "touch" a task that was submitted like
42 ;;; we "touch" a future. Instead, we simply assume that the task will
43 ;;; eventually complete.
47 (define-record-type <pool>
48 (%make-pool queue mutex condvar workers busy)
52 (condvar pool-condition-variable)
53 (workers pool-workers)
56 (define-syntax-rule (without-mutex mutex exp ...)
65 (define* (worker-thunk mutex condvar pop-queue
66 #:key idle busy (thread-name "guix worker"))
67 "Return the thunk executed by worker threads."
72 (wait-condition-variable condvar mutex)
75 ;; Release MUTEX while executing PROC.
80 ;; XXX: In Guile 2.0 ports are not thread-safe, so this could
81 ;; crash (Guile 2.2 is fine).
82 (display-backtrace (make-stack #t) (current-error-port))
83 (print-exception (current-error-port)
84 (and=> (make-stack #t)
92 (set-thread-name thread-name))
98 (define* (make-pool #:optional (count (current-processor-count))
99 #:key (thread-name "guix worker"))
100 "Return a pool of COUNT workers. Use THREAD-NAME as the name of these
101 threads as reported by the operating system."
102 (let* ((mutex (make-mutex))
103 (condvar (make-condition-variable))
106 (procs (unfold (cut >= <> count)
108 (worker-thunk mutex condvar
110 (and (not (q-empty? queue))
113 (set! busy (+ 1 busy)))
115 (set! busy (- busy 1)))
116 #:thread-name thread-name))
119 (threads (map (lambda (proc)
120 (call-with-new-thread proc))
122 (%make-pool queue mutex condvar threads (lambda () busy))))
124 (define (pool-enqueue! pool thunk)
125 "Enqueue THUNK for future execution by POOL."
126 (with-mutex (pool-mutex pool)
127 (enq! (pool-queue pool) thunk)
128 (signal-condition-variable (pool-condition-variable pool))))
130 (define (pool-idle? pool)
131 "Return true if POOL doesn't have any task in its queue and all the workers
132 are currently idle (i.e., waiting for a task)."
133 (with-mutex (pool-mutex pool)
134 (and (q-empty? (pool-queue pool))
135 (zero? ((pool-busy pool))))))
137 (define-syntax-rule (eventually pool exp ...)
138 "Run EXP eventually on one of the workers of POOL."
139 (pool-enqueue! pool (lambda () exp ...)))
142 ;;; eval: (put 'without-mutex 'scheme-indent-function 1)
145 ;;; workers.scm ends here