Skip to content

implemented a read/write lock utility class in sideboard.lib#46

Merged
EliAndrewC merged 1 commit intoresyncing_with_publicfrom
read_write_locking
Dec 5, 2016
Merged

implemented a read/write lock utility class in sideboard.lib#46
EliAndrewC merged 1 commit intoresyncing_with_publicfrom
read_write_locking

Conversation

@EliAndrewC
Copy link
Copy Markdown
Contributor

This feature has come up as something that would be useful both at my day job and in some of the MAGFest background tasks.

We want a lock with the following characteristics:

  • any number of threads may acquire the lock in "read mode"
  • only one thread may have the lock acquired in "write mode" at a time
  • the locking modes are reentrant, i.e. the same thing can acquire the lock multiple times
  • writes take precedence over reads, e.g. once a writer requests the lock, subsequent readers will block until the writer acquires and releases the lock

Locks are hard to write good unit tests for. I did add several unit tests for this feature, including one which spins up other threads to ensure that the locking works as expected. However, in order to REALLY test this out, I also wrote this script:
https://gist.github.com/EliAndrewC/2b23aa2bfb83b353abe5f64f0646c568

That script runs hundreds of threads at a time acquiring read and write locks. I can see from the output that all of the properties explained above are proven out, e.g. I can see that

  • many reader threads run at a time
  • only one write thread runs at a time
  • once a write thread requests the lock, readers start blocking even before it acquires the lock
  • while the write thread is running, all readers block until it's done
  • only one write thread has the lock at a time
  • as soon as all write threads are finished, all of the readers which were blocking acquire the read lock and go to work

I ran this script for many hours to ensure that there were no unexpected edge cases causing it to jam up. I also ensured that the CPU and memory usage was low to ensure that this implementation is efficient.

@EliAndrewC
Copy link
Copy Markdown
Contributor Author

Note: I branched this off of the resynching_with_public branch since that was easier in terms of having fewer merge conflicts.

@EliAndrewC
Copy link
Copy Markdown
Contributor Author

Gonna merge this in now, because it's just merging into the resyncing_with_public branch and not actually merging into master.

@EliAndrewC EliAndrewC merged commit 1f821c1 into resyncing_with_public Dec 5, 2016
@EliAndrewC EliAndrewC deleted the read_write_locking branch December 5, 2016 15:28
EliAndrewC added a commit that referenced this pull request Mar 8, 2017
implemented a read/write lock utility class in sideboard.lib
@EliAndrewC EliAndrewC mentioned this pull request Mar 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant