Commit fde4f26f authored by Aaryaman Sagar's avatar Aaryaman Sagar Committed by Facebook Github Bot

Add unique_lock construction utility

Summary:
Until C++17 constructor type deduction becomes a thing, we can use this
instead to mimic unique_lock construction without having to specify the type
of the mutex explicitly.

```
auto lck = folly::make_unique_lock(mutex);

// as opposed to

auto lck = std::unique_lock<MutexType>{mutex};
```

Reviewed By: davidtgoldblatt

Differential Revision: D10386999

fbshipit-source-id: 0780a6d1769597a3888305248bcdf93a84c9f9ee
parent d6ab7bcd
/*
* Copyright 2016-present Facebook, Inc.
*
* 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.
*/
#pragma once
#include <mutex>
namespace folly {
/**
* Given a mutex, return a RAII lock type templated on the type of the mutex
*
* auto lck = folly::make_unique_lock(mutex);
*
* After C++17, this function will no longer be useful because constructor
* type deduction can be used for the same purpose, and is also shorter to
* type. Note that we prepend the function with "make_" for consistency with
* standard library functions like "make_tuple", "make_pair", etc
*
* auto lck = std::unique_lock{mutex};
*
* Till we have C++17 and constructor type deduction this function offers
* the convenience of doing the same
*
* The tail of arguments after the mutex will be forwarded to the constructor
* of std::unique_lock
*/
template <typename Mutex, typename... Args>
std::unique_lock<Mutex> make_unique_lock(Mutex& mutex, Args&&... args) {
return std::unique_lock<Mutex>{mutex, std::forward<Args>(args)...};
}
} // namespace folly
/*
* Copyright 2016-present Facebook, Inc.
*
* 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 <folly/Utility.h>
#include <folly/portability/GTest.h>
#include <type_traits>
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