Having spent quite some time debugging issues with a project using this Mutex implementation, I've found that the MutexGuard being Send causes me many issues. I've spent some time reasoning about why this is and I've realised it's down to the compare_exchange_weak having AcqRel ordering.
If a Mutex is acquired and then immediately another task attempts to load the Mutex on the same thread due to Send allowing another await, then the next read could give the Mutex twice due to the store being Release ordering. This is not an issue on another thread, but it is an issue on the same thread.
The solution is to change the ordering to SeqCst or removing the Send trait from the MutexGuard.
Since this is a race condition, I'm unable to create a small repro, but hopefully you understand the issue from my description.
Thanks.
Having spent quite some time debugging issues with a project using this Mutex implementation, I've found that the MutexGuard being Send causes me many issues. I've spent some time reasoning about why this is and I've realised it's down to the compare_exchange_weak having AcqRel ordering.
If a Mutex is acquired and then immediately another task attempts to load the Mutex on the same thread due to Send allowing another await, then the next read could give the Mutex twice due to the store being Release ordering. This is not an issue on another thread, but it is an issue on the same thread.
The solution is to change the ordering to SeqCst or removing the Send trait from the MutexGuard.
Since this is a race condition, I'm unable to create a small repro, but hopefully you understand the issue from my description.
Thanks.