1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
;;; -*- mode: scheme; coding: utf-8; -*-
;;;
;;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (ice-9 futures)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (ice-9 q)
#:export (future make-future future? touch))
;;; Author: Ludovic Courtès <ludo@gnu.org>
;;;
;;; Commentary:
;;;
;;; This module provides an implementation of futures, a mechanism for
;;; fine-grain parallelism. Futures were first described by Henry Baker
;;; in ``The Incremental Garbage Collection of Processes'', 1977, and
;;; then implemented in MultiLisp (an implicit variant thereof, i.e.,
;;; without `touch'.)
;;;
;;; This modules uses a fixed thread pool, normally one per CPU core.
;;; Futures are off-loaded to these threads, when they are idle.
;;;
;;; Code:
;;;
;;; Futures.
;;;
(define-record-type <future>
(%make-future thunk done? mutex)
future?
(thunk future-thunk)
(done? future-done? set-future-done?!)
(result future-result set-future-result!)
(mutex future-mutex))
(define (make-future thunk)
"Return a new future for THUNK. Execution may start at any point
concurrently, or it can start at the time when the returned future is
touched."
(create-workers!)
(let ((future (%make-future thunk #f (make-mutex))))
(register-future! future)
future))
;;;
;;; Future queues.
;;;
(define %futures (make-q))
(define %futures-mutex (make-mutex))
(define %futures-available (make-condition-variable))
(define (register-future! future)
;; Register FUTURE as being processable.
(lock-mutex %futures-mutex)
(enq! %futures future)
(signal-condition-variable %futures-available)
(unlock-mutex %futures-mutex))
(define (process-future! future)
;; Process FUTURE, assuming its mutex is already taken.
(set-future-result! future
(catch #t
(lambda ()
(call-with-values (future-thunk future)
(lambda results
(lambda ()
(apply values results)))))
(lambda args
(lambda ()
(apply throw args)))))
(set-future-done?! future #t))
(define (process-futures)
;; Wait for futures to be available and process them.
(lock-mutex %futures-mutex)
(let loop ()
(wait-condition-variable %futures-available
%futures-mutex)
(or (q-empty? %futures)
(let ((future (deq! %futures)))
(lock-mutex (future-mutex future))
(or (and (future-done? future)
(unlock-mutex (future-mutex future)))
(begin
;; Do the actual work.
;; We want to release %FUTURES-MUTEX so that other workers
;; can progress. However, to avoid deadlocks, we have to
;; unlock FUTURE as well, to preserve lock ordering.
(unlock-mutex (future-mutex future))
(unlock-mutex %futures-mutex)
(lock-mutex (future-mutex future))
(or (future-done? future) ; lost the race?
(process-future! future))
(unlock-mutex (future-mutex future))
(lock-mutex %futures-mutex)))))
(loop)))
(define (touch future)
"Return the result of FUTURE, computing it if not already done."
(lock-mutex (future-mutex future))
(or (future-done? future)
(begin
;; Do the actual work. Unlock FUTURE first to preserve lock
;; ordering.
(unlock-mutex (future-mutex future))
(lock-mutex %futures-mutex)
(q-remove! %futures future)
(unlock-mutex %futures-mutex)
(lock-mutex (future-mutex future))
(or (future-done? future) ; lost the race?
(process-future! future))))
(unlock-mutex (future-mutex future))
((future-result future)))
;;;
;;; Workers.
;;;
(define %worker-count
(if (provided? 'threads)
(- (current-processor-count) 1)
0))
;; A dock of workers that stay here forever.
;; TODO
;; 1. Allow the pool to be shrunk, as in libgomp (though that we'd
;; need semaphores, which aren't yet in libguile!).
;; 2. Provide a `worker-count' fluid.
(define %workers '())
(define (%create-workers!)
(lock-mutex %futures-mutex)
(set! %workers
(unfold (lambda (i) (>= i %worker-count))
(lambda (i)
(call-with-new-thread process-futures))
1+
0))
(set! create-workers! (lambda () #t))
(unlock-mutex %futures-mutex))
(define create-workers!
(lambda () (%create-workers!)))
;;;
;;; Syntax.
;;;
(define-syntax-rule (future body)
"Return a new future for BODY."
(make-future (lambda () body)))
|