µ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-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13// ============================================================================
14// This file is for internal use in µOS++ and should not be included
15// in applications.
16
17thread::thread (thread&& t) noexcept
18{
19 swap (t);
20}
21
22thread&
24{
25 if (joinable ())
26 {
27 os::trace::printf ("%s() @%p attempt to assign a running thread\n",
28 __func__, this);
29 std::abort (); // in ISO it is std::terminate()
30 }
31 swap (t);
32 return *this;
33}
34
35void
37{
38 if (id_ != id ())
39 {
40 void* args = id_.native_thread_->function_args ();
41 if (args != nullptr && function_object_deleter_ != nullptr)
42 {
43 // Manually delete the function object used to store arguments.
45 }
46
47 // Manually delete the system thread.
48 delete id_.native_thread_;
49 }
50}
51
53{
54 os::trace::printf ("%s() @%p\n", __func__, this);
55 if (joinable ())
56 {
57 os::trace::printf ("%s() @%p attempt to destruct a running thread\n",
58 __func__, this);
59 std::abort (); // in ISO it is std::terminate()
60 }
61
63}
64
65// ------------------------------------------------------------------------
66
67void
68thread::swap (thread& t) noexcept
69{
70 std::swap (id_, t.id_);
71 std::swap (function_object_deleter_, t.function_object_deleter_);
72}
73
74bool
75thread::joinable () const noexcept
76{
77 return !(id_ == thread::id ());
78}
79
80void
82{
83 os::trace::printf ("%s() @%p\n", __func__, this);
84
86
87 id_ = id ();
88 os::trace::printf ("%s() @%p joined\n", __func__, this);
89}
90
91void
93{
94 os::trace::printf ("%s() @%p\n", __func__, this);
95 if (id_ != id ())
96 {
98 }
99
100 // The detached thread will continue to run, but we'll not have
101 // access to it from here, not even to delete it.
102 // TODO: arrange to delete it at exit()?
103
104 id_ = id ();
105 os::trace::printf ("%s() @%p detached\n", __func__, this);
106}
107
108// ==========================================================================
void * function_args(void) const
Get the thread function arguments.
Definition os-thread.h:2358
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:36
bool joinable(void) const noexcept
Definition thread-cpp.h:75
~thread()
Definition thread-cpp.h:52
thread() noexcept=default
void detach(void)
Definition thread-cpp.h:92
thread & operator=(const thread &)=delete
void swap(thread &t) noexcept
Definition thread-cpp.h:68
void join(void)
Definition thread-cpp.h:81
function_object_deleter_t function_object_deleter_
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
void swap(thread &x, thread &y) noexcept