µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
thread-cpp.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28// ============================================================================
29// This file is for internal use in µOS++ and should not be included
30// in applications.
31
32thread::thread (thread&& t) noexcept
33{
34 swap (t);
35}
36
37thread&
39{
40 if (joinable ())
41 {
42 os::trace::printf ("%s() @%p attempt to assign a running thread\n",
43 __func__, this);
44 std::abort (); // in ISO it is std::terminate()
45 }
46 swap (t);
47 return *this;
48}
49
50void
52{
53 if (id_ != id ())
54 {
55 void* args = id_.native_thread_->function_args ();
56 if (args != nullptr && function_object_deleter_ != nullptr)
57 {
58 // Manually delete the function object used to store arguments.
60 }
61
62 // Manually delete the system thread.
63 delete id_.native_thread_;
64 }
65}
66
68{
69 os::trace::printf ("%s() @%p\n", __func__, this);
70 if (joinable ())
71 {
72 os::trace::printf ("%s() @%p attempt to destruct a running thread\n",
73 __func__, this);
74 std::abort (); // in ISO it is std::terminate()
75 }
76
78}
79
80// ------------------------------------------------------------------------
81
82void
83thread::swap (thread& t) noexcept
84{
85 std::swap (id_, t.id_);
86 std::swap (function_object_deleter_, t.function_object_deleter_);
87}
88
89bool
90thread::joinable () const noexcept
91{
92 return !(id_ == thread::id ());
93}
94
95void
97{
98 os::trace::printf ("%s() @%p\n", __func__, this);
99
101
102 id_ = id ();
103 os::trace::printf ("%s() @%p joined\n", __func__, this);
104}
105
106void
108{
109 os::trace::printf ("%s() @%p\n", __func__, this);
110 if (id_ != id ())
111 {
113 }
114
115 // The detached thread will continue to run, but we'll not have
116 // access to it from here, not even to delete it.
117 // TODO: arrange to delete it at exit()?
118
119 id_ = id ();
120 os::trace::printf ("%s() @%p detached\n", __func__, this);
121}
122
123// ==========================================================================
void * function_args(void) const
Get the thread function arguments.
Definition os-thread.h:2352
result_t detach(void)
Detach a thread.
Thread unique id.
native_handle_type native_thread_
Standard thread.
void delete_system_thread(void)
Definition thread-cpp.h:51
bool joinable(void) const noexcept
Definition thread-cpp.h:90
~thread()
Definition thread-cpp.h:67
thread() noexcept=default
void detach(void)
Definition thread-cpp.h:107
thread & operator=(const thread &)=delete
void swap(thread &t) noexcept
Definition thread-cpp.h:83
void join(void)
Definition thread-cpp.h:96
function_object_deleter_t function_object_deleter_
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
void swap(thread &x, thread &y) noexcept