From 61dba6cb1a5fe4326e628c76633159853a9b718f Mon Sep 17 00:00:00 2001 From: Takase <20792268+takase1121@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:38:20 +0800 Subject: Fix compiler warning and Windows code (#148) * lpm.c: fix compiler warnings this commit doesn't fix the unused function warnings because they may be used in the future. * lpm.c: use CRITICAL_SECTION and _beginthreadex instead of handle mutexes and CreateThread --- src/lpm.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/lpm.c b/src/lpm.c index 6b33495..525e95a 100644 --- a/src/lpm.c +++ b/src/lpm.c @@ -1,6 +1,7 @@ #ifdef _WIN32 #include #include + #include #include #else #ifndef LPM_NO_THRAEDS @@ -80,7 +81,7 @@ typedef struct { typedef struct { #if _WIN32 - HANDLE mutex; + CRITICAL_SECTION mutex; #else pthread_mutex_t mutex; #endif @@ -90,7 +91,7 @@ static lpm_mutex_t* new_mutex() { lpm_mutex_t* mutex = malloc(sizeof(lpm_mutex_t)); #ifndef LPM_NO_THREADS #if _WIN32 - mutex->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&mutex->mutex); #else pthread_mutex_init(&mutex->mutex, NULL); #endif @@ -101,7 +102,7 @@ static lpm_mutex_t* new_mutex() { static void free_mutex(lpm_mutex_t* mutex) { #ifndef LPM_NO_THREADS #if _WIN32 - CloseHandle(mutex->mutex); + DeleteCriticalSection(&mutex->mutex); #else pthread_mutex_destroy(&mutex->mutex); #endif @@ -112,7 +113,7 @@ static void free_mutex(lpm_mutex_t* mutex) { static void lock_mutex(lpm_mutex_t* mutex) { #ifndef LPM_NO_THREADS #if _WIN32 - WaitForSingleObject(mutex->mutex, INFINITE); + EnterCriticalSection(&mutex->mutex); #else pthread_mutex_lock(&mutex->mutex); #endif @@ -122,7 +123,7 @@ static void lock_mutex(lpm_mutex_t* mutex) { static void unlock_mutex(lpm_mutex_t* mutex) { #ifndef LPM_NO_THREADS #if _WIN32 - ReleaseMutex(mutex->mutex); + LeaveCriticalSection(&mutex->mutex); #else pthread_mutex_unlock(&mutex->mutex); #endif @@ -131,7 +132,7 @@ static void unlock_mutex(lpm_mutex_t* mutex) { #if _WIN32 -static DWORD windows_thread_callback(void* data) { +static WINAPI unsigned int windows_thread_callback(void* data) { lpm_thread_t* thread = data; thread->data = thread->func(thread->data); return 0; @@ -144,7 +145,7 @@ static lpm_thread_t* create_thread(void* (*func)(void*), void* data) { #if _WIN32 thread->func = func; thread->data = data; - thread->thread = CreateThread(NULL, 0, windows_thread_callback, thread, 0, NULL); + thread->thread (HANDLE) _beginthreadex(NULL, 0, &windows_thread_callback, thread, 0, NULL); #else pthread_create(&thread->thread, NULL, func, data); #endif @@ -161,6 +162,8 @@ static void* join_thread(lpm_thread_t* thread) { #ifndef LPM_NO_THREADS #if _WIN32 WaitForSingleObject(thread->thread, INFINITE); + CloseHandle(thread->thread); + retval = thread->data; #else pthread_join(thread->thread, &retval); #endif @@ -399,7 +402,7 @@ static int lpm_hash(lua_State* L) { } fclose(file); } else { - sha256_update(&hash_ctx, data, len); + sha256_update(&hash_ctx, (unsigned char *) data, len); } sha256_final(&hash_ctx, buffer); lua_pushhexstring(L, buffer, digest_length); @@ -1210,7 +1213,7 @@ static int lpm_extract(lua_State* L) { } break; } int err; - if (err = mtar_next(&tar)) { + if ((err = mtar_next(&tar))) { mtar_close(&tar); return luaL_error(L, "Error while reading tar archive: %s", mtar_strerror(err)); } @@ -1451,7 +1454,7 @@ static int lpm_extract(lua_State* L) { static int lpm_socket_write(get_context_t* context, int len) { - return context->is_ssl ? mbedtls_ssl_write(&context->ssl, context->buffer, len) : write(context->s, context->buffer, len); + return context->is_ssl ? mbedtls_ssl_write(&context->ssl, (unsigned char *) context->buffer, len) : write(context->s, context->buffer, len); } static int lpm_socket_read(get_context_t* context, int len) { @@ -1459,7 +1462,7 @@ static int lpm_extract(lua_State* L) { len = sizeof(context->buffer) - context->buffer_length; if (len == 0) return len; - len = context->is_ssl ? mbedtls_ssl_read(&context->ssl, &context->buffer[context->buffer_length], len) : read(context->s, &context->buffer[context->buffer_length], len); + len = context->is_ssl ? mbedtls_ssl_read(&context->ssl, (unsigned char *) &context->buffer[context->buffer_length], len) : read(context->s, &context->buffer[context->buffer_length], len); if (len > 0) context->buffer_length += len; return len; @@ -1637,6 +1640,7 @@ static int lpm_extract(lua_State* L) { } } } + default: } finish: if (context->file) { -- cgit v1.2.3