[libcxx-commits] [PATCH] D91992: [libc++] Add an extension to allow constructing std::thread from native handles
Louis Dionne via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Nov 23 12:44:30 PST 2020
ldionne created this revision.
Herald added subscribers: libcxx-commits, jkorous.
Herald added a project: libc++.
Herald added a reviewer: libc++.
ldionne requested review of this revision.
On some systems, creating threads with the standard std::thread constructor
is not convenient, or downright impossible. For example, some systems require
passing a stack size when creating a thread, but the std::thread constructor
does not allow for that.
This patch adds a non-standard extension to std::thread that makes it
constructible from the native_handle_type. This allows creating a thread
using the underlying system's machinery (e.g. in a factory function),
while retaining the rest of the std::thread API.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D91992
Files:
libcxx/include/thread
libcxx/test/libcxx/thread/thread.threads/thread.thread.class/thread.thread.constr/ctor.native_handle.pass.cpp
Index: libcxx/test/libcxx/thread/thread.threads/thread.thread.class/thread.thread.constr/ctor.native_handle.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/thread/thread.threads/thread.thread.class/thread.thread.constr/ctor.native_handle.pass.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: libcpp-has-no-threads
+
+// <thread>
+
+// This test tests the constructor from a native handle provided as an
+// extension to std::thread.
+
+// class thread
+
+// thread::thread(native_handle_type);
+
+#include <thread>
+#include <cassert>
+#include <cstddef>
+
+#include "test_macros.h"
+
+bool ran = false;
+void* f(void*) { ran = true; return NULL; }
+
+int main(int, char**) {
+ pthread_t native;
+ pthread_create(&native, NULL, f, NULL);
+ std::thread t(native);
+ assert(t.native_handle() == native);
+ t.join();
+ assert(ran);
+
+ return 0;
+}
Index: libcxx/include/thread
===================================================================
--- libcxx/include/thread
+++ libcxx/include/thread
@@ -229,7 +229,8 @@
template <class _Fp, class ..._Args,
class = typename enable_if
<
- !is_same<typename __uncvref<_Fp>::type, thread>::value
+ !is_same<typename __uncvref<_Fp>::type, thread>::value &&
+ !is_same<typename __uncvref<_Fp>::type, native_handle_type>::value
>::type
>
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
@@ -239,6 +240,12 @@
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
explicit thread(_Fp __f);
#endif
+
+ _LIBCPP_INLINE_VISIBILITY
+ explicit thread(native_handle_type __handle)
+ : __t_(__handle)
+ { }
+
~thread();
_LIBCPP_INLINE_VISIBILITY
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91992.307174.patch
Type: text/x-patch
Size: 2194 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201123/8b5b5a7e/attachment-0001.bin>
More information about the libcxx-commits
mailing list