folly::lock() - a deadlock safe way to lock folly::Synchronized
Summary: `folly::lock()` is a deadlock safe way to acquire write locks on many lockables or `folly::Synchronized` objects ``` lock(folly::wlock(one), folly::rlock(two), folly::wlock(three)); ``` This executes the deadlock avoidance algorithm on a write lock for `one` and `three` and a read lock for `two`. ADL lookup is done for the `lock()` function. It can also work on arbitrary lockables and performs better than both `std::lock()` and acquiring the mutexes in order ``` folly::lock(one, two, three); ``` There is a big performance improvement compared to simply acquiring locks in the same order in the presence of contention. The backoff algorithm tries to adjust to contention and block on the mutex that it thinks is the best fit. Benchmarks look promising ``` ============================================================================ folly/test/SynchronizedBenchmark.cpp relative time/iter iters/s ============================================================================ ThreeThreadsPathologicalFollyLock 3.81us 262.24K ThreeThreadsPathologicalStdLock 5.34us 187.28K ThreeThreadsPathologicalOrdered 6.36us 157.28K ThreeThreadsPathologicalCarefullyOrdered 4.21us 237.29K ---------------------------------------------------------------------------- TwoThreadsTwoMutexesOrdered 260.87ns 3.83M TwoThreadsTwoMutexesSmart 161.28ns 6.20M TwoThreadsTwoMutexesPersistent 226.25ns 4.42M ---------------------------------------------------------------------------- TwoThreadsFourMutexesOrdered 196.01ns 5.10M TwoThreadsFourMutexesSmart 196.73ns 5.08M TwoThreadsFourMutexesPersistent 201.70ns 4.96M ---------------------------------------------------------------------------- TwoThreadsEightMutexesOrdered 195.76ns 5.11M TwoThreadsEightMutexesSmart 187.90ns 5.32M TwoThreadsEightMutexesPersistent 199.21ns 5.02M ---------------------------------------------------------------------------- TwoThreadsSixteenMutexesOrdered 203.91ns 4.90M TwoThreadsSixteenMutexesSmart 196.30ns 5.09M TwoThreadsSixteenMutexesPersistent 230.64ns 4.34M ---------------------------------------------------------------------------- FourThreadsTwoMutexesOrdered 814.98ns 1.23M FourThreadsTwoMutexesSmart 559.79ns 1.79M FourThreadsTwoMutexesPersistent 520.90ns 1.92M ---------------------------------------------------------------------------- FourThreadsFourMutexesOrdered 456.04ns 2.19M FourThreadsFourMutexesSmart 391.69ns 2.55M FourThreadsFourMutexesPersistent 414.56ns 2.41M ---------------------------------------------------------------------------- FourThreadsEightMutexesOrdered 392.20ns 2.55M FourThreadsEightMutexesSmart 277.89ns 3.60M FourThreadsEightMutexesPersistent 301.98ns 3.31M ---------------------------------------------------------------------------- FourThreadsSixteenMutexesOrdered 356.36ns 2.81M FourThreadsSixteenMutexesSmart 291.40ns 3.43M FourThreadsSixteenMutexesPersistent 292.23ns 3.42M ---------------------------------------------------------------------------- EightThreadsTwoMutexesOrdered 1.58us 634.85K EightThreadsTwoMutexesSmart 1.58us 634.85K EightThreadsTwoMutexesPersistent 1.56us 639.93K ---------------------------------------------------------------------------- EightThreadsFourMutexesOrdered 1.33us 753.45K EightThreadsFourMutexesSmart 794.36ns 936.34K EightThreadsFourMutexesPersistent 831.68ns 1.21M ---------------------------------------------------------------------------- EightThreadsEightMutexesOrdered 791.52ns 1.26M EightThreadsEightMutexesSmart 548.05ns 1.51M EightThreadsEightMutexesPersistent 563.14ns 2.78M ---------------------------------------------------------------------------- EightThreadsSixteenMutexesOrdered 785.40ns 2.11M EightThreadsSixteenMutexesSmart 541.27ns 1.60M EightThreadsSixteenMutexesPersistent 673.49ns 1.79M ---------------------------------------------------------------------------- SixteenThreadsTwoMutexesOrdered 1.98us 505.83K SixteenThreadsTwoMutexesSmart 1.85us 541.06K SixteenThreadsTwoMutexesPersistent 3.13us 319.53K ---------------------------------------------------------------------------- SixteenThreadsFourMutexesOrdered 2.46us 407.07K SixteenThreadsFourMutexesSmart 1.68us 594.47K SixteenThreadsFourMutexesPersistent 1.62us 617.22K ---------------------------------------------------------------------------- SixteenThreadsEightMutexesOrdered 1.67us 597.45K SixteenThreadsEightMutexesSmart 1.62us 616.83K SixteenThreadsEightMutexesPersistent 1.57us 637.50K ---------------------------------------------------------------------------- SixteenThreadsSixteenMutexesOrdered 1.20us 829.93K SixteenThreadsSixteenMutexesSmart 1.32us 757.03K SixteenThreadsSixteenMutexesPersistent 1.38us 726.75K ============================================================================ ``` Reviewed By: djwatson Differential Revision: D6673876 fbshipit-source-id: b57fdafb8fc2a42c74dc0279c051cc62976a4e07
Showing
This diff is collapsed.
Please register or sign in to comment