icl_core_logging/ThreadImplLxrt35.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 "ThreadImplLxrt35.h"
25 
27 #include <icl_core/os_lxrt.h>
28 
29 #include "Thread.h"
30 
31 namespace icl_core {
32 namespace logging {
33 
35  : m_thread_id(0),
36  m_thread(thread),
37  m_priority(priority),
38  m_rt_task(NULL),
39  m_rt_start_sync(NULL)
40 { }
41 
43 {
44  // Ensure that the thread is really destroyed.
45  join();
46 
47  if (m_rt_start_sync == NULL)
48  {
49  // Nothing to be done here.
50  }
51  else
52  {
53  pthread_barrier_destroy_rt(m_rt_start_sync);
54  delete m_rt_start_sync;
55  m_rt_start_sync = NULL;
56  }
57 }
58 
60 {
61  if (m_thread_id == 0)
62  {
63  // Nothing to be done here. The thread has already been destroyed.
64  }
65  else
66  {
67  pthread_join_rt(m_thread_id, NULL);
68  m_rt_task = NULL;
69  m_thread_id = 0;
70  }
71 }
72 
74 {
75  m_rt_start_sync = new (std::nothrow) pthread_barrier_t;
76  if (m_rt_start_sync == NULL)
77  {
78  // We cannot proceed, if this happens!
79  }
80  else
81  {
82  pthread_barrier_init_rt(m_rt_start_sync, NULL, 2);
83 
84  if (pthread_create(&m_thread_id, NULL, ThreadImplLxrt35::runThread, this))
85  {
86  m_thread_id = 0;
87  m_rt_task = NULL;
88 
89  pthread_barrier_destroy_rt(m_rt_start_sync);
90  delete m_rt_start_sync;
91  m_rt_start_sync = NULL;
92  }
93  else
94  {
95  pthread_barrier_wait_rt(m_rt_start_sync);
96  }
97  }
98 
99  return m_thread_id != 0;
100 }
101 
103 {
104  ThreadImplLxrt35 *self = static_cast<ThreadImplLxrt35*>(arg);
105 
106  if (self->m_rt_start_sync == NULL)
107  {
108  // Technically, this can never happen because this condition is already checked
109  // in the Start() function. But who knows!
110  PRINTF("ERROR: NULL thread start barrier!\n");
111  }
112  else
113  {
114  self->m_rt_task = rt_task_init(getpid() + pthread_self_rt(), abs(self->m_priority),
115  DEFAULT_STACK_SIZE, 0);
116  if (self->m_rt_task == NULL)
117  {
118  PRINTF("ERROR: Cannot initialize LXRT task %lu!\n", self->m_thread_id);
119  PRINTF(" Probably another thread with the same name already exists.\n");
120 
121  // Let the thread, which started us, continue!
122  pthread_barrier_wait_rt(self->m_rt_start_sync);
123  }
124  else
125  {
126  if (self->m_priority < 0)
127  {
128  rt_make_hard_real_time();
129  if (!rt_is_hard_real_time(rt_buddy()))
130  {
131  PRINTF("ERROR: Setting thread %lu to hard real-time failed!\n", self->m_thread_id);
132  }
133  else
134  {
135  // Everything worked as expected, so no message here.
136  }
137  }
138  else
139  {
140  // This is a soft realtime thread, so nothing additional has
141  // to be done here.
142  }
143 
144  pthread_barrier_wait_rt(self->m_rt_start_sync);
145 
146  self->m_thread->runThread();
147 
148  // Remark: It does not hurt to call this in a soft realtime
149  // thread, so just skip the hard realtime test.
150  rt_make_soft_real_time();
151  }
152  }
153 
154  return NULL;
155 }
156 
157 }
158 }
TimeSpan abs(const TimeSpan &span)
Definition: TimeSpan.h:222
Contains icl_core::lthread::ThreadImplLxrt35.
Contains a system independet PRINTF macro.
Contains icl_core::logging::Thread.
Contains global LXRT functions.
ThreadImplLxrt35(Thread *thread, icl_core::ThreadPriority priority)
#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