Based on a patch by Nala Ginrut <nalaginrut@gmail.com>,
with suggestions from Mark H. Weaver.
* module/ice-9/control.scm (call-with-escape-continuation, call/ec): New
procedures.
(let-escape-continuation, let/ec): New macros.
* module/ice-9/futures.scm (let/ec): Remove.
* test-suite/tests/control.test ("escape-only continuations")["call/ec",
"let/ec"]: New tests.
* doc/ref/api-control.texi (Prompt Primitives): Document `call/ec',
`let/ec', and their long names.
* module/ice-9/futures.scm (%futures-waiting, %within-future?,
%future-prompt): New variables.
(let/ec): New macro.
(process-future!): Run FUTURE's thunk in a prompt; capture FUTURE's
continuation when it aborts, and add it to %FUTURES-WAITING. Set
%WITHIN-FUTURE? in the dynamic extent of the call FUTURE's thunk.
(process-futures): Move loop body to...
(process-one-future): ... here. New procedure.
(notify-completion): New procedure.
(touch)[work, loop]: New procedures.
When %WITHIN-FUTURE? and FUTURE is started, abort; if not
%WITHIN-FUTURE, call `work' while waiting.
When FUTURE is queued, call `work' too.
* test-suite/tests/future.test ("nested futures"): New tests.
* module/ice-9/futures.scm (<future>)[completion]: New field.
[done?]: Rename to...
[state]: ... this. Change `set-future-done?!' to
`set-future-state!', and `future-done?' to `future-state'.
(make-future): Initialize the `completion' field to 'queued.
(with-mutex): New macro.
(process-future!): Remove `set-future-done?!' call.
(process-futures): Check `future-state'. Unlock FUTURE's mutex before
processing it. Broadcast FUTURE's `completion' cond. var. when done.
(touch): Likewise.
* module/ice-9/futures.scm (%create-workers!): Use 'with-mutex' in case
an exception is thrown. Within the critical section, check to make
sure the worker pool hasn't already been created by another thread.
Reported by David Pirotte.
* module/ice-9/futures.scm (process-futures): Wait on %FUTURES-AVAILABLE
only when %FUTURES is empty.
The problem was obvious when running (begin (use-modules (ice-9
threads)) (par-map 1+ (iota 400000))) : eventually, only the main
thread would do the work, while the others would remain idle, waiting
on %FUTURES-AVAILABLE.
* module/ice-9/futures.scm (%workers, %create-workers!)
(create-workers!): Define a mechanism to spawn off the future threads
only when the first future is created.
(make-future): Call create-workers! here.
* module/ice-9/futures.scm (%futures): Change from a list to a queue.
(register-future!, process-futures, touch): Adjust accordingly.
(unregister-future!): Remove.
* module/ice-9/futures.scm (process-future!): Use `call-with-values'
when invoking `(future-thunk future)'.
* test-suite/tests/future.test ("futures")["multiple values"]: New test.