Not a full RFC yet, but a start:
- Feature Name: mutex-api
- Start Date: 2015-05-13
- RFC PR:
- Rust Issue:
Summary
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.
Motivation
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
well.
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
Detailed design
The proposal of this RFC is to deprecate the current lock and try_lock
methods and to introduce two new ones:
-
acquire(&self): this replaces the previous lock().unwrap() pattern.
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
behavior.
-
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:
-
lock -> safe_acquire
-
try_lock -> safe_try_acquire
In addition the LockResult will gain a method into_inner_detox which
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.
Drawbacks
It deprecates a perfectly fine method name (lock).
Alternatives
Not doing anything.
Unresolved questions
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
add a force_acquire method that always returns the guard and ignores the
poison flag. This can be useful