Develop Biology
The language of life
Threaded.h
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#pragma once
23
24#include "ThreadSafe.h"
27#include "Types.h"
29
30//@formatter:off
31#if BIO_CPP_VERSION < 11
32 #ifdef BIO_OS_IS_LINUX
33 #include <pthread.h>
34 #endif
35#else
36 #include <thread>
37#endif
38//@formatter:on
39
40namespace bio {
41
52class Threaded :
53 virtual public ThreadSafe
54{
55public:
56 #if BIO_CPP_VERSION < 11
57 typedef pid_t ThreadId;
58 #else
59 typedef std::thread::id ThreadId;
60 #endif
61
66 {
67 return 0;
68 }
69
73 Threaded();
74
75 //TODO: support move & copy ctors.
76
80 virtual ~Threaded();
81
89 virtual bool Work()
90 {
91 //your code goes here!
92 return false;
93 }
94
98 virtual ThreadId GetThreadId();
99
104 virtual bool Start();
105
110 virtual bool Stop();
111
115 virtual bool IsRunning();
116
121 virtual void Sleep(TimeUS us);
122
123protected:
124
125 //@formatter:off
126 #if BIO_CPP_VERSION < 11
127 #ifdef BIO_OS_IS_LINUX
128 pthread_t m_thread;
129 #endif
131 #else
132 std::thread* m_thread;
133 #endif
134 //@formatter:on
135
139 virtual void RequestStop();
140
142 bool m_running; //written by spawn; read by parent.
143 bool m_stopRequested; //written by parent; read by spawn.
144
149 static void* Worker(void* arg);
150};
151
152} //bio namespace
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