Skip to content

Commit c9af0e6

Browse files
committed
[libc++] counting_semaphore should not be default-constructible.
Neither the current C++2b draft, nor any revision of [p1135], nor libstdc++, claims that `counting_semaphore` should be default-constructible. I think this was just a copy-paste issue somehow. Also, `explicit` was missing from the constructor. Also, `constexpr` remains missing; but that's probably more of a technical limitation, since apparently there are some platforms where we don't (can't??) use the atomic implementation and have to rely on pthreads, which obviously isn't constexpr. Differential Revision: https://reviews.llvm.org/D110042
1 parent 2f6b073 commit c9af0e6

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

libcxx/include/semaphore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class __atomic_semaphore_base
8282

8383
public:
8484
_LIBCPP_INLINE_VISIBILITY
85-
__atomic_semaphore_base(ptrdiff_t __count) : __a(__count)
85+
constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a(__count)
8686
{
8787
}
8888
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
@@ -136,7 +136,7 @@ class __platform_semaphore_base
136136

137137
public:
138138
_LIBCPP_INLINE_VISIBILITY
139-
__platform_semaphore_base(ptrdiff_t __count) :
139+
explicit __platform_semaphore_base(ptrdiff_t __count) :
140140
__semaphore()
141141
{
142142
__libcpp_semaphore_init(&__semaphore, __count);
@@ -190,7 +190,7 @@ public:
190190
}
191191

192192
_LIBCPP_INLINE_VISIBILITY
193-
counting_semaphore(ptrdiff_t __count = 0) : __semaphore(__count) { }
193+
constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore(__count) { }
194194
~counting_semaphore() = default;
195195

196196
counting_semaphore(const counting_semaphore&) = delete;

libcxx/test/std/thread/thread.semaphore/binary.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
#include <semaphore>
1919
#include <chrono>
2020
#include <thread>
21+
#include <type_traits>
2122

2223
#include "make_test_thread.h"
2324
#include "test_macros.h"
2425

26+
static_assert(std::is_same<std::binary_semaphore, std::counting_semaphore<1>>::value, "");
27+
2528
int main(int, char**)
2629
{
2730
std::binary_semaphore s(1);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// UNSUPPORTED: libcpp-has-no-threads
10+
// UNSUPPORTED: c++03, c++11
11+
12+
// <semaphore>
13+
14+
// constexpr explicit counting_semaphore(ptrdiff_t desired);
15+
16+
#include <semaphore>
17+
#include <type_traits>
18+
19+
#include "test_macros.h"
20+
21+
static_assert(!std::is_default_constructible<std::binary_semaphore>::value, "");
22+
static_assert(!std::is_default_constructible<std::counting_semaphore<>>::value, "");
23+
24+
static_assert(!std::is_convertible<int, std::binary_semaphore>::value, "");
25+
static_assert(!std::is_convertible<int, std::counting_semaphore<>>::value, "");
26+
27+
#if 0 // TODO FIXME: the ctor should be constexpr when TEST_STD_VER > 17
28+
constinit std::binary_semaphore bs(1);
29+
constinit std::counting_semaphore cs(1);
30+
#endif

libcxx/test/std/thread/thread.semaphore/max.pass.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
int main(int, char**)
2020
{
21-
static_assert(std::counting_semaphore<>::max() > 0, "");
21+
static_assert(std::counting_semaphore<>::max() >= 1, "");
2222
static_assert(std::counting_semaphore<1>::max() >= 1, "");
23-
static_assert(std::counting_semaphore<std::numeric_limits<int>::max()>::max() >= 1, "");
24-
static_assert(std::counting_semaphore<std::numeric_limits<ptrdiff_t>::max()>::max() >= 1, "");
25-
static_assert(std::counting_semaphore<1>::max() == std::binary_semaphore::max(), "");
23+
static_assert(std::counting_semaphore<std::numeric_limits<int>::max()>::max() >= std::numeric_limits<int>::max(), "");
24+
static_assert(std::counting_semaphore<std::numeric_limits<ptrdiff_t>::max()>::max() == std::numeric_limits<ptrdiff_t>::max(), "");
2625
return 0;
2726
}

0 commit comments

Comments
 (0)