Commit 663d9c42 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Cut folly/LockTraitsBoost.h

Summary:
[Folly] Cut `folly/LockTraitsBoost.h`. Also, discourage specializing `LockTraits`.

For ODR sanity, the better mechanism is to wrap mutex types with non-standard interfaces in mutex wrapper types with standard interfaces.

Reviewed By: Orvid

Differential Revision: D15426235

fbshipit-source-id: f54aee8c47951f6214f5b23fce7f8f6c29c8b5c2
parent 81f4e08f
...@@ -479,10 +479,6 @@ class SynchronizedBase<Subclass, detail::MutexLevel::UNIQUE> { ...@@ -479,10 +479,6 @@ class SynchronizedBase<Subclass, detail::MutexLevel::UNIQUE> {
* Supported mutexes that work by default include std::mutex, * Supported mutexes that work by default include std::mutex,
* std::recursive_mutex, std::timed_mutex, std::recursive_timed_mutex, * std::recursive_mutex, std::timed_mutex, std::recursive_timed_mutex,
* folly::SharedMutex, folly::RWSpinLock, and folly::SpinLock. * folly::SharedMutex, folly::RWSpinLock, and folly::SpinLock.
* Include LockTraitsBoost.h to get additional LockTraits specializations to
* support the following boost mutex types: boost::mutex,
* boost::recursive_mutex, boost::shared_mutex, boost::timed_mutex, and
* boost::recursive_timed_mutex.
*/ */
template <class T, class Mutex = SharedMutex> template <class T, class Mutex = SharedMutex>
struct Synchronized : public SynchronizedBase< struct Synchronized : public SynchronizedBase<
......
...@@ -153,15 +153,12 @@ mutex type: `Synchronized<T, Mutex>`. ...@@ -153,15 +153,12 @@ mutex type: `Synchronized<T, Mutex>`.
If not specified, the mutex type defaults to `folly::SharedMutex`. However, any If not specified, the mutex type defaults to `folly::SharedMutex`. However, any
mutex type supported by `folly::LockTraits` can be used instead. mutex type supported by `folly::LockTraits` can be used instead.
`folly::LockTraits` can be specialized to support other custom mutex `folly::LockTraits` can be specialized to support other custom mutex
types that it does not know about out of the box. See types that it does not know about out of the box.
`folly/LockTraitsBoost.h` for an example of how to support additional mutex
types.
`Synchronized` provides slightly different APIs when instantiated with a `Synchronized` provides slightly different APIs when instantiated with a
shared mutex type or an upgrade mutex type then with a plain exclusive mutex. shared mutex type or an upgrade mutex type then with a plain exclusive mutex.
If instantiated with either of the two mutex types above (either through If instantiated with either of the two mutex types above through having a
having a member called lock_shared() or specializing `LockTraits` as in member called lock_shared(), the `Synchronized` object has corresponding
`folly/LockTraitsBoost.h`) the `Synchronized` object has corresponding
`wlock`, `rlock` or `ulock` methods to acquire different lock types. When `wlock`, `rlock` or `ulock` methods to acquire different lock types. When
using a shared or upgrade mutex type, these APIs ensure that callers make an using a shared or upgrade mutex type, these APIs ensure that callers make an
explicit choice to acquire a shared, exclusive or upgrade lock and that explicit choice to acquire a shared, exclusive or upgrade lock and that
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
#include <folly/LockTraits.h> #include <folly/LockTraits.h>
#include <folly/LockTraitsBoost.h>
#include <mutex> #include <mutex>
...@@ -207,33 +206,6 @@ TEST(LockTraits, RWSpinLock) { ...@@ -207,33 +206,6 @@ TEST(LockTraits, RWSpinLock) {
traits::unlock_shared(mutex); traits::unlock_shared(mutex);
} }
TEST(LockTraits, boost_mutex) {
using traits = LockTraits<boost::mutex>;
static_assert(!traits::is_timed, "boost::mutex is not a timed lock");
static_assert(!traits::is_shared, "boost::mutex is not a shared lock");
static_assert(!traits::is_upgrade, "boost::mutex is not an upgradable lock");
boost::mutex mutex;
traits::lock(mutex);
traits::unlock(mutex);
}
TEST(LockTraits, boost_recursive_mutex) {
using traits = LockTraits<boost::recursive_mutex>;
static_assert(
!traits::is_timed, "boost::recursive_mutex is not a timed lock");
static_assert(
!traits::is_shared, "boost::recursive_mutex is not a shared lock");
static_assert(
!traits::is_upgrade, "boost::recursive_mutex is not an upgradable lock");
boost::recursive_mutex mutex;
traits::lock(mutex);
traits::lock(mutex);
traits::unlock(mutex);
traits::unlock(mutex);
}
#if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
TEST(LockTraits, timed_mutex) { TEST(LockTraits, timed_mutex) {
using traits = LockTraits<std::timed_mutex>; using traits = LockTraits<std::timed_mutex>;
...@@ -267,34 +239,6 @@ TEST(LockTraits, recursive_timed_mutex) { ...@@ -267,34 +239,6 @@ TEST(LockTraits, recursive_timed_mutex) {
traits::unlock(mutex); traits::unlock(mutex);
traits::unlock(mutex); traits::unlock(mutex);
} }
TEST(LockTraits, boost_shared_mutex) {
using traits = LockTraits<boost::shared_mutex>;
static_assert(traits::is_timed, "boost::shared_mutex is a timed lock");
static_assert(traits::is_shared, "boost::shared_mutex is a shared lock");
static_assert(
traits::is_upgrade, "boost::shared_mutex is an upgradable lock");
boost::shared_mutex mutex;
traits::lock(mutex);
auto gotLock = traits::try_lock_for(mutex, std::chrono::milliseconds(1));
EXPECT_FALSE(gotLock) << "should not have been able to acquire the "
<< "shared_mutex a second time";
gotLock = traits::try_lock_shared_for(mutex, std::chrono::milliseconds(1));
EXPECT_FALSE(gotLock) << "should not have been able to acquire the "
<< "shared_mutex a second time";
traits::unlock(mutex);
traits::lock_shared(mutex);
gotLock = traits::try_lock_for(mutex, std::chrono::milliseconds(1));
EXPECT_FALSE(gotLock) << "should not have been able to acquire the "
<< "shared_mutex a second time";
gotLock = traits::try_lock_shared_for(mutex, std::chrono::milliseconds(10));
EXPECT_TRUE(gotLock) << "should have been able to acquire the "
<< "shared_mutex a second time in shared mode";
traits::unlock_shared(mutex);
traits::unlock_shared(mutex);
}
#endif // FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES #endif // FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
/** /**
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include <folly/Synchronized.h> #include <folly/Synchronized.h>
#include <folly/Function.h> #include <folly/Function.h>
#include <folly/LockTraitsBoost.h>
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/SharedMutex.h> #include <folly/SharedMutex.h>
...@@ -43,18 +42,11 @@ using SynchronizedTestTypes = testing::Types< ...@@ -43,18 +42,11 @@ using SynchronizedTestTypes = testing::Types<
#if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
std::timed_mutex, std::timed_mutex,
std::recursive_timed_mutex, std::recursive_timed_mutex,
#endif
boost::mutex,
boost::recursive_mutex,
#if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
boost::timed_mutex,
boost::recursive_timed_mutex,
#endif #endif
#ifdef RW_SPINLOCK_USE_X86_INTRINSIC_ #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
folly::RWTicketSpinLock32, folly::RWTicketSpinLock32,
folly::RWTicketSpinLock64, folly::RWTicketSpinLock64,
#endif #endif
boost::shared_mutex,
folly::SpinLock>; folly::SpinLock>;
TYPED_TEST_CASE(SynchronizedTest, SynchronizedTestTypes); TYPED_TEST_CASE(SynchronizedTest, SynchronizedTestTypes);
...@@ -113,9 +105,6 @@ using SynchronizedTimedTestTypes = testing::Types< ...@@ -113,9 +105,6 @@ using SynchronizedTimedTestTypes = testing::Types<
#if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
std::timed_mutex, std::timed_mutex,
std::recursive_timed_mutex, std::recursive_timed_mutex,
boost::timed_mutex,
boost::recursive_timed_mutex,
boost::shared_mutex,
#endif #endif
#ifdef RW_SPINLOCK_USE_X86_INTRINSIC_ #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
folly::RWTicketSpinLock32, folly::RWTicketSpinLock32,
...@@ -137,9 +126,6 @@ template <class Mutex> ...@@ -137,9 +126,6 @@ template <class Mutex>
class SynchronizedTimedWithConstTest : public testing::Test {}; class SynchronizedTimedWithConstTest : public testing::Test {};
using SynchronizedTimedWithConstTestTypes = testing::Types< using SynchronizedTimedWithConstTestTypes = testing::Types<
#if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
boost::shared_mutex,
#endif
#ifdef RW_SPINLOCK_USE_X86_INTRINSIC_ #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
folly::RWTicketSpinLock32, folly::RWTicketSpinLock32,
folly::RWTicketSpinLock64, folly::RWTicketSpinLock64,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment