SatDump 2.0.0-alpha-76a915210
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
81 {
82 is_processing_mtx.lock();
83 if (is_processing && async_thread.joinable())
84 {
85 printf("ALREADY PROCESSING!!!!\n"); // TODOREWORK
86 is_processing_mtx.unlock();
87 return;
88 }
89 is_processing_mtx.unlock();
90
91 try
92 {
93 async_thread.join();
94 }
95 catch (std::exception &e)
96 {
97 }
98
99 is_processing_mtx.lock();
100 is_processing = true;
101 is_processing_mtx.unlock();
102 auto fun = [this]() { process(); };
103 async_thread = std::thread(fun);
104 }
105
106 static std::string getID();
107 static std::shared_ptr<Handler> getInstance();
108 };
109 } // namespace handlers
110} // namespace satdump
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:80