Valkka  0.17.0
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  * Copyright 2017, 2018 Valkka Security Ltd. and 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 Affero 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 Affero General Public License for more details.
21  *
22  * You should have received a copy of the GNU Affero General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
38 #include "Python.h"
39 #include "framefifo.h"
40 
41 // #define STD_THREAD 1 // keep this commented if you want to adjust processor affinity
42 
43 
50 struct ThreadContext {
51  int someint; // example: just an integer
52 };
53 
54 
61 enum class Signal {
62  none,
63  exit
64 };
65 
66 
75 struct SignalContext {
76  Signal signal;
77  ThreadContext *thread_context;
78 };
79 
80 
81 
90 class Thread { // <pyapi>
91 
92 public: // <pyapi>
99  Thread(const char* name); // don't include into python (this class is abstract)
100 
103  ~Thread(); // <pyapi>
104 
105 
106 private:
114  Thread( const Thread& ); //not implemented anywhere
117  void operator=( const Thread& ); //not implemented anywhere
118 
119 
120 protected: // common variables of all Thread subclasses
121  std::string name;
122  bool has_thread;
123  bool stop_requested;
124  bool thread_joined;
125 
126  std::mutex start_mutex;
127  std::condition_variable start_condition;
128 
129  std::mutex mutex;
130  std::condition_variable condition;
131 
132  std::mutex loop_mutex;
133 
134  std::deque<SignalContext> signal_fifo;
135  bool loop;
136 
137 
138 protected: // threads, processor affinity, etc.
139  int core_id;
140 #ifdef STD_THREAD
141  std::thread internal_thread;
142 #else
143  pthread_attr_t thread_attr;
144  cpu_set_t cpuset;
145  pthread_t internal_thread;
146 #endif
147 
148 public: // not protected, cause we might need to test these separately
150  virtual void run() = 0;
152  virtual void preRun() = 0;
154  virtual void postRun() = 0;
156  virtual void preJoin();
158  virtual void postJoin();
160  virtual void sendSignal(SignalContext signal_ctx);
162  virtual void sendSignalAndWait(SignalContext signal_ctx);
163 
164 protected:
165  void mainRun();
166  void closeThread();
167 #ifdef STD_THREAD
168 #else
169  static void* mainRun_(void *p);
170 #endif
171 
172 
173 public: // *** API *** // <pyapi>
175  void setAffinity(int i); // <pyapi>
176 
178  void startCall(); // <pyapi>
179 
184  virtual void stopCall(); // <pyapi>
185 
189  virtual void requestStopCall(); // <pyapi>
190 
193  virtual void waitStopCall(); // <pyapi>
194 }; // <pyapi>
195 
196 
197 
198 
199 
205 class TestProducerThread : public Thread {
206 
207 public:
208  TestProducerThread(const char* name, FrameFifo* framefifo, int index=0);
209 
210 public:
211  void run();
212  void preRun();
213  void postRun();
214 
215 protected:
217 
218 private:
219  int index;
220 
221 };
222 
229 class TestConsumerThread : public Thread {
230 
231 public:
232  TestConsumerThread(const char* name, FrameFifo* framefifo);
233 
234 public:
235  void run();
236  void preRun();
237  void postRun();
238 
239 protected:
241 
242 };
243 
244 #endif
245 
std::string name
Name of the thread.
Definition: thread.h:121
signal to AVThread or OpenGLThread. Also custom signals to custom Threads
Thread safe system of fifo and a stack.
bool loop
Use this boolean to control if the main loop in Thread:run should exit.
Definition: thread.h:135
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:72
pthread_attr_t thread_attr
Thread attributes, pthread_* way.
Definition: thread.h:143
Encapsulates data sent by the signal.
Definition: thread.h:75
undefined (initial value)
Definition: usbthread.h:143
Signal
List of possible signals for the thread.
Definition: thread.h:61
A demo thread for testing the producer/consumer module for fifos.
Definition: thread.h:229
An example of information context sent to the Thread inside Thread::SignalContext.
Definition: thread.h:50
std::deque< SignalContext > signal_fifo
Signal queue (fifo). Redefine in child classes.
Definition: thread.h:134
std::mutex loop_mutex
Protects thread&#39;s main execution loop (if necessary)
Definition: thread.h:132
A demo thread for testing the producer/consumer module for fifos.
Definition: thread.h:205
FrameFifo * framefifo
Feed frames here.
Definition: thread.h:216
bool has_thread
true if thread has been started
Definition: thread.h:122
std::mutex mutex
Mutex protecting the condition variable and signal queue.
Definition: thread.h:129
A class for multithreading with a signaling system.
Definition: thread.h:90
std::condition_variable start_condition
Notified when the thread has been started.
Definition: thread.h:127
std::condition_variable condition
Condition variable for the signal queue (triggered when all signals processed). Not necessarily used ...
Definition: thread.h:130
FrameFifo * framefifo
Consume frames from here.
Definition: thread.h:240
std::mutex start_mutex
Mutex protecting start_condition.
Definition: thread.h:126