From e3fa5fd34bbb891fc4c30161766f2855347811cd Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Wed, 25 Oct 2023 16:29:29 +0800 Subject: [PATCH] =?UTF-8?q?dev=20=E7=A7=BB=E5=8A=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9,=E6=B7=BB=E5=8A=A0kfifo,=E4=BF=AE=E6=94=B9CMakeLists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +--- include/kfifo.h | 0 ssocket.h => include/ssocket.h | 0 sysenv.h => include/sysenv.h | 0 tthread.h => include/tthread.h | 38 +++++++++++++++++----------------- test/client/CMakeLists.txt | 1 + test/server/CMakeLists.txt | 1 + test/thread/CMakeLists.txt | 1 + test/thread/thread.c | 3 ++- 9 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 include/kfifo.h rename ssocket.h => include/ssocket.h (100%) rename sysenv.h => include/sysenv.h (100%) rename tthread.h => include/tthread.h (84%) diff --git a/.gitignore b/.gitignore index c801c31..de986a3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,10 +2,8 @@ !.gitignore !README.txt !version.txt -!ssocket.h -!tthread.h -!sysenv.h !*/ +!include/* !test/client/client.c !test/client/CMakeLists.txt !test/server/server.c diff --git a/include/kfifo.h b/include/kfifo.h new file mode 100644 index 0000000..e69de29 diff --git a/ssocket.h b/include/ssocket.h similarity index 100% rename from ssocket.h rename to include/ssocket.h diff --git a/sysenv.h b/include/sysenv.h similarity index 100% rename from sysenv.h rename to include/sysenv.h diff --git a/tthread.h b/include/tthread.h similarity index 84% rename from tthread.h rename to include/tthread.h index 61439a1..402e035 100644 --- a/tthread.h +++ b/include/tthread.h @@ -3,15 +3,14 @@ #include "sysenv.h" +#include #include #if _OS_WIN +#include //#include //#include //#include -//#define INFINITE 0xFFFFFFFF // Infinite timeout -//#define WAIT_FAILED ((DWORD)0xFFFFFFFF) -//#include -#include + typedef DWORD TID; typedef CRITICAL_SECTION MUTEX; typedef CONDITION_VARIABLE COND; @@ -102,11 +101,12 @@ static inline void thread_exit(void) { static inline int thread_join(TID tid) { #if _OS_WIN HANDLE h = OpenThread(THREAD_ALL_ACCESS, 0, tid); - //if (h == NULL) { return ERR_THREAD_WIN_OPEN_THREAD; } - if (WaitForSingleObject(h, INFINITE) == WAIT_FAILED) { return ERR_THREAD_JOIN; } - if (!CloseHandle(h)) { return ERR_THREAD_WIN_CLOSE_HANDLE; } + if (h != NULL) { + if (WaitForSingleObject(h, INFINITE) == WAIT_FAILED) { return -ERR_THREAD_JOIN; } + if (!CloseHandle(h)) { return -ERR_THREAD_WIN_CLOSE_HANDLE; } + } #elif _OS_LINUX - if (pthread_join(tid, NULL) != 0) { return ERR_THREAD_JOIN; } + if (pthread_join(tid, NULL) != 0) { return -ERR_THREAD_JOIN; } #endif return ERR_THREAD_SUCCESS; } @@ -125,7 +125,7 @@ static inline int thread_mutex_init(MUTEX* mutex) { #if _OS_WIN InitializeCriticalSection(mutex); #elif _OS_LINUX - if (pthread_mutex_init() != 0) { return ERR_THREAD_MUTEX_INIT; } + if (pthread_mutex_init() != 0) { return -ERR_THREAD_MUTEX_INIT; } #endif return ERR_THREAD_SUCCESS; } @@ -133,7 +133,7 @@ static inline int thread_mutex_init(MUTEX* mutex) { static inline int thread_mutex_destroy(MUTEX* mutex) { #if _OS_WIN #elif _OS_LINUX - if (pthread_mutex_destroy(mutex) != 0) { return ERR_THREAD_MUTEX_DESTROY; } + if (pthread_mutex_destroy(mutex) != 0) { return -ERR_THREAD_MUTEX_DESTROY; } #endif return ERR_THREAD_SUCCESS; } @@ -142,7 +142,7 @@ static inline int thread_mutex_lock(MUTEX* mutex) { #if _OS_WIN EnterCriticalSection(mutex); #elif _OS_LINUX - if (pthread_mutex_lock(mutex) != 0) { return ERR_THREAD_MUTEX_LOCK; } + if (pthread_mutex_lock(mutex) != 0) { return -ERR_THREAD_MUTEX_LOCK; } #endif return ERR_THREAD_SUCCESS; } @@ -151,7 +151,7 @@ static inline int thread_mutex_unlock(MUTEX* mutex) { #if _OS_WIN LeaveCriticalSection(mutex); #elif _OS_LINUX - if (pthread_mutex_unlock(mutex) != 0) { return ERR_THREAD_MUTEX_UNLOCK; } + if (pthread_mutex_unlock(mutex) != 0) { return -ERR_THREAD_MUTEX_UNLOCK; } #endif return ERR_THREAD_SUCCESS; } @@ -160,9 +160,9 @@ static inline int thread_mutex_unlock(MUTEX* mutex) { static inline int thread_cond_init(COND* cond) { #if _OS_WIN InitializeConditionVariable(cond); - if (cond == NULL) { return ERR_THREAD_COND_INIT; } + if (cond == NULL) { return -ERR_THREAD_COND_INIT; } #elif _OS_LINUX - if (pthread_cond_init(cond) != 0) { return ERR_THREAD_COND_INIT; } + if (pthread_cond_init(cond) != 0) { return -ERR_THREAD_COND_INIT; } #endif return ERR_THREAD_SUCCESS; } @@ -170,7 +170,7 @@ static inline int thread_cond_init(COND* cond) { static inline int thread_cond_destroy(COND* cond) { #if _OS_WIN #elif _OS_LINUX - if (pthread_cond_destroy(cond) != 0) { return ERR_THREAD_COND_DESTROY; } + if (pthread_cond_destroy(cond) != 0) { return -ERR_THREAD_COND_DESTROY; } #endif return ERR_THREAD_SUCCESS; } @@ -179,7 +179,7 @@ static inline int thread_cond_singal(COND* cond) { #if _OS_WIN WakeConditionVariable(cond); #elif _OS_LINUX - if (pthread_cond_signal(cond) != 0) { return ERR_THREAD_COND_SINGAL; } + if (pthread_cond_signal(cond) != 0) { return -ERR_THREAD_COND_SINGAL; } #endif return ERR_THREAD_SUCCESS; } @@ -188,16 +188,16 @@ static inline int thread_cond_broadcast(COND* cond) { #if _OS_WIN WakeAllConditionVariable(cond); #elif _OS_LINUX - if (pthread_cond_broadcast(cond) != 0) { return ERR_THREAD_COND_BROADCAST; } + if (pthread_cond_broadcast(cond) != 0) { return -ERR_THREAD_COND_BROADCAST; } #endif return ERR_THREAD_SUCCESS; } static inline int thread_cond_wait(COND* cond, MUTEX* mutex) { #if _OS_WIN - int res = SleepConditionVariableCS(cond, mutex, INFINITE); + if (SleepConditionVariableCS(cond, mutex, INFINITE) != 0) { return -ERR_THREAD_COND_WAIT; } #elif _OS_LINUX - if (pthread_cond_wait(cond, mutex) != 0) { return ERR_THREAD_COND_WAIT; } + if (pthread_cond_wait(cond, mutex) != 0) { return -ERR_THREAD_COND_WAIT; } #endif return ERR_THREAD_SUCCESS; } diff --git a/test/client/CMakeLists.txt b/test/client/CMakeLists.txt index 12dd31f..cd3634d 100644 --- a/test/client/CMakeLists.txt +++ b/test/client/CMakeLists.txt @@ -6,6 +6,7 @@ set(EXECUTABLE_OUTPUT_PATH ../) set(CMAKE_C_STANDARD 99) set(SRC_FILE client.c) +include_directories(../../include) add_executable(${PROJECT_NAME} ${SRC_FILE}) if(WIN32) diff --git a/test/server/CMakeLists.txt b/test/server/CMakeLists.txt index b457301..86f45da 100644 --- a/test/server/CMakeLists.txt +++ b/test/server/CMakeLists.txt @@ -6,6 +6,7 @@ set(EXECUTABLE_OUTPUT_PATH ../) set(CMAKE_C_STANDARD 99) set(SRC_FILE server.c) +include_directories(../../include) add_executable(${PROJECT_NAME} ${SRC_FILE}) if(WIN32) diff --git a/test/thread/CMakeLists.txt b/test/thread/CMakeLists.txt index 8c221b7..dd0b4f9 100644 --- a/test/thread/CMakeLists.txt +++ b/test/thread/CMakeLists.txt @@ -6,6 +6,7 @@ set(EXECUTABLE_OUTPUT_PATH ../) set(CMAKE_C_STANDARD 99) set(SRC_FILE thread.c) +include_directories(../../include) add_executable(${PROJECT_NAME} ${SRC_FILE}) if(WIN32) diff --git a/test/thread/thread.c b/test/thread/thread.c index 56ffc7a..7468bc5 100644 --- a/test/thread/thread.c +++ b/test/thread/thread.c @@ -1,6 +1,7 @@ #include #include -#include "../../tthread.h" + +#include MUTEX mutex; COND cond;