SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
SoDaThread.hxx
Go to the documentation of this file.
1/*
2Copyright (c) 2012,2013,2014,2019, 2025, 2026 Matthew H. Reilly (kb1vc)
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#pragma once
30
31#include "SoDaBase.hxx"
32
33#include <string>
34#include "Debug.hxx"
35#include <thread>
36#include <chrono>
37#include <mutex>
38#include <memory>
39#include <condition_variable>
40
41#include <SoDa/MailBox.hxx>
42
43#include "version.h"
55
56
57namespace SoDa {
58
59 class BaseMBox;
60 class Command;
61
62 class Thread;
63 typedef std::shared_ptr<Thread> ThreadPtr;
64 typedef std::weak_ptr<Thread> ThreadWeakPtr;
65
79 class Thread : public Base, public Debug {
80 protected:
81
97 Thread(const std::string & oname, const std::string & version = std::string(SoDaRadio_VERSION));
98
99 public:
101
109 virtual void subscribeToMailBoxes(const std::vector<MailBoxBasePtr> & mailboxes) = 0;
110 public:
111
112 void operator() () {
113 // we woke up with a "start" or "join" call
114 outerRun();
115 }
116
120 void start() {
121 thread_ptr = std::unique_ptr<std::thread>(new std::thread(&SoDa::Thread::outerRun, this));
122 }
123
128 void join() {
129 thread_ptr->join();
130 }
131
137 virtual void run() {
138 std::cerr << "Thread " << getObjName() << " has no run method\n";
139 }
140
151
155 virtual void execGetCommand(CommandPtr cmd) { (void) cmd; }
156
160 virtual void execSetCommand(CommandPtr cmd) { (void) cmd; }
161
165 virtual void execRepCommand(CommandPtr cmd) { (void) cmd; }
166
170 virtual void shutDown() {
171 return;
172 }
173
174 void sleep_ms(unsigned int milliseconds) {
175 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
176 }
177 void sleep_us(unsigned int microseconds) {
178 std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
179 }
180
187 uint32_t getID();
188
189 protected:
193 std::weak_ptr<Thread> self;
194
195 private:
199 std::unique_ptr<std::thread> thread_ptr;
200
201
210 void outerRun();
211
212 static void sigsegHandler(int sig);
213
215
216 std::string version;
217 };
218
220 public:
221 MissingMailBox(const std::string & mbox_name, BasePtr thrower) :
222 SDR::Exception(SoDa::Format("Mailbox %0 could not be found.").addS(mbox_name).str(),
223 thrower)
224 {
225 }
226 };
227
228}
229
230
The Baseclass for all SoDa objects, and useful commonly used classes.
Base(const std::string &oname)
The constructor – pass a name for the object.
std::string & getObjName()
get the name of this object
Definition SoDaBase.hxx:122
This is a list of all the commands that can "do something" to one or more components in the SoDa radi...
Definition Command.hxx:52
Debug(std::string _unit_name=std::string("UNKNOWN"))
Definition Debug.hxx:41
MissingMailBox(const std::string &mbox_name, BasePtr thrower)
The SoDa::SDR Exception class (to distinguish it from the SoDaLibrary SoDa::Exception class.
Definition SoDaBase.hxx:166
Exception(const std::string &_reason, BasePtr obj=NULL)
The constructor.
Definition SoDaBase.hxx:174
BasePtr thrower
who caused the exception, if anyone?
Definition SoDaBase.hxx:230
The Thread baseclass for all SoDa thread objects.
void registerThread(SoDa::ThreadPtr me)
void join()
more properly "Wait for this thread to exit its run loop".
virtual void execRepCommand(CommandPtr cmd)
optional method that reports status or the result of some action.
Thread(const std::string &oname, const std::string &version=std::string("12.2.0"))
make the thread object.
void sleep_ms(unsigned int milliseconds)
void sleep_us(unsigned int microseconds)
uint32_t getID()
Get the process/thread ID for this thread – helps with figuring out who caused a segfault.
void start()
Execute the thread's run loop.
void outerRun()
the run method that is called by the thread handler.
void hookSigSeg()
std::string version
virtual void run()
Each thread object must define its "run" loop.
std::unique_ptr< std::thread > thread_ptr
This is the actual thread object –.
std::weak_ptr< Thread > self
A pointer to ourself.
virtual void shutDown()
optional method that performs cleanup – may not delete.
virtual void execSetCommand(CommandPtr cmd)
optional method to handle "SET" commands – commands that set internal state in the object.
virtual void subscribeToMailBoxes(const std::vector< MailBoxBasePtr > &mailboxes)=0
the creator of this thread may offer one or more mailboxes to this object.
virtual void execGetCommand(CommandPtr cmd)
optional method to handle "GET" commands – commands that request a response
void execCommand(CommandPtr cmd)
Execute (dispatch) a message removed from the command stream to one of the basic Command handler func...
void operator()()
static void sigsegHandler(int sig)
std::shared_ptr< Thread > ThreadPtr
std::shared_ptr< Command > CommandPtr
Definition Command.hxx:41
std::shared_ptr< Base > BasePtr
Definition SoDaBase.hxx:103
std::weak_ptr< Thread > ThreadWeakPtr