From a43288a3e96e6a247124f3f671a7881c69d292b4 Mon Sep 17 00:00:00 2001 From: Egor Date: Sun, 3 Apr 2022 18:24:29 +0300 Subject: [PATCH 1/2] remove OpenMP support --- .github/workflows/build.yml | 1 - make-release.sh | 1 - thdat/CMakeLists.txt | 6 ------ thdat/thdat.1 | 14 +++----------- thdat/thdat.c | 32 ++++++++++++-------------------- thtk/CMakeLists.txt | 6 ------ thtk/thdat02.c | 20 +++++++------------- thtk/thdat06.c | 22 ++++++---------------- thtk/thdat08.c | 21 ++++++--------------- thtk/thdat105.c | 33 ++++++++++----------------------- thtk/thdat95.c | 33 ++++++++++----------------------- 11 files changed, 54 insertions(+), 135 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index efe186e1..2551cd1d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -61,7 +61,6 @@ jobs: shell: cmd run: | call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" ${{matrix.arch}} - set THTK_ARCH=${{matrix.arch}} call C:\shells\msys2bash.cmd -c "pacman -S --noconfirm --needed --noprogressbar groff dos2unix zip" call C:\shells\msys2bash.cmd -c "./make-release.sh ci" - name: Upload Artifact diff --git a/make-release.sh b/make-release.sh index bd40175c..553831df 100755 --- a/make-release.sh +++ b/make-release.sh @@ -21,7 +21,6 @@ done cp build/thtk/RelWithDebInfo/thtk.dll $releasepath/ cp build/thtk/RelWithDebInfo/thtk.lib $releasepath/ cp build/thtk/RelWithDebInfo/thtk.pdb $releasepath-pdbs/ -cp "$(cygpath "$VCToolsRedistDir")/${THTK_ARCH:-x86}/Microsoft.VC143.OPENMP/vcomp140.dll" $releasepath/ copy_doc() { while [ "$1" != "" ]; do diff --git a/thdat/CMakeLists.txt b/thdat/CMakeLists.txt index ad392e68..52450d6f 100644 --- a/thdat/CMakeLists.txt +++ b/thdat/CMakeLists.txt @@ -1,12 +1,6 @@ include_directories(${CMAKE_SOURCE_DIR}) add_executable(thdat thdat.c) target_compile_options(thdat PRIVATE ${THTK_WARNING_FLAGS}) -find_package(OpenMP) -if (OPENMP_FOUND) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") -endif() target_link_libraries(thdat thtk util) link_setargv(thdat) install(TARGETS thdat DESTINATION bin) diff --git a/thdat/thdat.1 b/thdat/thdat.1 index 8ceb301f..7fb5c055 100644 --- a/thdat/thdat.1 +++ b/thdat/thdat.1 @@ -1,14 +1,14 @@ .\" Redistribution and use in source and binary forms, with .\" or without modification, are permitted provided that the .\" following conditions are met: -.\" +.\" .\" 1. Redistributions of source code must retain this list .\" of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce this .\" list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the .\" distribution. -.\" +.\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND .\" CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED .\" WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED @@ -24,7 +24,7 @@ .\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH .\" DAMAGE. -.Dd April 16, 2018 +.Dd April 3, 2022 .Dt THDAT 1 .Os thtk .Sh NAME @@ -57,14 +57,6 @@ Running the program without a command will list the supported formats. .No If Li d is specified instead of Ar version , .Nm automatically detects the file format. -.Sh ENVIRONMENT -.Bl -tag -width OMP_NUM_THREADS -.It Ev OMP_NUM_THREADS -The number of threads to be used for compression and decompression. -The default used when -.Ev OMP_NUM_THREADS -is not set depends on the OpenMP implementation. -.El .Sh EXIT STATUS The .Nm diff --git a/thdat/thdat.c b/thdat/thdat.c index 1b85c640..b88a400c 100644 --- a/thdat/thdat.c +++ b/thdat/thdat.c @@ -163,24 +163,19 @@ thdat_list( entries = malloc(entry_count * sizeof(*entries)); -#pragma omp parallel /* reduction(max:name_width) */ - { -#pragma omp for - for (e = 0; e < entry_count; ++e) { - thtk_error_t* error = NULL; - entries[e].name = thdat_entry_get_name(state->thdat, e, &error); - entries[e].size = thdat_entry_get_size(state->thdat, e, &error); - entries[e].zsize = thdat_entry_get_zsize(state->thdat, e, &error); - if (!entries[e].name || entries[e].size == -1 || entries[e].zsize == -1) { - print_error(error); - thtk_error_free(&error); - continue; - } - int entry_name_width = strlen(entries[e].name); -#pragma omp critical - if (entry_name_width > name_width) - name_width = entry_name_width; + for (e = 0; e < entry_count; ++e) { + thtk_error_t* error = NULL; + entries[e].name = thdat_entry_get_name(state->thdat, e, &error); + entries[e].size = thdat_entry_get_size(state->thdat, e, &error); + entries[e].zsize = thdat_entry_get_zsize(state->thdat, e, &error); + if (!entries[e].name || entries[e].size == -1 || entries[e].zsize == -1) { + print_error(error); + thtk_error_free(&error); + continue; } + int entry_name_width = strlen(entries[e].name); + if (entry_name_width > name_width) + name_width = entry_name_width; } // th105: Stored = Size @@ -270,7 +265,6 @@ thdat_create_wrapper( k = 0; /* TODO: Properly indicate when insertion fails. */ ssize_t i; -#pragma omp parallel for schedule(dynamic) for (i = 0; i < real_entry_count; ++i) { thtk_error_t* error = NULL; thtk_io_t* entry_stream; @@ -471,7 +465,6 @@ main( if (argc > 1) { ssize_t a; -#pragma omp parallel for schedule(dynamic) for (a = 1; a < argc; ++a) { thtk_error_t* error = NULL; int entry_index; @@ -497,7 +490,6 @@ main( } ssize_t entry_index; -#pragma omp parallel for schedule(dynamic) for (entry_index = 0; entry_index < entry_count; ++entry_index) { thtk_error_t* error = NULL; if (!thdat_extract_file(state, entry_index, &error)) { diff --git a/thtk/CMakeLists.txt b/thtk/CMakeLists.txt index c5f55ea8..d8a55160 100644 --- a/thtk/CMakeLists.txt +++ b/thtk/CMakeLists.txt @@ -17,12 +17,6 @@ add_library(thtk SHARED util.h thtk.h) target_compile_options(thtk PRIVATE ${THTK_WARNING_FLAGS}) -find_package(OpenMP) -if (OPENMP_FOUND) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") -endif() find_package(PkgConfig) if (PKG_CONFIG_FOUND) diff --git a/thtk/thdat02.c b/thtk/thdat02.c index a39c29d4..bbf6f975 100644 --- a/thtk/thdat02.c +++ b/thtk/thdat02.c @@ -124,11 +124,8 @@ th02_read( thdat_entry_t* entry = &thdat->entries[entry_index]; unsigned char* data; ssize_t ret; -#pragma omp critical - { - data = thtk_io_map(thdat->stream, entry->offset, entry->zsize, error); - } - if (!data) + + if (!(data = thtk_io_map(thdat->stream, entry->offset, entry->zsize, error))) return -1; for (ssize_t i = 0; i < entry->zsize; ++i) @@ -208,16 +205,13 @@ th02_write( ssize_t ret = -1; -#pragma omp critical - { - entry->offset = thtk_io_seek(thdat->stream, 0, SEEK_CUR, error); + entry->offset = thtk_io_seek(thdat->stream, 0, SEEK_CUR, error); - if (entry->offset != -1) - ret = thtk_io_write(thdat->stream, data, entry->zsize, error); + if (entry->offset != -1) + ret = thtk_io_write(thdat->stream, data, entry->zsize, error); - if (ret != -1) - thdat->offset += ret; - } + if (ret != -1) + thdat->offset += ret; thtk_io_unmap(output, data); diff --git a/thtk/thdat06.c b/thtk/thdat06.c index 2e9c72bc..f05f30f2 100644 --- a/thtk/thdat06.c +++ b/thtk/thdat06.c @@ -186,13 +186,8 @@ th06_read( thdat_entry_t* entry = &thdat->entries[entry_index]; unsigned char* zdata = malloc(entry->zsize); - int failed; -#pragma omp critical - { - failed = (thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || - (thtk_io_read(thdat->stream, zdata, entry->zsize, error) != entry->zsize); - } - if (failed) + if ((thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || + (thtk_io_read(thdat->stream, zdata, entry->zsize, error) != entry->zsize)) return -1; thtk_io_t* zdata_stream = thtk_io_open_memory(zdata, entry->zsize, error); @@ -202,7 +197,7 @@ th06_read( int ret = th_unlzss(zdata_stream, output, entry->size, error); thtk_io_close(zdata_stream); - + return ret; } @@ -247,14 +242,9 @@ th06_write( entry->extra += zdata[i]; } - int ret; - -#pragma omp critical - { - ret = thtk_io_write(thdat->stream, zdata, entry->zsize, error); - entry->offset = thdat->offset; - thdat->offset += entry->zsize; - } + int ret = thtk_io_write(thdat->stream, zdata, entry->zsize, error); + entry->offset = thdat->offset; + thdat->offset += entry->zsize; thtk_io_unmap(zdata_stream, zdata); thtk_io_close(zdata_stream); diff --git a/thtk/thdat08.c b/thtk/thdat08.c index a2b321a8..f1e6365e 100644 --- a/thtk/thdat08.c +++ b/thtk/thdat08.c @@ -151,14 +151,8 @@ th08_read( return -1; unsigned char* zdata = malloc(entry->zsize); - int failed = 0; -#pragma omp critical - { - failed = (thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || - (thtk_io_read(thdat->stream, zdata, entry->zsize, error) != entry->zsize); - } - - if (failed) + if ((thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || + (thtk_io_read(thdat->stream, zdata, entry->zsize, error) != entry->zsize)) return -1; thtk_io_t* zdata_stream = thtk_io_open_memory(zdata, entry->zsize, error); @@ -303,13 +297,10 @@ th08_write( if (!zdata) return -1; -#pragma omp critical - { - /* TODO: Handle error. */ - thtk_io_write(thdat->stream, zdata, entry->zsize, error); - entry->offset = thdat->offset; - thdat->offset += entry->zsize; - } + /* TODO: Handle error. */ + thtk_io_write(thdat->stream, zdata, entry->zsize, error); + entry->offset = thdat->offset; + thdat->offset += entry->zsize; thtk_io_unmap(zdata_stream, zdata); thtk_io_close(zdata_stream); diff --git a/thtk/thdat105.c b/thtk/thdat105.c index 67bfb0ba..25cc2020 100644 --- a/thtk/thdat105.c +++ b/thtk/thdat105.c @@ -107,14 +107,8 @@ th105_read( thdat_entry_t* entry = thdat->entries + entry_index; unsigned char* data = malloc(entry->size); - int failed = 0; -#pragma omp critical - { - failed = (thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || - (thtk_io_read(thdat->stream, data, entry->size, error) != entry->size); - } - - if (failed) + if ((thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || + (thtk_io_read(thdat->stream, data, entry->size, error) != entry->size)) return -1; th105_decrypt_data(thdat, entry, data); @@ -176,22 +170,15 @@ th105_write( th105_encrypt_data(thdat, entry, data); - int failed = 0; -#pragma omp critical - { - failed = (thtk_io_write(thdat->stream, data, entry->size, error) != entry->size); - if (!failed) { - entry->offset = thdat->offset; - thdat->offset += entry->size; - } - } - - free(data); - - if (failed) + if (!(thtk_io_write(thdat->stream, data, entry->size, error) != entry->size)) { + entry->offset = thdat->offset; + thdat->offset += entry->size; + free(data); + return entry->size; + } else { + free(data); return -1; - - return entry->size; + } } static int diff --git a/thtk/thdat95.c b/thtk/thdat95.c index 181ff990..2335e8a8 100644 --- a/thtk/thdat95.c +++ b/thtk/thdat95.c @@ -167,14 +167,8 @@ th95_read( unsigned char* data; unsigned char* zdata = malloc(entry->zsize); - int failed = 0; -#pragma omp critical - { - failed = (thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || - (thtk_io_read(thdat->stream, zdata, entry->zsize, error) != entry->zsize); - } - - if (failed) + if ((thtk_io_seek(thdat->stream, entry->offset, SEEK_SET, error) == -1) || + (thtk_io_read(thdat->stream, zdata, entry->zsize, error) != entry->zsize)) return -1; const crypt_params_t* crypt_params = th95_get_crypt_param(thdat->version, entry->name); @@ -267,22 +261,15 @@ th95_write( th_encrypt(data, entry->zsize, crypt_params->key, crypt_params->step, crypt_params->block, crypt_params->limit); - int failed = 0; -#pragma omp critical - { - failed = (thtk_io_write(thdat->stream, data, entry->zsize, error) != entry->zsize); - if (!failed) { - entry->offset = thdat->offset; - thdat->offset += entry->zsize; - } - } - - free(data); - - if (failed) + if (!(thtk_io_write(thdat->stream, data, entry->zsize, error) != entry->zsize)) { + entry->offset = thdat->offset; + thdat->offset += entry->zsize; + free(data); + return entry->zsize; + } else { + free(data); return -1; - - return entry->zsize; + } } static int From 2ddfc7d012786201e64864ae0447553823ca2a8b Mon Sep 17 00:00:00 2001 From: Egor Date: Sun, 3 Apr 2022 18:38:24 +0300 Subject: [PATCH 2/2] remove unlocked stdio --- CMakeLists.txt | 13 ------------- config.h.in | 37 ------------------------------------- thecl/ecsscan.l | 2 +- util/file.c | 12 ++++++------ 4 files changed, 7 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4168e250..41c710a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,19 +43,6 @@ check_symbol_exists("scandir" "dirent.h" HAVE_SCANDIR) check_symbol_exists("fstat" "sys/stat.h" HAVE_FSTAT) check_symbol_exists("fileno" "stdio.h" HAVE_FILENO) -check_symbol_exists("getc_unlocked" "stdio.h" HAVE_GETC_UNLOCKED) -if(${HAVE_GETC_UNLOCKED}) - check_symbol_exists("fread_unlocked" "stdio.h" HAVE_FREAD_UNLOCKED) - if(${HAVE_FREAD_UNLOCKED}) - check_symbol_exists("feof_unlocked" "stdio.h" HAVE_FEOF_UNLOCKED) - endif() -else() - check_symbol_exists("_getc_nolock" "stdio.h" HAVE__GETC_NOLOCK) - if(${HAVE__GETC_NOLOCK}) - check_symbol_exists("_fread_nolock" "stdio.h" HAVE__FREAD_NOLOCK) - endif() -endif() - set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) diff --git a/config.h.in b/config.h.in index 2b6b0b06..663dff7f 100644 --- a/config.h.in +++ b/config.h.in @@ -46,43 +46,6 @@ typedef SSIZE_T ssize_t; #cmakedefine HAVE_SCANDIR #cmakedefine HAVE_FILENO -#cmakedefine HAVE_GETC_UNLOCKED -#cmakedefine HAVE_FREAD_UNLOCKED -#cmakedefine HAVE_FEOF_UNLOCKED -#cmakedefine HAVE__GETC_NOLOCK -#cmakedefine HAVE__FREAD_NOLOCK -#ifndef HAVE_GETC_UNLOCKED -# ifdef HAVE__GETC_NOLOCK -# define getc_unlocked _getc_nolock -# define putc_unlocked _putc_nolock -# else -# define getc_unlocked getc -# define putc_unlocked putc -# endif -#endif -#ifndef HAVE_FREAD_UNLOCKED -# ifdef HAVE__FREAD_NOLOCK -# define fread_unlocked _fread_nolock -# define fwrite_unlocked _fwrite_nolock -# else -# define fread_unlocked fread -# define fwrite_unlocked fwrite -# endif -#endif -/* feof and fileno don't lock in VCRT, and thus they don't have _nolock versions. */ -#ifndef HAVE_FEOF_UNLOCKED -# define feof_unlocked feof -# if defined(HAVE_FILENO) -# define fileno_unlocked fileno -# endif -#endif -/* Our code shouldn't need to check these */ -#undef HAVE_GETC_UNLOCKED -#undef HAVE_FREAD_UNLOCKED -#undef HAVE_FEOF_UNLOCKED -#undef HAVE__GETC_NOLOCK -#undef HAVE__FREAD_NOLOCK - #ifdef _WIN32 # define API_SYMBOL __declspec(dllexport) diff --git a/thecl/ecsscan.l b/thecl/ecsscan.l index a87b68ae..0ee90243 100644 --- a/thecl/ecsscan.l +++ b/thecl/ecsscan.l @@ -45,7 +45,7 @@ /* TODO: Add some \r handling to the line counting stuff. */ #define YY_INPUT(buf,result,max_size) \ { \ - int c = getc_unlocked(yyin); \ + int c = getc(yyin); \ if (c == EOF) { \ result = YY_NULL; \ } else { \ diff --git a/util/file.c b/util/file.c index 4989faf9..f9a994ca 100644 --- a/util/file.c +++ b/util/file.c @@ -76,8 +76,8 @@ file_read( void* buffer, size_t size) { - if (fread_unlocked(buffer, size, 1, stream) != 1 && size != 0) { - if (feof_unlocked(stream)) { + if (fread(buffer, size, 1, stream) != 1 && size != 0) { + if (feof(stream)) { fprintf(stderr, "%s: failed reading %lu bytes: unexpected end of file\n", argv0, (long unsigned int)size); @@ -96,7 +96,7 @@ file_write( const void* buffer, size_t size) { - if (fwrite_unlocked(buffer, size, 1, stream) != 1 && size != 0) { + if (fwrite(buffer, size, 1, stream) != 1 && size != 0) { fprintf(stderr, "%s: failed writing %lu bytes: %s\n", argv0, (long unsigned int)size, strerror(errno)); return 0; @@ -116,7 +116,7 @@ file_read_asciiz( if (i == size - 1) { return -1; } - c = getc_unlocked(stream); + c = getc(stream); if (c == EOF) { return -1; } @@ -135,7 +135,7 @@ file_fsize( { #if defined(HAVE_FILENO) && defined(HAVE_FSTAT) struct stat sb; - int fd = fileno_unlocked(stream); + int fd = fileno(stream); if (fd == -1) { fprintf(stderr, "%s: invalid stream: %s\n", argv0, strerror(errno)); @@ -176,7 +176,7 @@ file_mmap( size_t length) { #ifdef HAVE_MMAP - void* map = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fileno_unlocked(stream), 0); + void* map = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fileno(stream), 0); if (map == MAP_FAILED) { fprintf(stderr, "%s: mmap failed: %s\n", argv0, strerror(errno)); return NULL;