Skip to content

Commit

Permalink
Pass full argument to CreateThread
Browse files Browse the repository at this point in the history
  • Loading branch information
plzombie committed Jan 19, 2023
1 parent 6d1673f commit 2caa515
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions include/depress_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ typedef struct {
size_t tasks_num;
size_t tasks_max;
size_t tasks_processed;
size_t tasks_next_to_process;
// Threads
HANDLE *threads;
depress_thread_arg_type *thread_args;
Expand Down
1 change: 1 addition & 0 deletions include/depress_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef struct {
depress_task_type *tasks;
depress_djvulibre_paths_type *djvulibre_paths;
size_t tasks_num;
size_t *tasks_next_to_process;
int thread_id;
int threads_num;
HANDLE global_error_event;
Expand Down
28 changes: 15 additions & 13 deletions src/depress_converter.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ bool depressConvertPage(depress_flags_type flags, wchar_t *inputfile, wchar_t *t
wchar_t *arg0 = 0, *arg_options, *arg_temp = 0, *djvulibre_path;
unsigned char *buffer = 0;
bool result = false;
const size_t arg0_size = 3*32768+1536; // 2*3(braces)+3(spaces)+1024(options)<1536

// Checking for modes that needed separate complex functions
if(flags.type == DEPRESS_PAGE_TYPE_LAYERED)
return depressConvertLayeredPage(flags, inputfile, tempfile, outputfile, djvulibre_paths);

arg0 = malloc((3*32768+1536+1024+80)*sizeof(wchar_t)); // 2*3(braces)+3(spaces)+1024(options)<1566
arg0 = malloc((arg0_size+1024+80)*sizeof(wchar_t)); //

if(!arg0)
goto EXIT;
else {
arg_options = arg0 + 3 * 32768 + 1536;
arg_options = arg0 + arg0_size;
arg_temp = arg_options + 1024;
}

Expand Down Expand Up @@ -117,7 +118,7 @@ bool depressConvertPage(depress_flags_type flags, wchar_t *inputfile, wchar_t *t
wcscat(arg_options, arg_temp);
}

swprintf(arg0, 32770, L"\"%ls\" %ls \"%ls\" \"%ls\"", djvulibre_path, arg_options, tempfile, outputfile);
swprintf(arg0, arg0_size, L"\"%ls\" %ls \"%ls\" \"%ls\"", djvulibre_path, arg_options, tempfile, outputfile);
} else {
int quality;

Expand All @@ -135,7 +136,7 @@ bool depressConvertPage(depress_flags_type flags, wchar_t *inputfile, wchar_t *t
wcscat(arg_options, arg_temp);
}

swprintf(arg0, 32770, L"\"%ls\" %ls \"%ls\" \"%ls\"", djvulibre_path, arg_options, tempfile, outputfile);
swprintf(arg0, arg0_size, L"\"%ls\" %ls \"%ls\" \"%ls\"", djvulibre_path, arg_options, tempfile, outputfile);
}

if(depressSpawn(djvulibre_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;
Expand Down Expand Up @@ -166,16 +167,17 @@ bool depressConvertLayeredPage(const depress_flags_type flags, wchar_t *inputfil
size_t outputfile_length = 0;
unsigned char *buffer = 0, *buffer_mask = 0, *buffer_bg = 0, *buffer_fg = 0;
bool result = false;
const size_t arg0_size = 5*32768+1536; // 2*3(braces)+3(spaces)+1024(options)<1536

outputfile_length = wcslen(outputfile);
if(outputfile_length > (32768-5-1)) goto EXIT;

arg0 = malloc((5*32768+1536+1024+80+3*32768)*sizeof(wchar_t)); // 2*3(braces)+3(spaces)+1024(options)<1566
arg0 = malloc((arg0_size+1024+80+3*32768)*sizeof(wchar_t));

if(!arg0)
goto EXIT;
else {
arg_options = arg0 + 5 * 32768 + 1536;
arg_options = arg0 + arg0_size;
arg_temp = arg_options + 1024;
arg_sjbz = arg_temp + 80;
arg_fg44 = arg_sjbz + 32768;
Expand Down Expand Up @@ -255,9 +257,9 @@ bool depressConvertLayeredPage(const depress_flags_type flags, wchar_t *inputfil
free(buffer_bg); buffer_bg = 0;
fclose(f_temp); f_temp = 0;
// Convert background
swprintf(arg0, 32770, L"\"%ls\" -slice %d,%d,%d -mask \"%ls\" \"%ls\" \"%ls\"", djvulibre_paths->c44_path, quality-25, quality-15, quality, arg_sjbz, tempfile, outputfile);
swprintf(arg0, arg0_size, L"\"%ls\" -slice %d,%d,%d -mask \"%ls\" \"%ls\" \"%ls\"", djvulibre_paths->c44_path, quality-25, quality-15, quality, arg_sjbz, tempfile, outputfile);
if(depressSpawn(djvulibre_paths->c44_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;
swprintf(arg0, 32770, L"\"%ls\" \"%ls\" \"BG44=%ls\"", djvulibre_paths->djvuextract_path, outputfile, arg_bg44);
swprintf(arg0, arg0_size, L"\"%ls\" \"%ls\" \"BG44=%ls\"", djvulibre_paths->djvuextract_path, outputfile, arg_bg44);
if(depressSpawn(djvulibre_paths->djvuextract_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;

// Save foreground
Expand All @@ -284,9 +286,9 @@ bool depressConvertLayeredPage(const depress_flags_type flags, wchar_t *inputfil
free(buffer_fg); buffer_fg = 0;
fclose(f_temp); f_temp = 0;
// Convert foreground
swprintf(arg0, 32770, L"\"%ls\" -slice %d -mask \"%ls\" \"%ls\" \"%ls\"", djvulibre_paths->c44_path, quality, arg_sjbz, tempfile, outputfile);
swprintf(arg0, arg0_size, L"\"%ls\" -slice %d -mask \"%ls\" \"%ls\" \"%ls\"", djvulibre_paths->c44_path, quality, arg_sjbz, tempfile, outputfile);
if(depressSpawn(djvulibre_paths->c44_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;
swprintf(arg0, 32770, L"\"%ls\" \"%ls\" \"BG44=%ls\"", djvulibre_paths->djvuextract_path, outputfile, arg_fg44);
swprintf(arg0, arg0_size, L"\"%ls\" \"%ls\" \"BG44=%ls\"", djvulibre_paths->djvuextract_path, outputfile, arg_fg44);
if(depressSpawn(djvulibre_paths->djvuextract_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;

// Save mask
Expand All @@ -297,13 +299,13 @@ bool depressConvertLayeredPage(const depress_flags_type flags, wchar_t *inputfil
goto EXIT;
free(buffer_mask); buffer_mask = 0;
fclose(f_temp); f_temp = 0;
swprintf(arg0, 32770, L"\"%ls\" \"%ls\" \"%ls\"", djvulibre_paths->cjb2_path, tempfile, outputfile);
swprintf(arg0, arg0_size, L"\"%ls\" \"%ls\" \"%ls\"", djvulibre_paths->cjb2_path, tempfile, outputfile);
if(depressSpawn(djvulibre_paths->cjb2_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;
swprintf(arg0, 32770, L"\"%ls\" \"%ls\" \"Sjbz=%ls\"", djvulibre_paths->djvuextract_path, outputfile, arg_sjbz);
swprintf(arg0, arg0_size, L"\"%ls\" \"%ls\" \"Sjbz=%ls\"", djvulibre_paths->djvuextract_path, outputfile, arg_sjbz);
if(depressSpawn(djvulibre_paths->djvuextract_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;

swprintf(arg_options, 1024, L"INFO=,,%d", flags.dpi);
swprintf(arg0, 32770, L"\"%ls\" \"%ls\" %ls \"Sjbz=%ls\" \"FG44=%ls\" \"BG44=%ls\"", djvulibre_paths->djvumake_path,
swprintf(arg0, arg0_size, L"\"%ls\" \"%ls\" %ls \"Sjbz=%ls\" \"FG44=%ls\" \"BG44=%ls\"", djvulibre_paths->djvumake_path,
outputfile, arg_options, arg_sjbz, arg_fg44, arg_bg44);
if(depressSpawn(djvulibre_paths->djvumake_path, arg0, true, true) == INVALID_HANDLE_VALUE) goto EXIT;

Expand Down
3 changes: 3 additions & 0 deletions src/depress_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,13 @@ bool depressDocumentRunTasks(depress_document_type *document)
group_affinity.Mask = MAXULONG_PTR;
}

document->tasks_next_to_process = 0;

for(i = 0; i < document->threads_num; i++) {
document->thread_args[i].tasks = document->tasks;
document->thread_args[i].djvulibre_paths = &document->djvulibre_paths;
document->thread_args[i].tasks_num = document->tasks_num;
document->thread_args[i].tasks_next_to_process = &(document->tasks_next_to_process);
document->thread_args[i].thread_id = i;
document->thread_args[i].threads_num = document->threads_num;
document->thread_args[i].global_error_event = document->global_error_event;
Expand Down
5 changes: 4 additions & 1 deletion src/depress_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "../include/depress_converter.h"

#include "../include/interlocked_ptr.h"

GetActiveProcessorGroupCount_type GetActiveProcessorGroupCount_funcptr = 0;
GetActiveProcessorCount_type GetActiveProcessorCount_funcptr = 0;
SetThreadGroupAffinity_type SetThreadGroupAffinity_funcptr;
Expand Down Expand Up @@ -82,7 +84,8 @@ unsigned int __stdcall depressThreadProc(void *args)

arg = *((depress_thread_arg_type *)args);

for(i = arg.thread_id; i < arg.tasks_num; i += arg.threads_num) {
//for(i = arg.thread_id; i < arg.tasks_num; i += arg.threads_num) {
while((i = InterlockedExchangeAddPtr(arg.tasks_next_to_process, 1)) < arg.tasks_num) {
if(global_error == false)
if(WaitForSingleObject(arg.global_error_event, 0) == WAIT_OBJECT_0)
global_error = true;
Expand Down

0 comments on commit 2caa515

Please sign in to comment.