[PATCH v2 1/3] librtemscxx: Add join() and detach() to the thread

chrisj at rtems.org chrisj at rtems.org
Thu Oct 8 23:52:27 UTC 2020


From: Chris Johns <chrisj at rtems.org>

- 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 e90e664dfa..fb408f60c0 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 ba46a8c2a5..8723856ac9 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 11bf3df230..7ef9d6ac30 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, &param),
-                          "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 ()),
-- 
2.24.1



More information about the devel mailing list