icl_core_logging/ThreadImplLxrt38.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
23 //----------------------------------------------------------------------
24 #include "ThreadImplLxrt38.h"
25 
27 #include <icl_core/os_lxrt.h>
28 
29 #include "Thread.h"
30 
31 
32 namespace icl_core {
33 namespace logging {
34 
36  : m_thread_id(0),
37  m_thread(thread),
38  m_priority(priority),
39  m_rt_task(NULL),
40  m_rt_start_sync(NULL)
41 {
42 }
43 
45 {
46  // Ensure that the thread is really destroyed.
47  join();
48 
49  if (m_rt_start_sync == NULL)
50  {
51  // Nothing to be done here.
52  }
53  else
54  {
55  rt_sem_delete(m_rt_start_sync);
56  m_rt_start_sync = NULL;
57  }
58 }
59 
61 {
62  if (m_thread_id == 0)
63  {
64  // Nothing to be done here. The thread has already been destroyed.
65  }
66  else
67  {
68  pthread_join_rt(m_thread_id, NULL);
69  m_rt_task = NULL;
70  m_thread_id = 0;
71  }
72 }
73 
75 {
76  m_rt_start_sync = rt_typed_sem_init(size_t(this), 2, CNT_SEM | PRIO_Q);
77  if (m_rt_start_sync == NULL)
78  {
79  // We cannot proceed if this happens!
80  }
81  else
82  {
83  if (pthread_create(&m_thread_id, NULL, ThreadImplLxrt38::runThread, this))
84  {
85  m_thread_id = 0;
86  m_rt_task = NULL;
87 
88  rt_sem_delete(m_rt_start_sync);
89  m_rt_start_sync = NULL;
90  }
91  else
92  {
93  rt_sem_wait_barrier(m_rt_start_sync);
94  }
95  }
96 
97  return m_thread_id != 0;
98 }
99 
101 {
102  ThreadImplLxrt38 *self = static_cast<ThreadImplLxrt38*>(arg);
103 
104  if (self->m_rt_start_sync == NULL)
105  {
106  // Technically, this can never happen because this condition is
107  // already checked in the Start() function. But who knows!
108  PRINTF("ERROR: NULL thread start barrier!\n");
109  }
110  else
111  {
112  self->m_rt_task = rt_task_init(getpid() + pthread_self_rt(), abs(self->m_priority),
113  DEFAULT_STACK_SIZE, 0);
114  if (self->m_rt_task == NULL)
115  {
116  PRINTF("ERROR: Cannot initialize LXRT task %lu!\n", self->m_thread_id);
117  PRINTF(" Probably another thread with the same name already exists.\n");
118 
119  // Let the thread, which started us, continue!
120  rt_sem_wait_barrier(self->m_rt_start_sync);
121  }
122  else
123  {
124  if (self->m_priority < 0)
125  {
126  rt_make_hard_real_time();
127  if (!rt_is_hard_real_time(rt_buddy()))
128  {
129  PRINTF("ERROR: Setting thread %lu to hard real-time failed!\n", self->m_thread_id);
130  }
131  else
132  {
133  // Everything worked as expected, so no message here.
134  }
135  }
136  else
137  {
138  // This is a soft realtime thread, so nothing additional has
139  // to be done here.
140  }
141 
142  rt_sem_wait_barrier(self->m_rt_start_sync);
143 
144  self->m_thread->runThread();
145 
146  // Remark: It does not hurt to call this in a soft realtime
147  // thread, so just skip the hard realtime test.
148  rt_make_soft_real_time();
149  }
150  }
151 
152  return NULL;
153 }
154 
155 }
156 }
TimeSpan abs(const TimeSpan &span)
Definition: TimeSpan.h:222
Contains icl_core::lthread::ThreadImplLxrt38.
Contains a system independet PRINTF macro.
ThreadImplLxrt38(Thread *thread, icl_core::ThreadPriority priority)
Contains icl_core::logging::Thread.
Contains global LXRT functions.
#define DEFAULT_STACK_SIZE
#define PRINTF
int32_t ThreadPriority
Definition: os_thread.h:50


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58