]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/port/win/win_thread.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
12 #ifndef _POSIX_THREADS
16 #include <type_traits>
18 #include "rocksdb/rocksdb_namespace.h"
20 namespace ROCKSDB_NAMESPACE
{
23 // This class is a replacement for std::thread
24 // 2 reasons we do not like std::thread:
25 // -- is that it dynamically allocates its internals that are automatically
26 // freed when the thread terminates and not on the destruction of the
27 // object. This makes it difficult to control the source of memory
29 // - This implements Pimpl so we can easily replace the guts of the
30 // object in our private version if necessary.
34 std::shared_ptr
<Data
> data_
;
37 void Init(std::function
<void()>&&);
40 using native_handle_type
= void*;
42 // Construct with no thread
45 // Template constructor
47 // This templated constructor accomplishes several things
49 // - Allows the class as whole to be not a template
51 // - take "universal" references to support both _lvalues and _rvalues
53 // - because this constructor is a catchall case in many respects it
54 // may prevent us from using both the default __ctor, the move __ctor.
55 // Also it may circumvent copy __ctor deletion. To work around this
56 // we make sure this one has at least one argument and eliminate
57 // it from the overload selection when WindowsThread is the first
60 // - construct with Fx(Ax...) with a variable number of types/arguments.
62 // - Gathers together the callable object with its arguments and constructs
63 // a single callable entity
65 // - Makes use of std::function to convert it to a specification-template
66 // dependent type that both checks the signature conformance to ensure
67 // that all of the necessary arguments are provided and allows pimpl
69 template <class Fn
, class... Args
,
70 class = typename
std::enable_if
<!std::is_same
<
71 typename
std::decay
<Fn
>::type
, WindowsThread
>::value
>::type
>
72 explicit WindowsThread(Fn
&& fx
, Args
&&... ax
) : WindowsThread() {
73 // Use binder to create a single callable entity
74 auto binder
= std::bind(std::forward
<Fn
>(fx
), std::forward
<Args
>(ax
)...);
75 // Use std::function to take advantage of the type erasure
76 // so we can still hide implementation within pimpl
77 // This also makes sure that the binder signature is compliant
78 std::function
<void()> target
= binder
;
80 Init(std::move(target
));
85 WindowsThread(const WindowsThread
&) = delete;
87 WindowsThread
& operator=(const WindowsThread
&) = delete;
89 WindowsThread(WindowsThread
&&) noexcept
;
91 WindowsThread
& operator=(WindowsThread
&&) noexcept
;
93 bool joinable() const;
95 unsigned int get_id() const { return th_id_
; }
97 native_handle_type
native_handle() const;
99 static unsigned hardware_concurrency();
105 void swap(WindowsThread
&);
108 } // namespace ROCKSDB_NAMESPACE
111 inline void swap(ROCKSDB_NAMESPACE::port::WindowsThread
& th1
,
112 ROCKSDB_NAMESPACE::port::WindowsThread
& th2
) {
117 #endif // !_POSIX_THREADS