Develop Biology
The language of life
Threaded.cpp
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2021 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "bio/common/Threaded.h"
23#include "bio/common/Cast.h"
24
25//@formatter:off
26#if BIO_CPP_VERSION < 11
27 #ifdef BIO_OS_IS_LINUX
28 #include <unistd.h>
29 #endif
30#else
31 #include <chrono>
32#endif
33//@formatter:on
34
35namespace bio {
36
38 :
39//@formatter:off
40 #if BIO_CPP_VERSION < 11
41 #ifdef BIO_OS_IS_LINUX
42 //use m_thread default ctor
43 #endif
44 m_id(InvalidThreadId()),
45 #else
46 m_thread(NULL),
47 #endif
48 //@formatter:on
49 m_created(false),
50 m_running(false),
51 m_stopRequested(false)
52{
53
54}
55
57{
59}
60
62{
63 LockThread();
64 bool ret = m_running;
66 return ret;
67}
68
70{
71 LockThread();
72 m_stopRequested = true;
74}
75
76/*static*/ void* Threaded::Worker(void* arg)
77{
78 Threaded* threaded = Cast< Threaded* >(arg);
79 BIO_SANITIZE(threaded, ,
80 return NULL);
81
82 threaded->LockThread();
83 threaded->m_running = true;
84 threaded->UnlockThread();
85
86 bool again = true;
87 while (again)
88 {
89 bool again = threaded->Work();
90 threaded->LockThread();
91 again = again && threaded->m_stopRequested;
92 threaded->UnlockThread();
93 }
94
95 threaded->LockThread();
96 threaded->m_running = false;
97 threaded->UnlockThread();
98
99 return NULL;
100}
101
103{
104 //@formatter:off
105 #if BIO_CPP_VERSION < 11
106 return m_id;
107 #else
108 BIO_SANITIZE(m_thread,,return InvalidThreadId())
109 return m_thread->get_id();
110 #endif
111 //@formatter:on
112}
113
115{
116 LockThread();
117 bool isStopped = !m_created && !m_running;
118 UnlockThread();
119 //@formatter:off
120 BIO_SANITIZE(!isStopped, ,return true)
121 //@formatter:on
122
123 //@formatter:off
124 #if BIO_CPP_VERSION < 11
125 BIO_SANITIZE(m_running,,return false)
126 #ifdef BIO_OS_IS_LINUX
127 int result = pthread_create(&m_thread, NULL, Worker, this);
128 #endif
129 m_created = result == 0;
130 #else
131 BIO_SANITIZE(m_thread,,return false)
132 m_thread = new std::thread(&Threaded::Worker, this);
133 m_created = true
134 #endif
135 //@formatter:on
136 return m_created;
137}
138
140{
141 LockThread();
142 bool isStopped = !m_created && !m_running;
143 UnlockThread();
144
145 BIO_SANITIZE(isStopped, ,
146 return true)
147 //@formatter:off
148 #if BIO_CPP_VERSION < 11
149 m_stopRequested = true;
150 #ifdef BIO_OS_IS_LINUX
151 void** threadReturn;
152 int result = pthread_join(m_thread, threadReturn);
153 #endif
154 RequestStop();
155 m_created = false;
156 return result == 0;
157 #else
158 BIO_SANITIZE(m_thread,,return true)
159 RequestStop();
160 m_thread->join();
161 delete m_thread;
162 m_thread = NULL;
163 m_created = false;
164 return true;
165 #endif
166 //@formatter:on
167
168}
169
171{
172 //@formatter:off
173 #if BIO_CPP_VERSION < 11
174 #ifdef BIO_OS_IS_LINUX
175 usleep(us);
176 #endif
177 #else
178 std::this_thread::sleep_for (std::chrono::microseconds(ms));
179 #endif
180 //@formatter:on
181}
182
183} //bio namespace
#define BIO_ASSERT(cond)
Definition: AssertMacros.h:29
#define BIO_CPP_VERSION
#define BIO_SANITIZE(test, success, failure)
void LockThread() const
Definition: ThreadSafe.cpp:84
void UnlockThread() const
Definition: ThreadSafe.cpp:97
virtual void RequestStop()
Definition: Threaded.cpp:69
virtual ThreadId GetThreadId()
Definition: Threaded.cpp:102
static ThreadId InvalidThreadId()
Definition: Threaded.h:65
bool m_created
Definition: Threaded.h:141
virtual ~Threaded()
Definition: Threaded.cpp:56
virtual bool IsRunning()
Definition: Threaded.cpp:61
virtual bool Start()
Definition: Threaded.cpp:114
virtual void Sleep(TimeUS us)
Definition: Threaded.cpp:170
virtual bool Stop()
Definition: Threaded.cpp:139
bool m_running
Definition: Threaded.h:142
pid_t ThreadId
Definition: Threaded.h:57
static void * Worker(void *arg)
Definition: Threaded.cpp:76
ThreadId m_id
Definition: Threaded.h:130
virtual bool Work()
Definition: Threaded.h:89
bool m_stopRequested
Definition: Threaded.h:143
Definition: Cell.h:31
uint32_t TimeUS
Definition: Types.h:74