libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
jthread.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_JTHREAD_HPP
2 #define LIBJMMCG_CORE_JTHREAD_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2019 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "config.h"
24 
25 #include <thread>
26 
27 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace ppd {
28 
29 /// Make sure the kernel-thread is always joined in the dtor.
30 /**
31  \see std::thread
32 */
33 struct jthread final : public std::thread {
34  using base_t=std::thread;
35  using os_traits=thread_os_traits<ppd::platform_api, ppd::heavyweight_threading>;
36  using thread_traits=typename os_traits::thread_traits;
37  using base_t::base_t;
38 
39  ~jthread();
40 
41  void operator=(jthread &&) noexcept(true);
42 
43  void __fastcall kernel_priority(const typename thread_traits::api_params_type::priority_type priority) noexcept(false);
44  void __fastcall kernel_affinity(const typename thread_traits::api_params_type::processor_mask_type &mask) noexcept(false);
45  void __fastcall set_name(char const *name) noexcept(false);
46 
47  bool is_running() const noexcept(true);
48 };
49 
50 inline
51 jthread::~jthread() {
52  if (base_t::joinable()) {
53  base_t::join();
54  }
55 }
56 
57 inline void
58 jthread::operator=(jthread &&t) noexcept(true) {
59  base_t::operator=(std::move(t));
60 }
61 
62 inline void
63 jthread::kernel_priority(const typename thread_traits::api_params_type::priority_type priority) noexcept(false) {
64  thread_traits::set_kernel_priority(native_handle(), priority);
65 }
66 
67 inline void
68 jthread::kernel_affinity(const typename thread_traits::api_params_type::processor_mask_type &mask) noexcept(false) {
69  thread_traits::set_kernel_affinity(native_handle(), mask);
70 }
71 
72 inline void
73 jthread::set_name(char const *name) noexcept(false) {
74  thread_traits::set_name(native_handle(), name);
75 }
76 
77 inline bool
78 jthread::is_running() const noexcept(true) {
79  return base_t::joinable();
80 }
81 
82 } } }
83 
84 #endif