SatDump 2.0.0-alpha-520736c72
Loading...
Searching...
No Matches
processing_handler.h
Go to the documentation of this file.
1#pragma once
2
6
7#include "handler.h"
8#include <thread>
9
10namespace satdump
11{
12 namespace handlers
13 {
30 class ProcessingHandler
31 {
32 public:
33 ProcessingHandler() {}
34 ~ProcessingHandler()
35 {
36 if (async_thread.joinable())
37 async_thread.join();
38 }
39
40 protected:
41 bool is_processing = false;
42 std::mutex is_processing_mtx;
43
47 virtual void do_process() = 0;
48
49 std::thread async_thread;
50
51 public:
55 void process()
56 {
57 is_processing_mtx.lock();
58 is_processing = true;
59 is_processing_mtx.unlock();
60 do_process();
61 is_processing_mtx.lock();
62 is_processing = false;
63 is_processing_mtx.unlock();
64 }
65
70 void set_is_processing(bool v)
71 {
72 is_processing_mtx.lock();
73 is_processing = v;
74 is_processing_mtx.unlock();
75 }
76
82 {
83 is_processing_mtx.lock();
84 bool s = is_processing;
85 is_processing_mtx.unlock();
86 return s;
87 }
88
93 {
94 is_processing_mtx.lock();
95 if (is_processing && async_thread.joinable())
96 {
97 printf("ALREADY PROCESSING!!!!\n"); // TODOREWORK
98 is_processing_mtx.unlock();
99 return;
100 }
101 is_processing_mtx.unlock();
102
103 try
104 {
105 async_thread.join();
106 }
107 catch (std::exception &e)
108 {
109 }
110
111 is_processing_mtx.lock();
112 is_processing = true;
113 is_processing_mtx.unlock();
114 auto fun = [this]() { process(); };
115 async_thread = std::thread(fun);
116 }
117 };
118 } // namespace handlers
119} // namespace satdump
bool get_is_processing()
Get the processing status externally.
Definition processing_handler.h:81
void set_is_processing(bool v)
Set the processing status externally, to force waiting before starting a new thread.
Definition processing_handler.h:70
void process()
Performing processing. This is not multi-threaded, intended to call it externally.
Definition processing_handler.h:55
virtual void do_process()=0
Actual processing function to be implemented by the child class.
void asyncProcess()
Perform processing asynchronously, in a thread.
Definition processing_handler.h:92