-
Notifications
You must be signed in to change notification settings - Fork 39
Removing need to use Mutex on ScheduledTimer
's slot field
#28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…e won't need to lock/unlock each time to update the timer slots.
…ut running into issues.
# Conflicts: # Cargo.toml
Real quick before looking deeper, is there any measurable performance change by avoiding the mutex? In general, lock-free stuff should only be considered once there's a clear justification for the risk |
I don't have the bench, since the affected functions and structs are all private, and measuring the performance of the timer itself is not quite straightforward, since there could be random delays depending on the timing when it's polled. But I'm open to proposals on how to make a bench for the changes. Also, this PR is not quite the same as "making everything lock-free", here we just tried to eliminate a |
I don't think this PR is worth it. Thanks heaps! |
Removing the need to use Mutex on on
ScheduledTimer
's slot fieldDescription
After analyzing the usage of
ScheduledTimer
's slot field in the library, it is determined that it is possible to remove Mutex protection fromScheduledTimer
's slot field, and replace it with a moregeneric and light-weight
AtomicUsize
struct.A few code changes are made to accommodate the removal of the lock code and using its replacement of the atomic structure.
Motivation and Context
It has been documented as a
TODO
in the library source code that useMutex
for theslot
field is probably an overkill and we should be able to explore an alternative solution for the same purpose. After investigations, it's clear that such goal is achievable, since modifying aScheduleTimer
instance almost always involves acquiring aMutex
lock for theat
field, that effectively fend off all concurrent access or modifications on the instance, hence guarantee the thread safety.In specific, all code that access or update the
slot
field are either called from timer'spoll
method, or from the reset code (i.e. fromTimer
'sadvance_to
method). The former's thread-safety is guaranteed the by locking the entirenode
via theat
-field's lock structure, hence theupdate_or_add
andremove
call are essentially synchronized code; the later is a bit complex, though the intention is to clean up theslot
, field, because the index location it points to has already been vacated from the previouspop
call, hence we shall simply reset the slot index to 0 at this point.How Has This Been Tested?
Types of changes
Checklist: