ThreadImplLxrt35.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00023 //----------------------------------------------------------------------
00024 #include "ThreadImplLxrt35.h"
00025 
00026 #include <icl_core/internal_raw_debug.h>
00027 #include <icl_core/os_lxrt.h>
00028 
00029 #include "Thread.h"
00030 
00031 namespace icl_core {
00032 namespace logging {
00033 
00034 ThreadImplLxrt35::ThreadImplLxrt35(Thread *thread, icl_core::ThreadPriority priority)
00035   : m_thread_id(0),
00036     m_thread(thread),
00037     m_priority(priority),
00038     m_rt_task(NULL),
00039     m_rt_start_sync(NULL)
00040 { }
00041 
00042 ThreadImplLxrt35::~ThreadImplLxrt35()
00043 {
00044   // Ensure that the thread is really destroyed.
00045   join();
00046 
00047   if (m_rt_start_sync == NULL)
00048   {
00049     // Nothing to be done here.
00050   }
00051   else
00052   {
00053     pthread_barrier_destroy_rt(m_rt_start_sync);
00054     delete m_rt_start_sync;
00055     m_rt_start_sync = NULL;
00056   }
00057 }
00058 
00059 void ThreadImplLxrt35::join()
00060 {
00061   if (m_thread_id == 0)
00062   {
00063     // Nothing to be done here. The thread has already been destroyed.
00064   }
00065   else
00066   {
00067     pthread_join_rt(m_thread_id, NULL);
00068     m_rt_task = NULL;
00069     m_thread_id = 0;
00070   }
00071 }
00072 
00073 bool ThreadImplLxrt35::start()
00074 {
00075   m_rt_start_sync = new (std::nothrow) pthread_barrier_t;
00076   if (m_rt_start_sync == NULL)
00077   {
00078     // We cannot proceed, if this happens!
00079   }
00080   else
00081   {
00082     pthread_barrier_init_rt(m_rt_start_sync, NULL, 2);
00083 
00084     if (pthread_create(&m_thread_id, NULL, ThreadImplLxrt35::runThread, this))
00085     {
00086       m_thread_id = 0;
00087       m_rt_task = NULL;
00088 
00089       pthread_barrier_destroy_rt(m_rt_start_sync);
00090       delete m_rt_start_sync;
00091       m_rt_start_sync = NULL;
00092     }
00093     else
00094     {
00095       pthread_barrier_wait_rt(m_rt_start_sync);
00096     }
00097   }
00098 
00099   return m_thread_id != 0;
00100 }
00101 
00102 void *ThreadImplLxrt35::runThread(void *arg)
00103 {
00104   ThreadImplLxrt35 *self = static_cast<ThreadImplLxrt35*>(arg);
00105 
00106   if (self->m_rt_start_sync == NULL)
00107   {
00108     // Technically, this can never happen because this condition is already checked
00109     // in the Start() function. But who knows!
00110     PRINTF("ERROR: NULL thread start barrier!\n");
00111   }
00112   else
00113   {
00114     self->m_rt_task = rt_task_init(getpid() + pthread_self_rt(), abs(self->m_priority),
00115                                    DEFAULT_STACK_SIZE, 0);
00116     if (self->m_rt_task == NULL)
00117     {
00118       PRINTF("ERROR: Cannot initialize LXRT task %lu!\n", self->m_thread_id);
00119       PRINTF("       Probably another thread with the same name already exists.\n");
00120 
00121       // Let the thread, which started us, continue!
00122       pthread_barrier_wait_rt(self->m_rt_start_sync);
00123     }
00124     else
00125     {
00126       if (self->m_priority < 0)
00127       {
00128         rt_make_hard_real_time();
00129         if (!rt_is_hard_real_time(rt_buddy()))
00130         {
00131           PRINTF("ERROR: Setting thread %lu to hard real-time failed!\n", self->m_thread_id);
00132         }
00133         else
00134         {
00135           // Everything worked as expected, so no message here.
00136         }
00137       }
00138       else
00139       {
00140         // This is a soft realtime thread, so nothing additional has
00141         // to be done here.
00142       }
00143 
00144       pthread_barrier_wait_rt(self->m_rt_start_sync);
00145 
00146       self->m_thread->runThread();
00147 
00148       // Remark: It does not hurt to call this in a soft realtime
00149       // thread, so just skip the hard realtime test.
00150       rt_make_soft_real_time();
00151     }
00152   }
00153 
00154   return NULL;
00155 }
00156 
00157 }
00158 }


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04