Commit 5bef498a authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

move make_unique_lock to folly/synchronization/Lock.h

Summary: Since all the other lock utilities are there.

Differential Revision: D31523986

fbshipit-source-id: 22a071c50e51518b061980d7429d1a28755dbe40
parent 1e7f9f89
......@@ -502,6 +502,33 @@ class shared_lock_guard
using base::base;
};
// make_unique_lock
//
// Returns a unique_lock constructed with the given arguments. Deduces the
// mutex type.
template <typename Mutex, typename... A>
FOLLY_NODISCARD unique_lock<Mutex> make_unique_lock(Mutex& mutex, A&&... a) {
return unique_lock<Mutex>{mutex, static_cast<A&&>(a)...};
}
// make_shared_lock
//
// Returns a shared_lock constructed with the given arguments. Deduces the
// mutex type.
template <typename Mutex, typename... A>
FOLLY_NODISCARD shared_lock<Mutex> make_shared_lock(Mutex& mutex, A&&... a) {
return shared_lock<Mutex>{mutex, static_cast<A&&>(a)...};
}
// make_upgrade_lock
//
// Returns an upgrade_lock constructed with the given arguments. Deduces the
// mutex type.
template <typename Mutex, typename... A>
FOLLY_NODISCARD upgrade_lock<Mutex> make_upgrade_lock(Mutex& mutex, A&&... a) {
return upgrade_lock<Mutex>{mutex, static_cast<A&&>(a)...};
}
namespace detail {
template <typename L>
......
......@@ -31,7 +31,6 @@
#include <folly/lang/Aligned.h>
#include <folly/synchronization/DistributedMutex.h>
#include <folly/synchronization/SmallLocks.h>
#include <folly/synchronization/Utility.h>
/* "Work cycle" is just an additional nop loop iteration.
* A smaller number of work cyles will result in more contention,
......@@ -132,7 +131,7 @@ class FlatCombiningMutexCaching
template <typename Mutex, typename CriticalSection>
auto lock_and(Mutex& mutex, std::size_t, CriticalSection func) {
auto lck = folly::make_unique_lock(mutex);
auto lck = std::unique_lock{mutex};
return func();
}
template <typename F>
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/synchronization/Utility.h>
#include <type_traits>
#include <folly/Utility.h>
#include <folly/portability/GTest.h>
class UtilityTest : public testing::Test {};
namespace {
class MockMutex {
public:
void lock() {
EXPECT_FALSE(locked_);
locked_ = true;
}
void unlock() {
EXPECT_TRUE(locked_);
locked_ = false;
}
bool try_lock() {
if (!locked_) {
locked_ = true;
return true;
}
return false;
}
template <typename Duration>
bool try_lock_for(const Duration&) {
return try_lock();
}
template <typename TimePoint>
bool try_lock_until(const TimePoint&) {
return try_lock();
}
void lock_shared() { lock(); }
void unlock_shared() { unlock(); }
bool locked_{false};
};
} // namespace
TEST_F(UtilityTest, TestMakeUniqueLock) {
auto&& mutex = MockMutex{};
{
auto lck = folly::make_unique_lock(mutex);
EXPECT_TRUE(mutex.locked_);
EXPECT_TRUE(lck);
}
EXPECT_FALSE(mutex.locked_);
}
TEST_F(UtilityTest, MakeUniqueLockDeferLock) {
auto&& mutex = MockMutex{};
{
auto lck = folly::make_unique_lock(mutex, std::defer_lock);
EXPECT_FALSE(mutex.locked_);
EXPECT_FALSE(lck);
}
EXPECT_FALSE(mutex.locked_);
}
TEST_F(UtilityTest, MakeUniqueLockAdoptLock) {
auto&& mutex = MockMutex{};
mutex.lock();
{
auto lck = folly::make_unique_lock(mutex, std::adopt_lock);
EXPECT_TRUE(mutex.locked_);
EXPECT_TRUE(lck);
}
EXPECT_FALSE(mutex.locked_);
}
TEST_F(UtilityTest, MakeUniqueLockTryToLock) {
auto&& mutex = MockMutex{};
{
mutex.lock();
auto lck = folly::make_unique_lock(mutex, std::try_to_lock);
EXPECT_TRUE(mutex.locked_);
EXPECT_FALSE(lck);
}
EXPECT_TRUE(mutex.locked_);
mutex.unlock();
{
auto lck = folly::make_unique_lock(mutex, std::try_to_lock);
EXPECT_TRUE(mutex.locked_);
EXPECT_TRUE(lck);
}
EXPECT_FALSE(mutex.locked_);
}
TEST_F(UtilityTest, MakeUniqueLockTimedLock) {
auto&& mutex = MockMutex{};
{
auto lck = folly::make_unique_lock(mutex, std::chrono::seconds{1});
EXPECT_TRUE(lck);
}
EXPECT_FALSE(mutex.locked_);
{
auto lck = folly::make_unique_lock(
mutex, std::chrono::steady_clock::now() + std::chrono::seconds{1});
EXPECT_TRUE(lck);
}
EXPECT_FALSE(mutex.locked_);
}
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