futures: Limit the number of nested futures on the same stack.

Fixes <http://bugs.gnu.org/13188>.
Reported by Nala Ginrut <nalaginrut@gmail.com>.

* module/ice-9/futures.scm (%nesting-level): Rename to...
  (%nesting-level): ... this.  Default to 0 instead of #f.  Update
  users.
  (%max-nesting-level): New variable.
  (touch): When FUTURE is queued and (%nesting-level) is above
  %MAX-NESTING-LEVEL, abort to %FUTURE-PROMPT.
* test-suite/tests/future.test ("nested futures")["loop"]: Remove
  `compile' call.
* test-suite/tests/threads.test ("par-map")["long list"]: New test.
* doc/ref/api-scheduling.texi (Futures): Add a paragraph about stack
  consumption.
This commit is contained in:
Ludovic Courtès 2013-03-27 18:03:47 +01:00
commit 8a177d316c
4 changed files with 37 additions and 20 deletions

View file

@ -1037,6 +1037,13 @@ future has completed. This suspend/resume is achieved by capturing the
calling future's continuation, and later reinstating it (@pxref{Prompts,
delimited continuations}).
Note that @code{par-map} above is not tail-recursive. This could lead
to stack overflows when @var{lst} is large compared to
@code{(current-processor-count)}. To address that, @code{touch} uses
the suspend mechanism described above to limit the number of nested
futures executing on the same stack. Thus, the above code should never
run into stack overflows.
@deffn {Scheme Syntax} future exp
Return a future for expression @var{exp}. This is equivalent to: