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 }
51
52 bool DoJob();
53
54 wxString m_ChartPath;
55 wxString m_ChartPathUTF8;
56 ChartClassDescriptor chart_desc;
57
59 std::shared_ptr<struct ChartTableEntry> m_chart_table_entry;
60
61 bool b_isaborted;
62 bool b_thread_safe;
63 int m_provider_type;
64 wxString provider_class_name;
65};
66#endif
67
68#if 0
69class ChartTableEntryPoolThread : public wxThread {
70public:
71 ChartTableEntryPoolThread(ChartTableEntryJobTicket *ticket, wxEvtHandler *message_target);
72 void *Entry();
73
74 wxEvtHandler *m_pMessageTarget;
76};
77#endif
78
79#if 1
80class ChartTableEntryPoolThread : public wxThread {
81public:
82 ChartTableEntryPoolThread(std::shared_ptr<ChartTableEntryJobTicket> ticket,
83 wxEvtHandler* message_target);
84 void* Entry();
85
86private:
87 wxEvtHandler* m_pMessageTarget = nullptr;
88 std::shared_ptr<ChartTableEntryJobTicket> m_ticket;
89};
90
91#endif
92
93#if 0
94class OCPN_ChartTableEntryThreadEvent : public wxEvent {
95public:
96 OCPN_ChartTableEntryThreadEvent(wxEventType commandType = wxEVT_NULL, int id = 0);
98
99 // accessors
100 void SetTicket(ChartTableEntryJobTicket *ticket) { m_ticket = ticket; }
101 ChartTableEntryJobTicket *GetTicket(void) { return m_ticket; }
102
103 // required for sending with wxPostEvent()
104 wxEvent *Clone() const;
105
106 int type;
107 int nstat;
108 int nstat_max;
109
110private:
111 ChartTableEntryJobTicket *m_ticket;
112};
113#endif
114
115#if 1
116// using TicketPtr = std::shared_ptr<ChartTableEntryJobTicket>;
117
118class OCPN_ChartTableEntryThreadEvent : public wxEvent {
119public:
120 OCPN_ChartTableEntryThreadEvent(wxEventType type) : wxEvent(0, type) {}
121
122 wxEvent* Clone() const override {
123 return new OCPN_ChartTableEntryThreadEvent(*this);
124 }
125
126 void SetTicket(std::shared_ptr<ChartTableEntryJobTicket> t) {
127 m_ticket = std::move(t);
128 }
129 std::shared_ptr<ChartTableEntryJobTicket> GetTicket() const {
130 return m_ticket;
131 }
132
133private:
134 std::shared_ptr<ChartTableEntryJobTicket> m_ticket;
135};
136#endif
137
138// Declare the event type
139wxDECLARE_EVENT(wxEVT_OCPN_CHARTTABLEENTRYTHREAD,
141
143public:
144 JobQueueCTE() {}
145 void Push(std::shared_ptr<ChartTableEntryJobTicket> job) {
146 std::lock_guard<std::mutex> lock(m_mutex);
147 m_queue.push(job);
148 m_cv.notify_one();
149 }
150
151 // Blocks until job available or shutdown requested
152 bool Pop(std::shared_ptr<ChartTableEntryJobTicket>& job) {
153 std::unique_lock<std::mutex> lock(m_mutex);
154 m_cv.wait(lock, [&] { return !m_queue.empty() || m_shutdown; });
155
156 if (m_shutdown && m_queue.empty()) return false;
157
158 job = m_queue.front();
159 m_queue.pop();
160 return true;
161 }
162
163 void Shutdown() {
164 std::lock_guard<std::mutex> lock(m_mutex);
165 m_shutdown = true;
166 m_cv.notify_all();
167 n_workers = 0;
168 }
169
170 void AddWorker() { n_workers++; }
171 int GetWorkerCount() { return n_workers; }
172
173private:
174 std::queue<std::shared_ptr<ChartTableEntryJobTicket>> m_queue;
175 std::mutex m_mutex;
176 std::condition_variable m_cv;
177 bool m_shutdown = false;
178 int n_workers = 0;
179};
180
181class PoolWorkerThread : public wxThread {
182public:
183 PoolWorkerThread(JobQueueCTE& queue, wxEvtHandler* target)
184 : wxThread(wxTHREAD_DETACHED), m_queue(queue), m_target(target) {}
185
186protected:
187 ExitCode Entry() override {
188 std::shared_ptr<ChartTableEntryJobTicket> job;
189
190 while (m_queue.Pop(job)) {
191 if (!job->DoJob()) {
192 job->b_isaborted = true;
193 printf("job aborted\n");
194 }
195
196 // printf("..Queue event\n");
197 auto* evt =
198 new OCPN_ChartTableEntryThreadEvent(wxEVT_OCPN_CHARTTABLEENTRYTHREAD);
199 evt->SetTicket(job);
200 wxQueueEvent(m_target, evt);
201 }
202
203 // printf("thread exit\n");
204 return 0;
205 }
206
207private:
208 JobQueueCTE& m_queue;
209 wxEvtHandler* m_target;
210};
211
212#endif
Basic chart info storage.