重写windows方面的mutex以适配新增cond

This commit is contained in:
zzy 2023-10-24 08:52:42 +08:00
parent 73847e5708
commit 3a07642187
2 changed files with 212 additions and 33 deletions

View File

@ -3,36 +3,123 @@
#include "../../tthread.h"
MUTEX mutex;
COND cond;
MUTEX stdio;
void work(void* arg) {
void work1(void* arg) {
int* num = (int*)arg;
for (int i = 0; i < 100; i++) {
thread_mutex_lock(mutex);
printf("%d => %d\n", 1, (*num)++);
thread_mutex_unlock(mutex);
thread_mutex_lock(&mutex);
tfprintf(&stdio, stdout, "%d => %d\n", thread_self(), (*num)++);
thread_mutex_unlock(&mutex);
}
}
int test1() {
int res;
#define MAX1 2
TID tid[MAX1] = { 0 };
int num = 1;
for (int i = 0; i < MAX1; i++) {
if (thread_create(&tid[i], work1, &num) != 0) {
return -2;
}
}
//res = thread_join(tid[0]);
//if (res != 0) {
// return res;
//}
//res = thread_join(tid[1]);
//if (res != 0) {
// return res;
//}
for (int i = 0; i < MAX1; i++) {
res = thread_join(tid[i]);
if (res != 0 && res != ERR_THREAD_WIN_OPEN_THREAD) {
return res;
}
}
return 0;
}
void work2(void* arg) {
thread_mutex_lock(&mutex);
thread_cond_wait(&cond, &mutex);
tfprintf(&stdio, stdout, "<%lu>\n", thread_self());
thread_mutex_unlock(&mutex);
}
int test2() {
int res;
#define MAX 6
TID tid[MAX] = { 0 };
for (int i = 0; i < MAX; i++) {
if (thread_create(&tid[i], work2, NULL) != 0) {
return -2;
}
}
//thread_mutex_lock(&mutex);
tfprintf(&stdio, stdout, "press enter to continue\n");
if (getchar());
//thread_mutex_unlock(&mutex);
thread_cond_singal(&cond);
Sleep(1000);
//thread_mutex_lock(&mutex);
tfprintf(&stdio, stdout, "press enter to continue\n");
if (getchar());
//thread_mutex_unlock(&mutex);
thread_cond_broadcast(&cond);
for (int i = 0; i < MAX; i++) {
res = thread_join(tid[i]);
if (res != 0 && res != ERR_THREAD_WIN_OPEN_THREAD) {
return res;
}
}
return 0;
}
int main()
{
//DWORD a[3] = { 1, 2, 3 };
//LPDWORD p = a;
//printf("a[0] = %d, a[1] = %d, a[3] = %d\n", a[0], a[1], a[2]);
//printf("p[0] = %d, p[1] = %d, p[3] = %d\n", p[0], p[1], p[2]);
//printf("DWORD:%lld LPDWORD:%lld\n", sizeof(a), sizeof(p));
//printf("DWORD:%lld LPDWORD:%lld long:%lld\n", sizeof(DWORD), sizeof(LPDWORD), sizeof(long));
//return 0;
int res;
TID tid;
int num = 0;
if (thread_mutex_init(&mutex) != 0) {
return -1;
}
if (thread_create(&tid, work, &num) != 0) {
if (thread_mutex_init(&stdio) != 0) {
return -1;
}
tfprintf(&stdio, stdout, "test1 start...\n");
res = test1();
if (res != 0) {
return GetLastError();
}
tfprintf(&stdio, stdout, "test1 end...\n");
if (thread_cond_init(&cond) != 0) {
return -2;
}
for (int i = 0; i < 100; i++) {
thread_mutex_lock(mutex);
printf("%d => %d\n", 0, num++);
thread_mutex_unlock(mutex);
}
res = thread_join(tid);
tfprintf(&stdio, stdout, "test2 start...\n");
res = test2();
if (res != 0) {
//res = GetLastError();
return res;
return GetLastError();
}
tfprintf(&stdio, stdout, "test2 end...\n");
if(getchar());
if (thread_mutex_destroy(&mutex) != 0) {
return -3;
}
if (thread_cond_destroy(&cond) != 0) {
return -4;
}
return 0;

128
tthread.h
View File

@ -2,6 +2,8 @@
#define _TTHREAD_H_
#include "sysenv.h"
#include <stdarg.h>
#if _OS_WIN
//#include <handleapi.h>
//#include <synchapi.h>
@ -11,13 +13,13 @@
//#include <WinBase.h>
#include <Windows.h>
typedef DWORD TID;
typedef HANDLE MUTEX;
typedef HANDLE COND;
typedef CRITICAL_SECTION MUTEX;
typedef CONDITION_VARIABLE COND;
#elif _OS_LINUX
#include <pthread.h>
#define TID pthread_t
#define MUTEX pthread_mutex_t
#define COND pthread_cond_t
typedef pthread_t TID;
typedef pthread_mutex_t MUTEX;
typedef pthread_cond_t COND;
#else
#error "Not Supported Operator System"
#endif
@ -52,15 +54,23 @@ enum {
static inline int thread_create(TID* tid, void(*start_routine)(void*), void* arg);
static inline void thread_exit(void);
static inline int thread_join(TID tid);
static inline TID thread_self(void);
static inline int thread_mutex_init(MUTEX* mutex);
static inline int thread_mutex_destory(MUTEX mutex);
static inline int thread_mutex_lock(MUTEX mutex);
static inline int thread_mutex_unlock(MUTEX mutex);
static inline int thread_mutex_destroy(MUTEX* mutex);
static inline int thread_mutex_lock(MUTEX* mutex);
static inline int thread_mutex_unlock(MUTEX* mutex);
static inline int thread_cond_init(COND* cond);
static inline int thread_cond_destroy(COND* cond);
static inline int thread_cond_singal(COND* cond);
static inline int thread_cond_broadcast(COND* cond);
static inline int thread_cond_wait(COND* cond, MUTEX* mutex);
static inline int thread_cond_timedwait(COND* cond, MUTEX* mutex);
static inline int thread_create(TID* tid, void(*start_routine)(void*), void* arg) {
#if _OS_WIN
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, tid);
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, (LPDWORD)tid);
if (h == NULL) { return ERR_THREAD_CREATE; }
if (!CloseHandle(h)) { return ERR_THREAD_WIN_CLOSE_HANDLE; }
#elif _OS_LINUX
@ -89,44 +99,126 @@ static inline int thread_join(TID tid) {
return ERR_THREAD_SUCCESS;
}
static inline TID thread_self(void) {
#if _OS_WIN
return GetCurrentThreadId();
#elif _OS_LINUX
return pthread_self(void);
#endif
}
// mutex part
static inline int thread_mutex_init(MUTEX* mutex) {
#if _OS_WIN
*mutex = CreateMutex(NULL, FALSE, NULL);
if (*mutex == NULL) { return ERR_THREAD_MUTEX_INIT; }
InitializeCriticalSection(mutex);
//if (*mutex == NULL) { return ERR_THREAD_MUTEX_INIT; }
#elif _OS_LINUX
if (pthread_mutex_init() != 0) { return ERR_THREAD_MUTEX_INIT; }
#endif
return ERR_THREAD_SUCCESS;
}
static inline int thread_mutex_destory(MUTEX mutex) {
static inline int thread_mutex_destroy(MUTEX* mutex) {
#if _OS_WIN
if (!CloseHandle(mutex)) { return ERR_THREAD_MUTEX_DESTROY; }
//if (!CloseHandle(mutex)) { return ERR_THREAD_MUTEX_DESTROY; }
#elif _OS_LINUX
if (pthread_mutex_destroy(mutex) != 0) { return ERR_THREAD_MUTEX_DESTROY; }
#endif
ERR_THREAD_SUCCESS;
return ERR_THREAD_SUCCESS;
}
static inline int thread_mutex_lock(MUTEX mutex) {
static inline int thread_mutex_lock(MUTEX* mutex) {
#if _OS_WIN
if (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) { return ERR_THREAD_MUTEX_LOCK; }
EnterCriticalSection(mutex);
//if (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) { return ERR_THREAD_MUTEX_LOCK; }
#elif _OS_LINUX
if (pthread_mutex_lock(mutex) != 0) { return ERR_THREAD_MUTEX_LOCK; }
#endif
return ERR_THREAD_SUCCESS;
}
static inline int thread_mutex_unlock(MUTEX mutex) {
static inline int thread_mutex_unlock(MUTEX* mutex) {
#if _OS_WIN
ReleaseMutex(mutex);
LeaveCriticalSection(mutex);
//ReleaseMutex(mutex);
#elif _OS_LINUX
if (pthread_mutex_unlock(mutex) != 0) { return ERR_THREAD_MUTEX_UNLOCK; }
#endif
return ERR_THREAD_SUCCESS;
}
//cond part
static inline int thread_cond_init(COND* cond) {
#if _OS_WIN
InitializeConditionVariable(cond);
if (cond == NULL) { return ERR_THREAD_COND_INIT; }
#elif _OS_LINUX
if (pthread_cond_init(cond) != 0) { return ERR_THREAD_COND_INIT; }
#endif
return ERR_THREAD_SUCCESS;
}
static inline int thread_cond_destroy(COND* cond) {
#if _OS_WIN
//if (!CloseHandle(cond)) { return ERR_THREAD_COND_DESTROY; }
#elif _OS_LINUX
if (pthread_cond_destroy(cond) != 0) { return ERR_THREAD_COND_DESTROY; }
#endif
return ERR_THREAD_SUCCESS;
}
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; }
#endif
return ERR_THREAD_SUCCESS;
}
static inline int thread_cond_broadcast(COND* cond) {
#if _OS_WIN
WakeAllConditionVariable(cond);
//if (!PulseEvent(cond)) { return ERR_THREAD_COND_BROADCAST; }
#elif _OS_LINUX
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 (res == WAIT_FAILED) { return ERR_THREAD_COND_WAIT; }
//if (res == WAIT_OBJECT_0) { }
#elif _OS_LINUX
if (pthread_cond_wait(cond, mutex) != 0) { return ERR_THREAD_COND_WAIT; }
#endif
return ERR_THREAD_SUCCESS;
}
//static inline int thread_cond_timedwait(COND cond, MUTEX mutex, ) {
//#if _OS_WIN
// if (SignalObjectAndWait(cond, mutex, INFINITE, 0) == WAIT_FAILED) { return ERR_THREAD_COND_TIMEDWAIT; }
// WAIT_TIMEOUT
//#elif _OS_LINUX
// if (pthread_cond_timedwait(cond, mutex, ) != 0) { return ERR_THREAD_COND_TIMEDWAIT; }
// ETIMEDOUT
//#endif
// return ERR_THREAD_SUCCESS;
//}
// thread safety stdio
static inline tfprintf(MUTEX* mutex, FILE *const stream, const char* format, ...) {
va_list var;
va_start(var, format);
thread_mutex_lock(mutex);
vfprintf(stream, format, var);
thread_mutex_unlock(mutex);
va_end(var);
}
#ifdef __cplusplus
}
#endif