Grok 20.3.2
Codec.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <thread>
21#include <queue>
22#include <mutex>
23#include <condition_variable>
24#include <future>
25#include <atomic>
26#include <exception>
27
28namespace grk
29{
30
31class Codec
32{
33public:
34 explicit Codec(grk::IStream* stream)
35 : compressor_(nullptr), decompressor_(nullptr), stream_(stream), stop_worker_(false),
36 thread_started_(false)
37 {
38 obj.wrapper = new GrkObjectWrapperImpl<Codec>(this);
39 }
40
42 {
44 delete compressor_;
45 delete decompressor_;
46 }
47
48 Codec(const Codec&) = delete;
49 Codec& operator=(const Codec&) = delete;
50
51 static Codec* getImpl(grk_object* codec)
52 {
53 return ((GrkObjectWrapperImpl<Codec>*)codec->wrapper)->getWrappee();
54 }
55
56 std::future<bool> queueDecompressTile(uint16_t tile_index)
57 {
59
60 std::promise<bool> promise;
61 std::future<bool> future = promise.get_future();
62
63 {
64 std::lock_guard<std::mutex> lock(queue_mutex_);
65 task_queue_.push(TileTask{tile_index, std::move(promise)});
66 }
67 queue_cv_.notify_one(); // Notify the worker thread
68
69 return future;
70 }
74 std::unique_ptr<grk::IStream> stream_;
75
76private:
78 {
79 std::lock_guard<std::mutex> lock(thread_start_mutex_);
81 {
82 worker_ = std::thread(&Codec::workerThread, this);
83 thread_started_ = true;
84 }
85 }
86
88 {
89 {
90 std::lock_guard<std::mutex> lock(queue_mutex_);
91 stop_worker_ = true;
92 queue_cv_.notify_one();
93 }
94
95 if(worker_.joinable())
96 {
97 worker_.join();
98 }
99 }
100
102 {
103 while(true)
104 {
105 TileTask task;
106
107 // Wait for tasks or stop signal
108 {
109 std::unique_lock<std::mutex> lock(queue_mutex_);
110 queue_cv_.wait(lock, [this]() { return stop_worker_ || !task_queue_.empty(); });
111
112 if(stop_worker_ && task_queue_.empty())
113 {
114 return; // Exit the thread
115 }
116
117 task = std::move(task_queue_.front());
118 task_queue_.pop();
119 }
120
121 // Process the task
122 try
123 {
124 bool success = decompressor_ ? decompressor_->decompressTile(task.tile_index) : false;
125 task.promise.set_value(success);
126 }
127 catch(const std::exception& e)
128 {
129 task.promise.set_exception(std::current_exception());
130 }
131 }
132 }
133
134 struct TileTask
135 {
136 uint16_t tile_index;
137 std::promise<bool> promise;
138 };
139
140 // Thread and task queue
141 std::thread worker_;
142 std::queue<TileTask> task_queue_;
143 std::mutex queue_mutex_;
144 std::condition_variable queue_cv_;
145 std::atomic<bool> stop_worker_; // Signal to stop the thread
146
147 // Thread lazy initialization
150};
151
152} // namespace grk
void stopWorkerThread()
Definition Codec.h:87
std::future< bool > queueDecompressTile(uint16_t tile_index)
Definition Codec.h:56
void startWorkerThreadIfNeeded()
Definition Codec.h:77
Codec & operator=(const Codec &)=delete
std::mutex thread_start_mutex_
Definition Codec.h:148
Codec(grk::IStream *stream)
Definition Codec.h:34
void workerThread()
Definition Codec.h:101
std::thread worker_
Definition Codec.h:141
std::condition_variable queue_cv_
Definition Codec.h:144
IDecompressor * decompressor_
Definition Codec.h:73
static Codec * getImpl(grk_object *codec)
Definition Codec.h:51
Codec(const Codec &)=delete
~Codec()
Definition Codec.h:41
std::queue< TileTask > task_queue_
Definition Codec.h:142
ICompressor * compressor_
Definition Codec.h:72
std::mutex queue_mutex_
Definition Codec.h:143
std::unique_ptr< grk::IStream > stream_
Definition Codec.h:74
std::atomic< bool > stop_worker_
Definition Codec.h:145
grk_object obj
Definition Codec.h:71
bool thread_started_
Definition Codec.h:149
Definition GrkObjectWrapper.h:31
ResWindow.
Definition CompressedChunkCache.h:36
Definition Codec.h:135
uint16_t tile_index
Definition Codec.h:136
std::promise< bool > promise
Definition Codec.h:137
Definition ICompressor.h:24
Decompress interface.
Definition IDecompressor.h:29
Definition IStream.h:60
Opaque reference-counted object.