add std::optional backport option for cxx11

pull/67/head
Jeff Becker 6 years ago
parent f8180839db
commit 4e105f3cd5
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

File diff suppressed because it is too large Load Diff

@ -5,7 +5,11 @@
#include <queue_manager.hpp>
#include <atomic>
#if __cplusplus >= 201703L
#include <optional>
#else
#include <tl/optional.hpp>
#endif
#include <tuple>
namespace llarp
@ -30,12 +34,12 @@ namespace llarp
QueueManager m_manager;
std::atomic<std::uint32_t> m_waitingPoppers;
std::atomic< std::uint32_t > m_waitingPoppers;
util::Semaphore m_popSemaphore;
const char
m_popSemaphorePadding[(2u * Alignment) - sizeof(util::Semaphore)];
std::atomic<std::uint32_t> m_waitingPushers;
std::atomic< std::uint32_t > m_waitingPushers;
util::Semaphore m_pushSemaphore;
const char
m_pushSemaphorePadding[(2u * Alignment) - sizeof(util::Semaphore)];
@ -72,8 +76,11 @@ namespace llarp
// Remove an element from the queue. Block until an element is available
Type
popFront();
#if __cplusplus >= 201703L
std::optional< Type >
#else
tl::optional< Type >
#endif
tryPopFront();
// Remove all elements from the queue. Note this is not atomic, and if
@ -260,7 +267,11 @@ namespace llarp
}
template < typename Type >
#if __cplusplus >= 201703L
std::optional< Type >
#else
tl::optional< Type >
#endif
Queue< Type >::tryPopFront()
{
uint32_t generation;
@ -284,9 +295,12 @@ namespace llarp
// - update the queue
// - notify any waiting pushers
QueuePopGuard popGuard(*this, generation, index);
QueuePopGuard< Type > popGuard(*this, generation, index);
#if __cplusplus >= 201703L
return std::optional< Type >(std::move(m_data[index]));
#else
return tl::optional< Type >(std::move(m_data[index]));
#endif
}
template < typename Type >
@ -382,8 +396,7 @@ namespace llarp
m_waitingPoppers.fetch_sub(1, std::memory_order_relaxed);
}
QueuePopGuard popGuard(*this, generation, index);
QueuePopGuard< Type > popGuard(*this, generation, index);
return Type(std::move(m_data[index]));
}

@ -6,6 +6,7 @@ namespace llarp
{
namespace thread
{
#if __cplusplus >= 201703L
// Turn an enum into its underlying value.
template < typename E >
constexpr auto
@ -13,7 +14,14 @@ namespace llarp
{
return static_cast< std::underlying_type_t< E > >(e);
}
#else
template < typename E >
constexpr uint32_t
to_underlying(E e) noexcept
{
return static_cast< uint32_t >(e);
}
#endif
static constexpr uint32_t GENERATION_COUNT_SHIFT = 0x2;
// Max number of generations which can be held in an uint32_t.

@ -163,7 +163,7 @@ struct ExceptionTester
}
};
std::atomic< std::thread::id > ExceptionTester::throwFrom = std::thread::id();
std::atomic< std::thread::id > ExceptionTester::throwFrom = {std::thread::id()};
void
sleepNWait(size_t microseconds, util::Barrier& barrier)
@ -211,7 +211,7 @@ struct MoveTester
MoveTester&
operator=(const MoveTester& rhs) = delete;
explicit MoveTester(MoveTester&& rhs)
MoveTester(MoveTester&& rhs)
: moved(false), moveCounter(rhs.moveCounter), value(rhs.value)
{
rhs.moved = true;
@ -495,10 +495,10 @@ TEST(TestQueue, exceptionSafety)
util::Semaphore semaphore{0};
std::atomic_size_t caught = 0;
std::atomic_size_t caught = {0};
std::thread producer{std::bind(&exceptionProducer, std::ref(queue),
std::ref(semaphore), std::ref(caught))};
std::thread producer(std::bind(&exceptionProducer, std::ref(queue),
std::ref(semaphore), std::ref(caught)));
ExceptionTester::throwFrom = std::this_thread::get_id();
@ -572,8 +572,13 @@ TEST(TestQueue, moveIt)
(void)popped;
ASSERT_EQ(5u, counter);
#if __cplusplus >= 201703L
std::optional< MoveTester >
#else
tl::optional< MoveTester >
#endif
optPopped = queue.tryPopFront();
std::optional< MoveTester > optPopped = queue.tryPopFront();
ASSERT_TRUE(optPopped.has_value());
// Moved twice here to construct the optional.

@ -1,6 +1,9 @@
#include <queue_manager.hpp>
#if __cplusplus >= 201703L
#include <optional>
#else
#include <tl/optional.hpp>
#endif
#include <vector>
#include <gtest/gtest.h>
@ -74,8 +77,11 @@ class IntQueue
return false;
}
}
#if __cplusplus >= 201703L
std::optional< int >
#else
tl::optional< int >
#endif
tryPopFront()
{
uint32_t gen = 0;

@ -370,7 +370,7 @@ TEST_P(TryAdd, noblocking)
util::Barrier startBarrier(d.threads + 1);
util::Barrier stopBarrier(d.threads + 1);
BarrierArgs args{startBarrier, stopBarrier, 0};
BarrierArgs args{startBarrier, stopBarrier, {0}};
auto simpleJob = std::bind(barrierFunction, std::ref(args));
@ -389,7 +389,7 @@ TEST_P(TryAdd, noblocking)
// and that we emptied the queue.
ASSERT_EQ(0u, pool.jobCount());
BasicWorkArgs basicWorkArgs = {0};
BasicWorkArgs basicWorkArgs = {{0}};
auto workJob = std::bind(basicWork, std::ref(basicWorkArgs));
@ -417,7 +417,7 @@ TEST(TestThreadPool, recurseJob)
ThreadPool pool(threads, capacity);
util::Barrier barrier(threads + 1);
std::atomic_size_t counter = 0;
std::atomic_size_t counter{0};
pool.start();

Loading…
Cancel
Save