icl_core_logging/Thread.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 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #include "Thread.h"
24 
25 #include <icl_core/os_time.h>
26 
27 #if defined _SYSTEM_LXRT_
28 # include "ThreadImplLxrt.h"
29 #endif
30 
31 #if defined _SYSTEM_POSIX_
32 # include "ThreadImplPosix.h"
33 #elif defined _SYSTEM_WIN32_
34 # include "ThreadImplWin32.h"
35 #else
36 # error "No thread implementation defined for this platform."
37 #endif
38 
39 namespace icl_core {
40 namespace logging {
41 
43  : m_execute(false),
44  m_finished(true),
45  m_joined(true),
46  m_starting(false),
47  m_impl(0)
48 {
49 #if defined _SYSTEM_LXRT_
50  // Only create an LXRT implementation if the LXRT runtime system
51  // is really available. Otherwise create an ACE or POSIX implementation,
52  // depending on the system configuration.
53  // Remark: This allows us to compile programs with LXRT support but run
54  // them on systems on which no LXRT is installed and to disable LXRT support
55  // at program startup on systems with installed LXRT!
57  {
58  m_impl = new ThreadImplLxrt(this, priority);
59  }
60  else
61  {
62  m_impl = new ThreadImplPosix(this, priority);
63  }
64 
65 #elif defined _SYSTEM_POSIX_
66  m_impl = new ThreadImplPosix(this, priority);
67 
68 #elif defined _SYSTEM_WIN32_
69  m_impl = new ThreadImplWin32(this, priority);
70 
71 #endif
72 }
73 
75 {
76  if (!m_joined)
77  {
78  stop();
79  join();
80  }
81  delete m_impl;
82 }
83 
85 {
86  if (running())
87  {
88  m_impl->join();
89  }
90 
91  m_joined = true;
92 }
93 
95 {
96  // Don't do anything if the thread is already starting or running.
97  if (m_starting || running())
98  {
99  waitStarted();
100 
101  return running();
102  }
103 
104  m_starting = true;
105  m_finished = false;
106 
107  if (!m_joined)
108  {
109  join();
110  }
111 
112  m_joined = false;
113 
114  if (!m_impl->start())
115  {
116  m_finished = true;
117  m_starting = false;
118  m_joined = true;
119 
120  return false;
121  }
122  else
123  {
124  waitStarted();
125 
126  return true;
127  }
128 }
129 
131 {
132  m_execute = true;
133  m_starting = false;
134  m_finished = false;
135 
136  run();
137 
138  m_execute = false;
139  m_finished = true;
140 }
141 
143 {
144  while (m_starting)
145  {
146  // Sleep for 1 microsecond.
148  }
149 }
150 
151 }
152 }
virtual ~Thread()
Deletes the thread. Stops it if it is still running.
int usleep(unsigned long useconds)
Definition: os_time.h:56
Thread(icl_core::ThreadPriority priority=0)
Contains icl_core::logging::Thread.
bool isThisLxrtTask()
Definition: os_lxrt.cpp:150
Contains global functions for time manipulation, encapsulated into the icl_core::os namespace...
virtual void run()=0
int32_t ThreadPriority
Definition: os_thread.h:50
void waitStarted() const
Suspends the calling thread until thread is started.


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