Not a full RFC yet, but a start:
- Feature Name: mutex-api
- Start Date: 2015-05-13
- RFC PR:
- Rust Issue:
Currently mutexes in Rust suffer a bit from a less than ideal API caused
by leaking out the internals of the poisoning system. This proposal
changes this to a more user friendly API for the common case.
Mutexes in Rust can be “poisoned” by a task panic. This essentially means
that after a task failed another task will no longer be able to lock the
mutex. This is a useful feature but for the vast majority of cases the
poisoning state is actually not useful and the only reasonable action for
the user is to propagate the failure by panicking the current task as
Right now for instance there is no real way to remove the poison flag
from the mutex which either means that you need to continue using a
poisoned mutex which makes the locking pattern a lot more complex or
The proposal of this RFC is to deprecate the current
methods and to introduce two new ones:
acquire(&self): this replaces the previous
Unlike that however the default behavior is to panic with a reasonable
error message. As poisoned locks are only encountered when a task
already failed it means that task failure is already a considered
try_acquire(&self): same as acquire but does not block. This also
propagates poisoned failures by panicking. This has the added advantage
that code that handles the error case only needs to be considered with
the failed acquisition of the lock and not the poison case.
In addition the old methods would survive with new names:
In addition the
LockResult will gain a method
converts the result into the guard but also releases the internal poison
flag. This allows the recovering of the mutex without having to use a
second mutex to guard the mutex swap.
It deprecates a perfectly fine method name (
Not doing anything.
The names of the new methods are not as nice as the old ones, maybe some
alternatives can be found.
For the pattern where poisoning can be ignored it might be reasonable to
force_acquire method that always returns the guard and ignores the
poison flag. This can be useful