ThreadImplLxrt33.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 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #include "ThreadImplLxrt33.h"
00024 
00025 #include <icl_core/internal_raw_debug.h>
00026 #include <icl_core/os_lxrt.h>
00027 
00028 #include "Thread.h"
00029 #include "Logging.h"
00030 
00031 #define DEFAULT_STACK_SIZE 0x4000
00032 
00033 namespace icl_core {
00034 namespace thread {
00035 
00036 ThreadImplLxrt33::ThreadImplLxrt33(Thread *thread, const icl_core::String& description,
00037                                    icl_core::ThreadPriority priority)
00038   : m_thread_id(0),
00039     m_thread(thread),
00040     m_priority(priority),
00041     m_description(description),
00042     m_rt_task(NULL)
00043 { }
00044 
00045 ThreadImplLxrt33::~ThreadImplLxrt33()
00046 { }
00047 
00048 void ThreadImplLxrt33::cancel()
00049 {
00050   if (m_rt_task != NULL)
00051   {
00052     rt_task_delete(m_rt_task);
00053     m_rt_task = NULL;
00054   }
00055   pthread_cancel_rt(m_thread_id);
00056   m_thread_id = 0;
00057   m_rt_task = NULL;
00058 }
00059 
00060 bool ThreadImplLxrt33::isHardRealtime() const
00061 {
00062   return m_priority < 0;
00063 }
00064 
00065 bool ThreadImplLxrt33::executesHardRealtime() const
00066 {
00067   return os::isThisHRT();
00068 }
00069 
00070 void ThreadImplLxrt33::join()
00071 {
00072   pthread_join_rt(m_thread_id, NULL);
00073   m_thread_id = 0;
00074   m_rt_task = NULL;
00075 }
00076 
00077 icl_core::ThreadPriority ThreadImplLxrt33::priority() const
00078 {
00079   return m_priority;
00080 }
00081 
00082 bool ThreadImplLxrt33::setHardRealtime(bool hard_realtime)
00083 {
00084   if (hard_realtime && !os::isThisHRT())
00085   {
00086     rt_make_hard_real_time();
00087     return os::isThisHRT();
00088   }
00089   else if (!hard_realtime && os::isThisHRT())
00090   {
00091     rt_make_soft_real_time();
00092     return !os::isThisHRT();
00093   }
00094   else
00095   {
00096     return false;
00097   }
00098 }
00099 
00100 bool ThreadImplLxrt33::setPriority(icl_core::ThreadPriority priority)
00101 {
00102   // TODO: Make this work!
00103   /*
00104     if (m_rt_task != NULL) {
00105     int ret = rt_change_prio(m_rt_task, abs(priority));
00106     if (ret == 0) {
00107     m_priority = priority;
00108 
00109     if (priority > 0 && IsHardRealtimeThread()) {
00110     rt_make_soft_real_time();
00111     } else if (priority < 0 && !IsHardRealtimeThread()) {
00112     rt_make_hard_real_time();
00113     }
00114 
00115     return true;
00116     }
00117     }
00118   */
00119 
00120   return false;
00121 }
00122 
00123 bool ThreadImplLxrt33::start()
00124 {
00125   if (pthread_create(&m_thread_id, NULL, ThreadImplLxrt33::runThread, this))
00126   {
00127     m_thread_id = 0;
00128     m_rt_task = NULL;
00129   }
00130 
00131   return m_thread_id != 0;
00132 }
00133 
00134 icl_core::ThreadId ThreadImplLxrt33::threadId() const
00135 {
00136   return ::icl_core::ThreadId(m_thread_id);
00137 }
00138 
00139 void * ThreadImplLxrt33::runThread(void *arg)
00140 {
00141   ThreadImplLxrt33 *self = static_cast<ThreadImplLxrt33*>(arg);
00142 
00143   self->m_rt_task = rt_task_init(getpid() + pthread_self_rt(),
00144                                  abs(self->m_priority), DEFAULT_STACK_SIZE, 0);
00145   if (self->m_rt_task == NULL)
00146   {
00147     PRINTF("ERROR: Cannot initialize LXRT task %lu!\n", self->m_thread_id);
00148     PRINTF("       Probably another thread with the same name already exists.\n");
00149   }
00150   else
00151   {
00152     rt_task_use_fpu(self->m_rt_task, 1);
00153 
00154     if (self->m_priority < 0)
00155     {
00156       rt_make_hard_real_time();
00157       if (!rt_is_hard_real_time(rt_buddy()))
00158       {
00159         PRINTF("ERROR: Setting thread %lu to hard real-time failed!\n", self->m_thread_id);
00160       }
00161     }
00162 
00163     self->m_thread->runThread();
00164 
00165     rt_make_soft_real_time();
00166     rt_task_delete(self->m_rt_task);
00167     self->m_rt_task = NULL;
00168   }
00169 
00170   return NULL;
00171 }
00172 
00173 }
00174 }


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