Commit | Line | Data |
---|---|---|
1563d6c7 LC |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org> | |
3 | ;;; | |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
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. | |
10 | ;;; | |
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. | |
15 | ;;; | |
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/>. | |
18 | ||
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) | |
8902d0f2 | 26 | #:use-module ((guix build syscalls) #:select (set-thread-name)) |
1563d6c7 LC |
27 | #:export (pool? |
28 | make-pool | |
29 | pool-enqueue! | |
30 | pool-idle? | |
31 | eventually)) | |
32 | ||
33 | ;;; Commentary: | |
34 | ;;; | |
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. | |
39 | ;;; | |
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. | |
44 | ;;; | |
45 | ;;; Code: | |
46 | ||
47 | (define-record-type <pool> | |
48 | (%make-pool queue mutex condvar workers) | |
49 | pool? | |
50 | (queue pool-queue) | |
51 | (mutex pool-mutex) | |
52 | (condvar pool-condition-variable) | |
53 | (workers pool-workers)) | |
54 | ||
55 | (define-syntax-rule (without-mutex mutex exp ...) | |
56 | (dynamic-wind | |
57 | (lambda () | |
58 | (unlock-mutex mutex)) | |
59 | (lambda () | |
60 | exp ...) | |
61 | (lambda () | |
62 | (lock-mutex mutex)))) | |
63 | ||
8902d0f2 LC |
64 | (define* (worker-thunk mutex condvar pop-queue |
65 | #:key (thread-name "guix worker")) | |
1563d6c7 LC |
66 | "Return the thunk executed by worker threads." |
67 | (define (loop) | |
68 | (match (pop-queue) | |
69 | (#f ;empty queue | |
70 | (wait-condition-variable condvar mutex)) | |
71 | ((? procedure? proc) | |
72 | ;; Release MUTEX while executing PROC. | |
73 | (without-mutex mutex | |
74 | (catch #t proc | |
75 | (lambda (key . args) | |
76 | ;; XXX: In Guile 2.0 ports are not thread-safe, so this could | |
77 | ;; crash (Guile 2.2 is fine). | |
78 | (display-backtrace (make-stack #t) (current-error-port)) | |
79 | (print-exception (current-error-port) | |
80 | (stack-ref (make-stack #t) 0) | |
81 | key args)))))) | |
82 | (loop)) | |
83 | ||
84 | (lambda () | |
8902d0f2 LC |
85 | (catch 'system-error |
86 | (lambda () | |
87 | (set-thread-name thread-name)) | |
88 | (const #f)) | |
89 | ||
1563d6c7 LC |
90 | (with-mutex mutex |
91 | (loop)))) | |
92 | ||
8902d0f2 LC |
93 | (define* (make-pool #:optional (count (current-processor-count)) |
94 | #:key (thread-name "guix worker")) | |
95 | "Return a pool of COUNT workers. Use THREAD-NAME as the name of these | |
96 | threads as reported by the operating system." | |
1563d6c7 LC |
97 | (let* ((mutex (make-mutex)) |
98 | (condvar (make-condition-variable)) | |
99 | (queue (make-q)) | |
100 | (procs (unfold (cut >= <> count) | |
101 | (lambda (n) | |
102 | (worker-thunk mutex condvar | |
103 | (lambda () | |
104 | (and (not (q-empty? queue)) | |
8902d0f2 LC |
105 | (q-pop! queue))) |
106 | #:thread-name thread-name)) | |
1563d6c7 LC |
107 | 1+ |
108 | 0)) | |
109 | (threads (map (lambda (proc) | |
110 | (call-with-new-thread proc)) | |
111 | procs))) | |
112 | (%make-pool queue mutex condvar threads))) | |
113 | ||
114 | (define (pool-enqueue! pool thunk) | |
115 | "Enqueue THUNK for future execution by POOL." | |
116 | (with-mutex (pool-mutex pool) | |
117 | (enq! (pool-queue pool) thunk) | |
118 | (signal-condition-variable (pool-condition-variable pool)))) | |
119 | ||
120 | (define (pool-idle? pool) | |
121 | "Return true if POOL doesn't have any task in its queue." | |
122 | (with-mutex (pool-mutex pool) | |
123 | (q-empty? (pool-queue pool)))) | |
124 | ||
125 | (define-syntax-rule (eventually pool exp ...) | |
126 | "Run EXP eventually on one of the workers of POOL." | |
127 | (pool-enqueue! pool (lambda () exp ...))) | |
128 | ||
129 | ;;; Local Variables: | |
130 | ;;; eval: (put 'without-mutex 'scheme-indent-function 1) | |
131 | ;;; End: | |
132 | ||
133 | ;;; workers.scm ends here |