ThreadImplLxrt38.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 //----------------------------------------------------------------------
00010 //----------------------------------------------------------------------
00011 #include "ThreadImplLxrt38.h"
00012 
00013 #include <icl_core/internal_raw_debug.h>
00014 #include <icl_core/os_lxrt.h>
00015 
00016 #include "Thread.h"
00017 
00018 
00019 namespace icl_core {
00020 namespace logging {
00021 
00022 ThreadImplLxrt38::ThreadImplLxrt38(Thread *thread, icl_core::ThreadPriority priority)
00023   : m_thread_id(0),
00024     m_thread(thread),
00025     m_priority(priority),
00026     m_rt_task(NULL),
00027     m_rt_start_sync(NULL)
00028 {
00029 }
00030 
00031 ThreadImplLxrt38::~ThreadImplLxrt38()
00032 {
00033   // Ensure that the thread is really destroyed.
00034   join();
00035 
00036   if (m_rt_start_sync == NULL)
00037   {
00038     // Nothing to be done here.
00039   }
00040   else
00041   {
00042     rt_sem_delete(m_rt_start_sync);
00043     m_rt_start_sync = NULL;
00044   }
00045 }
00046 
00047 void ThreadImplLxrt38::join()
00048 {
00049   if (m_thread_id == 0)
00050   {
00051     // Nothing to be done here. The thread has already been destroyed.
00052   }
00053   else
00054   {
00055     pthread_join_rt(m_thread_id, NULL);
00056     m_rt_task = NULL;
00057     m_thread_id = 0;
00058   }
00059 }
00060 
00061 bool ThreadImplLxrt38::start()
00062 {
00063   m_rt_start_sync = rt_typed_sem_init(size_t(this), 2, CNT_SEM | PRIO_Q);
00064   if (m_rt_start_sync == NULL)
00065   {
00066     // We cannot proceed if this happens!
00067   }
00068   else
00069   {
00070     if (pthread_create(&m_thread_id, NULL, ThreadImplLxrt38::runThread, this))
00071     {
00072       m_thread_id = 0;
00073       m_rt_task = NULL;
00074 
00075       rt_sem_delete(m_rt_start_sync);
00076       m_rt_start_sync = NULL;
00077     }
00078     else
00079     {
00080       rt_sem_wait_barrier(m_rt_start_sync);
00081     }
00082   }
00083 
00084   return m_thread_id != 0;
00085 }
00086 
00087 void *ThreadImplLxrt38::runThread(void *arg)
00088 {
00089   ThreadImplLxrt38 *self = static_cast<ThreadImplLxrt38*>(arg);
00090 
00091   if (self->m_rt_start_sync == NULL)
00092   {
00093     // Technically, this can never happen because this condition is
00094     // already checked in the Start() function. But who knows!
00095     PRINTF("ERROR: NULL thread start barrier!\n");
00096   }
00097   else
00098   {
00099     self->m_rt_task = rt_task_init(getpid() + pthread_self_rt(), abs(self->m_priority),
00100                                    DEFAULT_STACK_SIZE, 0);
00101     if (self->m_rt_task == NULL)
00102     {
00103       PRINTF("ERROR: Cannot initialize LXRT task %lu!\n", self->m_thread_id);
00104       PRINTF("       Probably another thread with the same name already exists.\n");
00105 
00106       // Let the thread, which started us, continue!
00107       rt_sem_wait_barrier(self->m_rt_start_sync);
00108     }
00109     else
00110     {
00111       if (self->m_priority < 0)
00112       {
00113         rt_make_hard_real_time();
00114         if (!rt_is_hard_real_time(rt_buddy()))
00115         {
00116           PRINTF("ERROR: Setting thread %lu to hard real-time failed!\n", self->m_thread_id);
00117         }
00118         else
00119         {
00120           // Everything worked as expected, so no message here.
00121         }
00122       }
00123       else
00124       {
00125         // This is a soft realtime thread, so nothing additional has
00126         // to be done here.
00127       }
00128 
00129       rt_sem_wait_barrier(self->m_rt_start_sync);
00130 
00131       self->m_thread->runThread();
00132 
00133       // Remark: It does not hurt to call this in a soft realtime
00134       // thread, so just skip the hard realtime test.
00135       rt_make_soft_real_time();
00136     }
00137   }
00138 
00139   return NULL;
00140 }
00141 
00142 }
00143 }


schunk_svh_driver
Author(s): Georg Heppner
autogenerated on Fri Aug 28 2015 12:59:19