From 14838d1f6f514d42841b953b24a0cfb60a993a7c Mon Sep 17 00:00:00 2001 From: Charles-Francois Natali Date: Wed, 24 Nov 2021 13:43:30 +0000 Subject: [PATCH] Make is_lock_free constexpr. This makes it possible to use it in `static_assert`, which is better than a runtime error. --- blockingconcurrentqueue.h | 2 +- concurrentqueue.h | 2 +- tests/unittests/unittests.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/blockingconcurrentqueue.h b/blockingconcurrentqueue.h index 66579b6c..205a4db7 100644 --- a/blockingconcurrentqueue.h +++ b/blockingconcurrentqueue.h @@ -544,7 +544,7 @@ class BlockingConcurrentQueue // Returns true if the underlying atomic variables used by // the queue are lock-free (they should be on most platforms). // Thread-safe. - static bool is_lock_free() + static constexpr bool is_lock_free() { return ConcurrentQueue::is_lock_free(); } diff --git a/concurrentqueue.h b/concurrentqueue.h index 609ca4ab..75073bd2 100644 --- a/concurrentqueue.h +++ b/concurrentqueue.h @@ -1314,7 +1314,7 @@ class ConcurrentQueue // Returns true if the underlying atomic variables used by // the queue are lock-free (they should be on most platforms). // Thread-safe. - static bool is_lock_free() + static constexpr bool is_lock_free() { return details::static_is_lock_free::value == 2 && diff --git a/tests/unittests/unittests.cpp b/tests/unittests/unittests.cpp index 029f7428..c5d20adc 100644 --- a/tests/unittests/unittests.cpp +++ b/tests/unittests/unittests.cpp @@ -3643,9 +3643,9 @@ class ConcurrentQueueTests : public TestClass // is_lock_free() { - bool lockFree = ConcurrentQueue::is_lock_free(); + constexpr bool lockFree = ConcurrentQueue::is_lock_free(); #if defined(__amd64__) || defined(_M_X64) || defined(__x86_64__) || defined(_M_IX86) || defined(__i386__) || defined(_M_PPC) || defined(__powerpc__) - ASSERT_OR_FAIL(lockFree); + static_assert(lockFree, "is_lock_free should be true"); #else (void)lockFree; #endif