1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* windows.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#define SQFS_BUILDING_DLL
#include "internal.h"
struct compress_worker_t {
sqfs_data_writer_t *shared;
sqfs_compressor_t *cmp;
HANDLE thread;
sqfs_u8 scratch[];
};
static DWORD WINAPI worker_proc(LPVOID arg)
{
compress_worker_t *worker = arg;
sqfs_data_writer_t *shared = worker->shared;
sqfs_block_t *blk = NULL;
int status = 0;
for (;;) {
EnterCriticalSection(&shared->mtx);
if (blk != NULL) {
data_writer_store_done(shared, blk, status);
WakeConditionVariable(&shared->done_cond);
}
while (shared->queue == NULL && shared->status == 0) {
SleepConditionVariableCS(&shared->queue_cond,
&shared->mtx, INFINITE);
}
blk = data_writer_next_work_item(shared);
LeaveCriticalSection(&shared->mtx);
if (blk == NULL)
break;
status = data_writer_do_block(blk, worker->cmp,
worker->scratch,
shared->max_block_size);
}
return 0;
}
sqfs_data_writer_t *sqfs_data_writer_create(size_t max_block_size,
sqfs_compressor_t *cmp,
unsigned int num_workers,
size_t max_backlog,
size_t devblksz,
sqfs_file_t *file)
{
sqfs_data_writer_t *proc;
unsigned int i;
if (num_workers < 1)
num_workers = 1;
proc = alloc_flex(sizeof(*proc),
sizeof(proc->workers[0]), num_workers);
if (proc == NULL)
return NULL;
InitializeCriticalSection(&proc->mtx);
InitializeConditionVariable(&proc->queue_cond);
InitializeConditionVariable(&proc->done_cond);
if (data_writer_init(proc, max_block_size, cmp, num_workers,
max_backlog, devblksz, file)) {
goto fail;
}
for (i = 0; i < num_workers; ++i) {
proc->workers[i] = alloc_flex(sizeof(compress_worker_t),
1, max_block_size);
if (proc->workers[i] == NULL)
goto fail;
proc->workers[i]->shared = proc;
proc->workers[i]->cmp = cmp->create_copy(cmp);
if (proc->workers[i]->cmp == NULL)
goto fail;
proc->workers[i]->thread = CreateThread(NULL, 0, worker_proc,
proc->workers[i], 0, 0);
if (proc->workers[i]->thread == NULL)
goto fail;
}
return proc;
fail:
sqfs_data_writer_destroy(proc);
return NULL;
}
void sqfs_data_writer_destroy(sqfs_data_writer_t *proc)
{
unsigned int i;
EnterCriticalSection(&proc->mtx);
proc->status = -1;
WakeAllConditionVariable(&proc->queue_cond);
LeaveCriticalSection(&proc->mtx);
for (i = 0; i < proc->num_workers; ++i) {
if (proc->workers[i] == NULL)
continue;
if (proc->workers[i]->thread != NULL) {
WaitForSingleObject(proc->workers[i]->thread, INFINITE);
CloseHandle(proc->workers[i]->thread);
}
if (proc->workers[i]->cmp != NULL)
proc->workers[i]->cmp->destroy(proc->workers[i]->cmp);
free(proc->workers[i]);
}
DeleteCriticalSection(&proc->mtx);
data_writer_cleanup(proc);
}
int append_to_work_queue(sqfs_data_writer_t *proc, sqfs_block_t *block,
bool signal_threads)
{
int status;
EnterCriticalSection(&proc->mtx);
status = proc->status;
if (status != 0) {
free(block);
LeaveCriticalSection(&proc->mtx);
return status;
}
if (block != NULL) {
if (proc->queue_last == NULL) {
proc->queue = proc->queue_last = block;
} else {
proc->queue_last->next = block;
proc->queue_last = block;
}
block->sequence_number = proc->enqueue_id++;
block->next = NULL;
proc->backlog += 1;
}
if (signal_threads)
WakeAllConditionVariable(&proc->queue_cond);
LeaveCriticalSection(&proc->mtx);
return 0;
}
static sqfs_block_t *try_dequeue(sqfs_data_writer_t *proc)
{
sqfs_block_t *queue, *it, *prev;
it = proc->done;
prev = NULL;
while (it != NULL && it->sequence_number == proc->dequeue_id) {
prev = it;
it = it->next;
proc->dequeue_id += 1;
}
if (prev == NULL) {
queue = NULL;
} else {
queue = proc->done;
prev->next = NULL;
proc->done = it;
}
return queue;
}
static sqfs_block_t *queue_merge(sqfs_block_t *lhs, sqfs_block_t *rhs)
{
sqfs_block_t *it, *head = NULL, **next_ptr = &head;
while (lhs != NULL && rhs != NULL) {
if (lhs->sequence_number <= rhs->sequence_number) {
it = lhs;
lhs = lhs->next;
} else {
it = rhs;
rhs = rhs->next;
}
*next_ptr = it;
next_ptr = &it->next;
}
it = (lhs != NULL ? lhs : rhs);
*next_ptr = it;
return head;
}
static int process_done_queue(sqfs_data_writer_t *proc, sqfs_block_t *queue)
{
sqfs_block_t *it, *block = NULL;
int status = 0;
while (queue != NULL && status == 0) {
it = queue;
queue = it->next;
if (it->flags & SQFS_BLK_IS_FRAGMENT) {
block = NULL;
status = process_completed_fragment(proc, it, &block);
if (block != NULL && status == 0) {
EnterCriticalSection(&proc->mtx);
proc->dequeue_id = it->sequence_number;
block->sequence_number = it->sequence_number;
if (proc->queue == NULL) {
proc->queue = block;
proc->queue_last = block;
} else {
block->next = proc->queue;
proc->queue = block;
}
proc->backlog += 1;
proc->done = queue_merge(queue, proc->done);
WakeAllConditionVariable(&proc->queue_cond);
LeaveCriticalSection(&proc->mtx);
queue = NULL;
} else {
free(block);
}
} else {
status = process_completed_block(proc, it);
}
free(it);
}
free_blk_list(queue);
return status;
}
int test_and_set_status(sqfs_data_writer_t *proc, int status)
{
EnterCriticalSection(&proc->mtx);
if (proc->status == 0) {
proc->status = status;
} else {
status = proc->status;
}
WakeAllConditionVariable(&proc->queue_cond);
LeaveCriticalSection(&proc->mtx);
return status;
}
int wait_completed(sqfs_data_writer_t *proc)
{
sqfs_block_t *queue;
int status;
EnterCriticalSection(&proc->mtx);
for (;;) {
queue = try_dequeue(proc);
status = proc->status;
if (queue != NULL || status != 0)
break;
SleepConditionVariableCS(&proc->done_cond, &proc->mtx,
INFINITE);
}
LeaveCriticalSection(&proc->mtx);
if (status != 0) {
free_blk_list(queue);
return status;
}
status = process_done_queue(proc, queue);
return status ? test_and_set_status(proc, status) : status;
}
|