OpenCPN Partial API docs
Loading...
Searching...
No Matches
chartdb_thread.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2026 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17
24#ifndef __CHARTDBTHREAD_H__
25#define __CHARTDBTHREAD_H__
26
27#include <memory>
28#include <queue>
29#include <mutex>
30#include <condition_variable>
31
32// #include <wx/xml/xml.h>
33
34// #include "chartbase.h"
35// #include "chartbase.h"
36// #include "chartdbs.h"
37// #include "chartimg.h"
39
41
42#if 1
44public:
46 pthread = nullptr;
47 b_thread_safe = true;
48 m_provider_type = 0;
49 m_ticket_type = 0;
50 }
52
53 bool DoJob();
54
55 wxString m_ChartPath;
56 wxString m_ChartPathUTF8;
57 ChartClassDescriptor chart_desc;
58
60 std::shared_ptr<struct ChartTableEntry> m_chart_table_entry;
61
62 bool b_isaborted;
63 bool b_thread_safe;
64 int m_provider_type;
65 int m_ticket_type;
66 wxString provider_class_name;
67};
68#endif
69
70#if 0
71class ChartTableEntryPoolThread : public wxThread {
72public:
73 ChartTableEntryPoolThread(ChartTableEntryJobTicket *ticket, wxEvtHandler *message_target);
74 void *Entry();
75
76 wxEvtHandler *m_pMessageTarget;
78};
79#endif
80
81#if 1
82class ChartTableEntryPoolThread : public wxThread {
83public:
84 ChartTableEntryPoolThread(std::shared_ptr<ChartTableEntryJobTicket> ticket,
85 wxEvtHandler* message_target);
86 void* Entry();
87
88private:
89 wxEvtHandler* m_pMessageTarget = nullptr;
90 std::shared_ptr<ChartTableEntryJobTicket> m_ticket;
91};
92
93#endif
94
95#if 0
96class OCPN_ChartTableEntryThreadEvent : public wxEvent {
97public:
98 OCPN_ChartTableEntryThreadEvent(wxEventType commandType = wxEVT_NULL, int id = 0);
100
101 // accessors
102 void SetTicket(ChartTableEntryJobTicket *ticket) { m_ticket = ticket; }
103 ChartTableEntryJobTicket *GetTicket(void) { return m_ticket; }
104
105 // required for sending with wxPostEvent()
106 wxEvent *Clone() const;
107
108 int type;
109 int nstat;
110 int nstat_max;
111
112private:
113 ChartTableEntryJobTicket *m_ticket;
114};
115#endif
116
117#if 1
118// using TicketPtr = std::shared_ptr<ChartTableEntryJobTicket>;
119
120class OCPN_ChartTableEntryThreadEvent : public wxEvent {
121public:
122 OCPN_ChartTableEntryThreadEvent(wxEventType type) : wxEvent(0, type) {}
123
124 wxEvent* Clone() const override {
125 return new OCPN_ChartTableEntryThreadEvent(*this);
126 }
127
128 void SetTicket(std::shared_ptr<ChartTableEntryJobTicket> t) {
129 m_ticket = std::move(t);
130 }
131 std::shared_ptr<ChartTableEntryJobTicket> GetTicket() const {
132 return m_ticket;
133 }
134
135private:
136 std::shared_ptr<ChartTableEntryJobTicket> m_ticket;
137};
138#endif
139
140// Declare the event type
141wxDECLARE_EVENT(wxEVT_OCPN_CHARTTABLEENTRYTHREAD,
143
145public:
146 JobQueueCTE() {}
147 void Push(std::shared_ptr<ChartTableEntryJobTicket> job) {
148 std::lock_guard<std::mutex> lock(m_mutex);
149 m_queue.push(job);
150 m_cv.notify_one();
151 }
152
153 // Blocks until job available or shutdown requested
154 bool Pop(std::shared_ptr<ChartTableEntryJobTicket>& job) {
155 std::unique_lock<std::mutex> lock(m_mutex);
156 m_cv.wait(lock, [&] { return !m_queue.empty() || m_shutdown; });
157
158 if (m_shutdown && m_queue.empty()) return false;
159
160 job = m_queue.front();
161 m_queue.pop();
162 return true;
163 }
164
165 void Shutdown() {
166 std::lock_guard<std::mutex> lock(m_mutex);
167 m_shutdown = true;
168 m_cv.notify_all();
169 n_workers = 0;
170 }
171
172 void AddWorker() { n_workers++; }
173 int GetWorkerCount() { return n_workers; }
174
175private:
176 std::queue<std::shared_ptr<ChartTableEntryJobTicket>> m_queue;
177 std::mutex m_mutex;
178 std::condition_variable m_cv;
179 bool m_shutdown = false;
180 int n_workers = 0;
181};
182
183class PoolWorkerThread : public wxThread {
184public:
185 PoolWorkerThread(JobQueueCTE& queue, wxEvtHandler* target)
186 : wxThread(wxTHREAD_DETACHED), m_queue(queue), m_target(target) {
187 printf("New thread\n");
188 }
189
190protected:
191 ExitCode Entry() override {
192 std::shared_ptr<ChartTableEntryJobTicket> job;
193
194 while (m_queue.Pop(job)) {
195 if (!job->DoJob()) {
196 job->b_isaborted = true;
197 printf("job aborted\n");
198 }
199
200 // printf("..Queue event\n");
201 auto* evt =
202 new OCPN_ChartTableEntryThreadEvent(wxEVT_OCPN_CHARTTABLEENTRYTHREAD);
203 evt->SetTicket(job);
204 wxQueueEvent(m_target, evt);
205 }
206
207 return 0;
208 }
209
210private:
211 JobQueueCTE& m_queue;
212 wxEvtHandler* m_target;
213};
214
215#endif
Basic chart info storage.