mozilla/rust — pull request #4885 by cpeterso (2013-02-11 08:59:50+00:00)
Title: Issue #4709 RFC: reseed task_rng

@graydon for issue #4709.

The first change (commit 294b4a9) is straightforward. It adds a new `isaac_reseed()` function and uninlines `isaac_seed()` and `isaac_init()`.

The second change (commit 2ef8a32) is more questionable. It adds a `rust_sched_loop::idle()` function, which is called when `rust_sched_driver::start_main_loop()` has no runnable tasks. `idle()` checks whether `rctx` has drained its entropy pool, i.e. generated more than 256 random `uint32_t`s. If not reseeded, `rctx` will be stirring the same entropy bits to generate new random numbers.

Possible improvements:
* Add a minimum and/or maximum reseed frequency to `idle()`. For example, Schneier and Ferguson's Fortuna PRNG doesn't reseed more often 10 times per second.
* Reseed less frequently than 256 random numbers (isaac's `RANDSIZ`)?
* If Rust's task scheduler supported alarm tasks to be run after a specified timeout, they could be used for periodic cleanup work like reseeding the RNG.

This change also adds a fast path to `rust_sched_loop::schedule_task()` when there is only 1 runnable task to avoid depleting `task_rng`'s precious entropy.