[rtems commit] librtemscxx: Add join() and detach() to the thread
Chris Johns
chrisj at rtems.org
Fri Oct 9 05:48:57 UTC 2020
Module: rtems
Branch: master
Commit: 6f2a219f573b05c869c14840625b6c1e86f9d718
Changeset: http://git.rtems.org/rtems/commit/?id=6f2a219f573b05c869c14840625b6c1e86f9d718
Author: Chris Johns <chrisj at rtems.org>
Date: Thu Oct 8 19:12:20 2020 +1100
librtemscxx: Add join() and detach() to the thread
- Do not start threads detached
---
cpukit/include/rtems/thread.hpp | 4 ++++
cpukit/librtemscxx/error.cpp | 9 ++++++---
cpukit/librtemscxx/thread.cpp | 44 ++++++++++++++++++++++++++++++-----------
3 files changed, 43 insertions(+), 14 deletions(-)
diff --git a/cpukit/include/rtems/thread.hpp b/cpukit/include/rtems/thread.hpp
index e90e664..fb408f6 100644
--- a/cpukit/include/rtems/thread.hpp
+++ b/cpukit/include/rtems/thread.hpp
@@ -321,6 +321,10 @@ namespace rtems
bool joinable() const noexcept;
+ void join();
+
+ void detach();
+
/*
* Constrain use. These are not available.
*/
diff --git a/cpukit/librtemscxx/error.cpp b/cpukit/librtemscxx/error.cpp
index ba46a8c..8723856 100644
--- a/cpukit/librtemscxx/error.cpp
+++ b/cpukit/librtemscxx/error.cpp
@@ -56,21 +56,24 @@ namespace rtems
void
runtime_error_check (const rtems_status_code sc)
{
- if (sc != RTEMS_SUCCESSFUL)
+ if (sc != RTEMS_SUCCESSFUL) {
throw runtime_error (sc);
+ }
}
void
runtime_error_check (const rtems_status_code sc, const std::string& what)
{
- if (sc != RTEMS_SUCCESSFUL)
+ if (sc != RTEMS_SUCCESSFUL) {
throw runtime_error (sc, what);
+ }
}
void
runtime_error_check (const rtems_status_code sc, const char* what)
{
- if (sc != RTEMS_SUCCESSFUL)
+ if (sc != RTEMS_SUCCESSFUL) {
throw runtime_error (sc, what);
+ }
}
};
diff --git a/cpukit/librtemscxx/thread.cpp b/cpukit/librtemscxx/thread.cpp
index 11bf3df..7ef9d6a 100644
--- a/cpukit/librtemscxx/thread.cpp
+++ b/cpukit/librtemscxx/thread.cpp
@@ -60,8 +60,9 @@ namespace rtems
void
system_error_check (int ec, const char* what)
{
- if (ec != 0)
+ if (ec != 0) {
throw std::system_error (ec, std::system_category(), what);
+ }
}
attributes::attributes ()
@@ -206,8 +207,9 @@ namespace rtems
if (!scheduler.empty ()) {
char sname[4] = { ' ', ' ', ' ', ' ' };
for (size_t c = 0; c < sizeof (sname); ++c) {
- if (c >= scheduler.length ())
+ if (c >= scheduler.length ()) {
break;
+ }
sname[c] = scheduler[c];
}
rtems_name scheduler_name = rtems_build_name (sname[0],
@@ -285,8 +287,9 @@ namespace rtems
runtime_error_check (::rtems_task_get_scheduler (RTEMS_SELF, &scheduler_id));
#if HAVE_GET_SCHEDULER_NAME
char name[5];
- if (!get_scheduler_name (scheduler_id, &name[0]))
+ if (!get_scheduler_name (scheduler_id, &name[0])) {
system_error_check (ENOENT, "get scheduler name");
+ }
scheduler = name;
#endif
}
@@ -328,8 +331,9 @@ namespace rtems
thread&
thread::operator= (thread&& thread_)
{
- if (joinable ())
+ if (joinable ()) {
std::terminate ();
+ }
swap(thread_);
return *this;
}
@@ -346,6 +350,26 @@ namespace rtems
return !(id_ == id());
}
+ void
+ thread::join()
+ {
+ if (!joinable()) {
+ system_error_check (EINVAL, "join");
+ }
+ system_error_check (::pthread_join (id_.id_, nullptr), "join");
+ id_ = id();
+ }
+
+ void
+ thread::detach()
+ {
+ if (!joinable()) {
+ system_error_check (EINVAL, "detach");
+ }
+ system_error_check (::pthread_detach (id_.id_), "detach");
+ id_ = id();
+ }
+
thread::state_base::~state_base () = default;
void
@@ -358,14 +382,10 @@ namespace rtems
system_error_check (::pthread_attr_init (&pattr),
"attribute init");
- system_error_check (::pthread_attr_setdetachstate (&pattr,
- PTHREAD_CREATE_DETACHED),
- "set detached state");
-
struct sched_param param;
param.sched_priority = attr.get_priority ();
system_error_check (::pthread_attr_setschedparam (&pattr, ¶m),
- "set ");
+ "set sched param");
int spolicy;
switch (attr.get_scheduler_policy ()) {
@@ -385,10 +405,12 @@ namespace rtems
system_error_check (::pthread_attr_setschedpolicy (&pattr, spolicy),
"set scheduler policy");
- if (attr.get_scheduler_attr () == attributes::sched_inherit)
+ if (attr.get_scheduler_attr () == attributes::sched_inherit) {
::pthread_attr_setinheritsched (&pattr, PTHREAD_INHERIT_SCHED);
- else
+ }
+ else {
::pthread_attr_setinheritsched (&pattr, PTHREAD_EXPLICIT_SCHED);
+ }
system_error_check (::pthread_attr_setstacksize(&pattr,
attr.get_stack_size ()),
More information about the vc
mailing list