See ChangeLog from 2005-03-02.
[bpt/guile.git] / libguile / futures.c
CommitLineData
6b468ba4
MD
1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2003 Free Software Foundation, Inc.
2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
6b468ba4 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
6b468ba4 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
6b468ba4
MD
17
18
19\f
20
21#include "libguile/_scm.h"
22#include "libguile/eval.h"
23#include "libguile/ports.h"
24#include "libguile/validate.h"
25#include "libguile/stime.h"
26#include "libguile/threads.h"
27
28#include "libguile/futures.h"
29
30#define LINK(list, obj) \
31do { \
32 SCM_SET_FUTURE_NEXT (obj, list); \
33 list = obj; \
34} while (0)
35
36#define UNLINK(list, obj) \
37do { \
38 obj = list; \
39 list = SCM_FUTURE_NEXT (list); \
40} while (0)
41
9de87eea 42scm_i_pthread_mutex_t future_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
6b468ba4
MD
43
44static SCM futures = SCM_EOL;
45static SCM young = SCM_EOL;
46static SCM old = SCM_EOL;
47static SCM undead = SCM_EOL;
48
49static long last_switch;
50
51#ifdef SCM_FUTURES_DEBUG
52static int n_dead = 0;
53
54static SCM
55count (SCM ls)
56{
57 int n = 0;
d2e53ed6 58 while (!scm_is_null (ls))
6b468ba4
MD
59 {
60 ++n;
61 ls = SCM_FUTURE_NEXT (ls);
62 }
e11e83f3 63 return scm_from_int (n);
6b468ba4
MD
64}
65
66extern SCM scm_future_cache_status (void);
67
68SCM_DEFINE (scm_future_cache_status, "future-cache-status", 0, 0, 0,
69 (),
70 "Return a list containing number of futures, youngs, olds, undeads and deads.")
71#define FUNC_NAME s_scm_future_cache_status
72{
73 int nd = n_dead;
74 n_dead = 0;
75 return scm_list_5 (count (futures),
76 count (young),
77 count (old),
78 count (undead),
e11e83f3 79 scm_from_int (nd));
6b468ba4
MD
80}
81#undef FUNC_NAME
82
83#endif
84
85SCM *scm_loc_sys_thread_handler;
86
87SCM_DEFINE (scm_make_future, "make-future", 1, 0, 0,
88 (SCM thunk),
89 "Make a future evaluating THUNK.")
90#define FUNC_NAME s_scm_make_future
91{
92 SCM_VALIDATE_THUNK (1, thunk);
93 return scm_i_make_future (thunk);
94}
95#undef FUNC_NAME
96
97static char *s_future = "future";
98
99static void
100cleanup (scm_t_future *future)
101{
9de87eea
MV
102 scm_i_pthread_mutex_destroy (&future->mutex);
103 scm_i_pthread_cond_destroy (&future->cond);
6b468ba4
MD
104 scm_gc_free (future, sizeof (*future), s_future);
105#ifdef SCM_FUTURES_DEBUG
106 ++n_dead;
107#endif
108}
109
110static SCM
111future_loop (scm_t_future *future)
112{
9de87eea 113 scm_i_scm_pthread_mutex_lock (&future->mutex);
6b468ba4
MD
114 do {
115 if (future->status == SCM_FUTURE_SIGNAL_ME)
9de87eea 116 scm_i_pthread_cond_broadcast (&future->cond);
6b468ba4
MD
117 future->status = SCM_FUTURE_COMPUTING;
118 future->data = (SCM_CLOSUREP (future->data)
119 ? scm_i_call_closure_0 (future->data)
120 : scm_call_0 (future->data));
9de87eea 121 scm_i_scm_pthread_cond_wait (&future->cond, &future->mutex);
6b468ba4
MD
122 } while (!future->die_p);
123 future->status = SCM_FUTURE_DEAD;
9de87eea 124 scm_i_pthread_mutex_unlock (&future->mutex);
6b468ba4
MD
125 return SCM_UNSPECIFIED;
126}
127
128static SCM
129future_handler (scm_t_future *future, SCM key, SCM args)
130{
131 future->status = SCM_FUTURE_DEAD;
9de87eea 132 scm_i_pthread_mutex_unlock (&future->mutex);
6b468ba4
MD
133 return scm_apply_1 (*scm_loc_sys_thread_handler, key, args);
134}
135
136static SCM
137alloc_future (SCM thunk)
138{
139 scm_t_future *f = scm_gc_malloc (sizeof (*f), s_future);
140 SCM future;
141 f->data = SCM_BOOL_F;
9de87eea
MV
142 scm_i_pthread_mutex_init (&f->mutex, NULL);
143 scm_i_pthread_cond_init (&f->cond, NULL);
6b468ba4
MD
144 f->die_p = 0;
145 f->status = SCM_FUTURE_TASK_ASSIGNED;
9de87eea 146 scm_i_scm_pthread_mutex_lock (&future_admin_mutex);
6b468ba4
MD
147 SCM_NEWSMOB2 (future, scm_tc16_future, futures, f);
148 SCM_SET_FUTURE_DATA (future, thunk);
149 futures = future;
9de87eea 150 scm_i_pthread_mutex_unlock (&future_admin_mutex);
6b468ba4
MD
151 scm_spawn_thread ((scm_t_catch_body) future_loop,
152 SCM_FUTURE (future),
153 (scm_t_catch_handler) future_handler,
154 SCM_FUTURE (future));
155 return future;
156}
157
158static void
159kill_future (SCM future)
160{
161 SCM_FUTURE (future)->die_p = 1;
162 LINK (undead, future);
163}
164
165SCM
166scm_i_make_future (SCM thunk)
167{
168 SCM future;
9de87eea 169 scm_i_scm_pthread_mutex_lock (&future_admin_mutex);
6b468ba4
MD
170 while (1)
171 {
d2e53ed6 172 if (!scm_is_null (old))
6b468ba4 173 UNLINK (old, future);
d2e53ed6 174 else if (!scm_is_null (young))
6b468ba4
MD
175 UNLINK (young, future);
176 else
177 {
9de87eea 178 scm_i_pthread_mutex_unlock (&future_admin_mutex);
6b468ba4
MD
179 return alloc_future (thunk);
180 }
9de87eea 181 if (scm_i_pthread_mutex_trylock (SCM_FUTURE_MUTEX (future)))
6b468ba4
MD
182 kill_future (future);
183 else if (!SCM_FUTURE_ALIVE_P (future))
184 {
9de87eea 185 scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future));
6b468ba4
MD
186 cleanup (SCM_FUTURE (future));
187 }
188 else
189 break;
190 }
191 LINK (futures, future);
9de87eea 192 scm_i_pthread_mutex_unlock (&future_admin_mutex);
6b468ba4
MD
193 SCM_SET_FUTURE_DATA (future, thunk);
194 SCM_SET_FUTURE_STATUS (future, SCM_FUTURE_TASK_ASSIGNED);
9de87eea
MV
195 scm_i_pthread_cond_signal (SCM_FUTURE_COND (future));
196 scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future));
6b468ba4
MD
197 return future;
198}
199
200static SCM
201future_mark (SCM ptr) {
202 return SCM_FUTURE_DATA (ptr);
203}
204
205static int
206future_print (SCM exp, SCM port, scm_print_state *pstate)
207{
208 int writingp = SCM_WRITINGP (pstate);
209 scm_puts ("#<future ", port);
210 SCM_SET_WRITINGP (pstate, 1);
211 scm_iprin1 (SCM_FUTURE_DATA (exp), port, pstate);
212 SCM_SET_WRITINGP (pstate, writingp);
213 scm_putc ('>', port);
214 return !0;
215}
216
217SCM_DEFINE (scm_future_ref, "future-ref", 1, 0, 0,
218 (SCM future),
219 "If the future @var{x} has not been computed yet, compute and\n"
220 "return @var{x}, otherwise just return the previously computed\n"
221 "value.")
222#define FUNC_NAME s_scm_future_ref
223{
224 SCM res;
225 SCM_VALIDATE_FUTURE (1, future);
9de87eea 226 scm_i_scm_pthread_mutex_lock (SCM_FUTURE_MUTEX (future));
6b468ba4
MD
227 if (SCM_FUTURE_STATUS (future) != SCM_FUTURE_COMPUTING)
228 {
229 SCM_SET_FUTURE_STATUS (future, SCM_FUTURE_SIGNAL_ME);
9de87eea
MV
230 scm_i_scm_pthread_cond_wait (SCM_FUTURE_COND (future),
231 SCM_FUTURE_MUTEX (future));
6b468ba4
MD
232 }
233 if (!SCM_FUTURE_ALIVE_P (future))
234 {
9de87eea 235 scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future));
6b468ba4
MD
236 SCM_MISC_ERROR ("requesting result from failed future ~A",
237 scm_list_1 (future));
238 }
239 res = SCM_FUTURE_DATA (future);
9de87eea 240 scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future));
6b468ba4
MD
241 return res;
242}
243#undef FUNC_NAME
244
245static void
246kill_futures (SCM victims)
247{
d2e53ed6 248 while (!scm_is_null (victims))
6b468ba4
MD
249 {
250 SCM future;
251 UNLINK (victims, future);
252 kill_future (future);
9de87eea 253 scm_i_pthread_cond_signal (SCM_FUTURE_COND (future));
6b468ba4
MD
254 }
255}
256
257static void
258cleanup_undead ()
259{
260 SCM next = undead, *nextloc = &undead;
d2e53ed6 261 while (!scm_is_null (next))
6b468ba4 262 {
9de87eea 263 if (scm_i_pthread_mutex_trylock (SCM_FUTURE_MUTEX (next)))
6b468ba4
MD
264 goto next;
265 else if (SCM_FUTURE_ALIVE_P (next))
266 {
9de87eea
MV
267 scm_i_pthread_cond_signal (SCM_FUTURE_COND (next));
268 scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (next));
6b468ba4 269 next:
361d631f 270 SCM_SET_GC_MARK (next);
6b468ba4
MD
271 nextloc = SCM_FUTURE_NEXTLOC (next);
272 next = *nextloc;
273 }
274 else
275 {
276 SCM future;
277 UNLINK (next, future);
9de87eea 278 scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future));
6b468ba4
MD
279 cleanup (SCM_FUTURE (future));
280 *nextloc = next;
281 }
282 }
283}
284
285static void
286mark_futures (SCM futures)
287{
d2e53ed6 288 while (!scm_is_null (futures))
6b468ba4 289 {
6b468ba4
MD
290 SCM_SET_GC_MARK (futures);
291 futures = SCM_FUTURE_NEXT (futures);
292 }
293}
294
295static void *
296scan_futures (void *dummy1, void *dummy2, void *dummy3)
297{
298 SCM next, *nextloc;
299
300 long now = scm_c_get_internal_run_time ();
301 if (now - last_switch > SCM_TIME_UNITS_PER_SECOND)
302 {
303 /* switch out old (> 1 sec), unused futures */
304 kill_futures (old);
305 old = young;
306 young = SCM_EOL;
307 last_switch = now;
308 }
361d631f
MD
309 else
310 mark_futures (young);
6b468ba4
MD
311
312 next = futures;
313 nextloc = &futures;
d2e53ed6 314 while (!scm_is_null (next))
6b468ba4
MD
315 {
316 if (!SCM_GC_MARK_P (next))
317 goto free;
318 keep:
319 nextloc = SCM_FUTURE_NEXTLOC (next);
320 next = *nextloc;
321 }
322 goto exit;
d2e53ed6 323 while (!scm_is_null (next))
6b468ba4
MD
324 {
325 if (SCM_GC_MARK_P (next))
326 {
327 *nextloc = next;
328 goto keep;
329 }
330 free:
331 {
332 SCM future;
333 UNLINK (next, future);
361d631f 334 SCM_SET_GC_MARK (future);
6b468ba4
MD
335 LINK (young, future);
336 }
337 }
338 *nextloc = SCM_EOL;
339 exit:
340 cleanup_undead ();
6b468ba4 341 mark_futures (old);
6b468ba4
MD
342 return 0;
343}
344
9de87eea
MV
345scm_t_bits scm_tc16_future;
346
6b468ba4
MD
347void
348scm_init_futures ()
349{
350 last_switch = scm_c_get_internal_run_time ();
351
352 scm_loc_sys_thread_handler
353 = SCM_VARIABLE_LOC (scm_c_define ("%thread-handler", SCM_BOOL_F));
354
355 scm_tc16_future = scm_make_smob_type ("future", 0);
356 scm_set_smob_mark (scm_tc16_future, future_mark);
357 scm_set_smob_print (scm_tc16_future, future_print);
358
359 scm_c_hook_add (&scm_before_sweep_c_hook, scan_futures, 0, 0);
360#include "libguile/futures.x"
361}
362
363/*
364 Local Variables:
365 c-file-style: "gnu"
366 End:
367*/