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

Add piecewise_construct constructor to folly::Syncronized

Summary: As title

Reviewed By: yfeldblum

Differential Revision: D7379148

fbshipit-source-id: 6865940adee5899a055e1dac6b23705db2bc8ed7
parent 26262a91
......@@ -30,9 +30,11 @@
#include <folly/Preprocessor.h>
#include <folly/SharedMutex.h>
#include <folly/Traits.h>
#include <folly/Utility.h>
#include <glog/logging.h>
#include <mutex>
#include <type_traits>
#include <utility>
namespace folly {
......@@ -497,6 +499,21 @@ struct Synchronized : public SynchronizedBase<
explicit Synchronized(in_place_t, Args&&... args)
: datum_(std::forward<Args>(args)...) {}
/**
* Lets you construct the synchronized object and also pass construction
* parameters to the underlying mutex if desired
*/
template <typename... DatumArgs, typename... MutexArgs>
Synchronized(
std::piecewise_construct_t,
std::tuple<DatumArgs...> datumArgs,
std::tuple<MutexArgs...> mutexArgs)
: Synchronized{std::piecewise_construct,
std::move(datumArgs),
std::move(mutexArgs),
make_index_sequence<sizeof...(DatumArgs)>{},
make_index_sequence<sizeof...(MutexArgs)>{}} {}
/**
* The canonical assignment operator only assigns the data, NOT the
* mutex. It locks the two objects in ascending order of their
......@@ -739,6 +756,20 @@ struct Synchronized : public SynchronizedBase<
nxMoveCtor)
: datum_(std::move(rhs.datum_)) {}
template <
typename... DatumArgs,
typename... MutexArgs,
std::size_t... IndicesOne,
std::size_t... IndicesTwo>
Synchronized(
std::piecewise_construct_t,
std::tuple<DatumArgs...> datumArgs,
std::tuple<MutexArgs...> mutexArgs,
std::index_sequence<IndicesOne...>,
std::index_sequence<IndicesTwo...>)
: datum_{std::get<IndicesOne>(std::move(datumArgs))...},
mutex_{std::get<IndicesTwo>(std::move(mutexArgs))...} {}
// Synchronized data members
T datum_;
mutable Mutex mutex_;
......
......@@ -333,6 +333,26 @@ class FakeAllPowerfulAssertingMutex {
}
};
class NonDefaultConstructibleMutex {
public:
explicit NonDefaultConstructibleMutex(int valueIn) {
value = valueIn;
}
NonDefaultConstructibleMutex() = delete;
NonDefaultConstructibleMutex(const NonDefaultConstructibleMutex&) = delete;
NonDefaultConstructibleMutex(NonDefaultConstructibleMutex&&) = delete;
NonDefaultConstructibleMutex& operator=(const NonDefaultConstructibleMutex&) =
delete;
NonDefaultConstructibleMutex& operator=(NonDefaultConstructibleMutex&&) =
delete;
static int value;
void lock() {}
void unlock() {}
};
int NonDefaultConstructibleMutex::value{0};
TEST_F(SynchronizedLockTest, TestCopyConstructibleValues) {
struct NonCopyConstructible {
NonCopyConstructible(const NonCopyConstructible&) = delete;
......@@ -514,3 +534,13 @@ TEST_F(SynchronizedLockTest, UpgradableLockingWithULock) {
globalAllPowerfulAssertingMutex.lock_state,
FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
}
TEST_F(SynchronizedLockTest, TestPieceWiseConstruct) {
auto&& synchronized = folly::Synchronized<int, NonDefaultConstructibleMutex>{
std::piecewise_construct,
std::forward_as_tuple(3),
std::forward_as_tuple(1)};
EXPECT_EQ(*synchronized.lock(), 3);
EXPECT_EQ(NonDefaultConstructibleMutex::value, 1);
}
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