Valkka  1.6.1
OpenSource Video Management
thread.h
Go to the documentation of this file.
1 #ifndef THREADS_HEADER_GUARD
2 #define THREADS_HEADER_GUARD
3 /*
4  * thread.h : Base class for multithreading
5  *
6  * (c) Copyright 2017-2024 Sampsa Riikonen
7  *
8  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
9  *
10  * This file is part of the Valkka library.
11  *
12  * Valkka is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
37 #include "Python.h"
38 #include "framefifo.h"
39 
40 // #define STD_THREAD 1 // keep this commented if you want to adjust processor affinity
41 
49 {
50  int someint; // example: just an integer
51 };
52 
59 enum class Signal
60 {
61  none,
62  exit
63 };
64 
74 {
75  Signal signal;
76  ThreadContext *thread_context;
77 };
78 
87 class Thread { // <pyapi>
88 
89 public: // <pyapi>
96  Thread(const char *name); // don't include into python (this class is abstract)
97 
100  ~Thread(); // <pyapi>
101 
102 private:
110  Thread(const Thread &); //not implemented anywhere
113  void operator=(const Thread &); //not implemented anywhere
114 
115 protected: // common variables of all Thread subclasses
116  std::string name;
117  bool has_thread;
118  bool stop_requested;
119  bool thread_joined;
120 
121  std::mutex start_mutex;
122  std::condition_variable start_condition;
123 
124  std::mutex mutex;
125  std::condition_variable condition;
126 
127  std::mutex loop_mutex;
128 
129  std::deque<SignalContext> signal_fifo;
130  bool loop;
131 
132 protected: // threads, processor affinity, etc.
133  int core_id;
134 #ifdef STD_THREAD
135  std::thread internal_thread;
136 #else
137  pthread_attr_t thread_attr;
138  cpu_set_t cpuset;
139  pthread_t internal_thread;
140 #endif
141 
142 public: // not protected, cause we might need to test these separately
144  virtual void run() = 0;
146  virtual void preRun() = 0;
148  virtual void postRun() = 0;
150  virtual void preJoin();
152  virtual void postJoin();
154  virtual void sendSignal(SignalContext signal_ctx);
156  virtual void sendSignalAndWait(SignalContext signal_ctx);
157 
158 protected:
159  void mainRun();
160  void closeThread();
161 #ifdef STD_THREAD
162 #else
163  static void *mainRun_(void *p);
164 #endif
165 
166 public: // *** API *** // <pyapi>
168  void setAffinity(int i); // <pyapi>
169 
171  void startCall(); // <pyapi>
172 
177  virtual void stopCall(); // <pyapi>
178 
182  virtual void requestStopCall(); // <pyapi>
183 
186  virtual void waitStopCall(); // <pyapi>
187 
189  virtual void waitReady(); // <pyapi>
190 
191 }; // <pyapi>
192 
199 {
200 
201 public:
202  TestProducerThread(const char *name, FrameFifo *framefifo, int index = 0);
203 
204 public:
205  void run();
206  void preRun();
207  void postRun();
208 
209 protected:
211 
212 private:
213  int index;
214 };
215 
223 {
224 
225 public:
227 
228 public:
229  void run();
230  void preRun();
231  void postRun();
232 
233 protected:
235 };
236 
237 #endif
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
A demo thread for testing the producer/consumer module for fifos.
Definition: thread.h:223
void preRun()
Called before entering the main execution loop, but after creating the thread.
Definition: thread.cpp:269
void run()
Main execution loop is defined here.
Definition: thread.cpp:271
void postRun()
Called after the main execution loop exits, but before joining the thread.
Definition: thread.cpp:270
FrameFifo * framefifo
Consume frames from here.
Definition: thread.h:234
A demo thread for testing the producer/consumer module for fifos.
Definition: thread.h:199
FrameFifo * framefifo
Feed frames here.
Definition: thread.h:210
void postRun()
Called after the main execution loop exits, but before joining the thread.
Definition: thread.cpp:249
void run()
Main execution loop is defined here.
Definition: thread.cpp:250
void preRun()
Called before entering the main execution loop, but after creating the thread.
Definition: thread.cpp:248
A class for multithreading with a signaling system.
Definition: thread.h:87
virtual void requestStopCall()
API method: stops the thread.
Definition: thread.cpp:200
std::condition_variable condition
Condition variable for the signal queue (triggered when all signals processed). Not necessarily used ...
Definition: thread.h:125
virtual void waitStopCall()
API method: waits until the thread is joined.
Definition: thread.cpp:213
void operator=(const Thread &)
Void copy-constructor: this class is non-copyable.
Thread(const Thread &)
Void copy-constructor: this class is non-copyable.
virtual void sendSignal(SignalContext signal_ctx)
Send a signal to the thread.
Definition: thread.cpp:218
std::string name
Name of the thread.
Definition: thread.h:116
~Thread()
Destructor:not virtual.
Definition: thread.cpp:45
std::mutex loop_mutex
Protects thread's main execution loop (if necessary)
Definition: thread.h:127
std::mutex start_mutex
Mutex protecting start_condition.
Definition: thread.h:121
void setAffinity(int i)
API method for setting the thread affinity.
Definition: thread.cpp:58
virtual void postRun()=0
Called after the main execution loop exits, but before joining the thread.
void closeThread()
Sends exit signal to the thread, calls join. This method blocks until thread has exited....
Definition: thread.cpp:91
virtual void preJoin()
Called before the thread is joined.
Definition: thread.cpp:126
std::mutex mutex
Mutex protecting the condition variable and signal queue.
Definition: thread.h:124
void startCall()
API method: starts the thread.
Definition: thread.cpp:134
pthread_attr_t thread_attr
Thread attributes, pthread_* way.
Definition: thread.h:137
std::condition_variable start_condition
Notified when the thread has been started.
Definition: thread.h:122
Thread(const char *name)
Default constructor.
Definition: thread.cpp:41
virtual void stopCall()
API method: stops the thread.
Definition: thread.cpp:184
bool has_thread
true if thread has been started
Definition: thread.h:117
virtual void run()=0
Main execution loop is defined here.
virtual void waitReady()
Wait until thread has processed all its signals.
Definition: thread.cpp:234
std::deque< SignalContext > signal_fifo
Signal queue (fifo). Redefine in child classes.
Definition: thread.h:129
virtual void preRun()=0
Called before entering the main execution loop, but after creating the thread.
virtual void sendSignalAndWait(SignalContext signal_ctx)
Send a signal to the thread and wait for all signals to be executed.
Definition: thread.cpp:225
bool loop
Use this boolean to control if the main loop in Thread:run should exit.
Definition: thread.h:130
void mainRun()
Does the preRun, run, postRun sequence.
Definition: thread.cpp:68
virtual void postJoin()
Called after the thread has been joined.
Definition: thread.cpp:129
Thread safe system of fifo and a stack.
Signal
List of possible signals for the thread.
Definition: thread.h:60
Encapsulates data sent by the signal.
Definition: thread.h:74
An example of information context sent to the Thread inside Thread::SignalContext.
Definition: thread.h:49
@ none
undefined (initial value)
Definition: usbthread.h:143