[libc++] Support move construction and assignment in <thread> in C++03

Libc++ provides support for <thread> in C++03 as an extension. Furthermore,
it does not support any compiler that doesn't have rvalue references. It
is hence possible to provide the move constructor and move assignment
operator in C++03.
This commit is contained in:
Louis Dionne 2020-06-03 11:51:59 -04:00
parent 7113271528
commit 62cfa3a0b5
4 changed files with 44 additions and 68 deletions

View File

@ -241,12 +241,19 @@ public:
#endif
~thread();
#ifndef _LIBCPP_CXX03_LANG
_LIBCPP_INLINE_VISIBILITY
thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {
__t.__t_ = _LIBCPP_NULL_THREAD;
}
_LIBCPP_INLINE_VISIBILITY
thread& operator=(thread&& __t) _NOEXCEPT;
#endif // _LIBCPP_CXX03_LANG
thread& operator=(thread&& __t) _NOEXCEPT {
if (!__libcpp_thread_isnull(&__t_))
terminate();
__t_ = __t.__t_;
__t.__t_ = _LIBCPP_NULL_THREAD;
return *this;
}
_LIBCPP_INLINE_VISIBILITY
void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
@ -304,17 +311,6 @@ thread::thread(_Fp&& __f, _Args&&... __args)
__throw_system_error(__ec, "thread constructor failed");
}
inline
thread&
thread::operator=(thread&& __t) _NOEXCEPT
{
if (!__libcpp_thread_isnull(&__t_))
terminate();
__t_ = __t.__t_;
__t.__t_ = _LIBCPP_NULL_THREAD;
return *this;
}
#else // _LIBCPP_CXX03_LANG
template <class _Fp>

View File

@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads, c++03
// UNSUPPORTED: libcpp-has-no-threads
// <thread>
@ -16,6 +16,7 @@
#include <thread>
#include <cassert>
#include <utility>
#include "test_macros.h"
@ -30,12 +31,10 @@ public:
G(const G& g) : alive_(g.alive_) {++n_alive;}
~G() {alive_ = 0; --n_alive;}
void operator()(int i, double j)
void operator()()
{
assert(alive_ == 1);
assert(n_alive >= 1);
assert(i == 5);
assert(j == 5.5);
op_run = true;
}
};
@ -45,22 +44,27 @@ bool G::op_run = false;
int main(int, char**)
{
assert(G::n_alive == 0);
assert(!G::op_run);
{
assert(G::n_alive == 0);
assert(!G::op_run);
{
G g;
std::thread t0(g, 5, 5.5);
assert(G::n_alive == 1);
assert(!G::op_run);
std::thread t0(g);
std::thread::id id = t0.get_id();
std::thread t1;
t1 = std::move(t0);
assert(t1.get_id() == id);
assert(t0.get_id() == std::thread::id());
t1.join();
}
assert(G::n_alive == 0);
assert(G::n_alive == 1);
assert(G::op_run);
}
assert(G::n_alive == 0);
assert(G::op_run);
return 0;
return 0;
}

View File

@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++03
// <thread>
@ -16,38 +15,18 @@
// thread& operator=(thread&& t);
#include <thread>
#include <exception>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <exception>
#include <utility>
#include "test_macros.h"
class G
struct G
{
int alive_;
public:
static int n_alive;
static bool op_run;
G() : alive_(1) {++n_alive;}
G(const G& g) : alive_(g.alive_) {++n_alive;}
~G() {alive_ = 0; --n_alive;}
void operator()(int i, double j)
{
assert(alive_ == 1);
assert(n_alive >= 1);
assert(i == 5);
assert(j == 5.5);
op_run = true;
}
void operator()() { }
};
int G::n_alive = 0;
bool G::op_run = false;
void f1()
{
std::_Exit(0);
@ -58,11 +37,11 @@ int main(int, char**)
std::set_terminate(f1);
{
G g;
std::thread t0(g, 5, 5.5);
std::thread t0(g);
std::thread t1;
t0 = std::move(t1);
assert(false);
}
return 0;
return 0;
}

View File

@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads, c++03
// UNSUPPORTED: libcpp-has-no-threads
// <thread>
@ -15,9 +15,9 @@
// thread(thread&& t);
#include <thread>
#include <new>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <utility>
#include "test_macros.h"
@ -38,15 +38,6 @@ public:
assert(n_alive >= 1);
op_run = true;
}
void operator()(int i, double j)
{
assert(alive_ == 1);
assert(n_alive >= 1);
assert(i == 5);
assert(j == 5.5);
op_run = true;
}
};
int G::n_alive = 0;
@ -54,20 +45,26 @@ bool G::op_run = false;
int main(int, char**)
{
assert(G::n_alive == 0);
assert(!G::op_run);
{
G g;
assert(G::n_alive == 1);
assert(!G::op_run);
std::thread t0(g, 5, 5.5);
std::thread t0(g);
std::thread::id id = t0.get_id();
std::thread t1 = std::move(t0);
assert(t1.get_id() == id);
assert(t0.get_id() == std::thread::id());
t1.join();
assert(G::n_alive == 1);
assert(G::op_run);
}
assert(G::n_alive == 0);
assert(G::op_run);
return 0;
return 0;
}