dev 完善线程函数
This commit is contained in:
parent
04f5d638dd
commit
ea7c81cea0
@ -36,7 +36,7 @@ int test1() {
|
||||
//}
|
||||
for (int i = 0; i < MAX1; i++) {
|
||||
res = thread_join(tid[i]);
|
||||
if (res != 0 && res != ERR_THREAD_WIN_OPEN_THREAD) {
|
||||
if (res != 0) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -74,7 +74,7 @@ int test2() {
|
||||
|
||||
for (int i = 0; i < MAX; i++) {
|
||||
res = thread_join(tid[i]);
|
||||
if (res != 0 && res != ERR_THREAD_WIN_OPEN_THREAD) {
|
||||
if (res != 0) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
68
tthread.h
68
tthread.h
@ -34,7 +34,7 @@ enum {
|
||||
ERR_THREAD_CREATE,
|
||||
ERR_THREAD_JOIN,
|
||||
ERR_THREAD_WIN_CLOSE_HANDLE,
|
||||
ERR_THREAD_WIN_OPEN_THREAD,
|
||||
//ERR_THREAD_WIN_OPEN_THREAD,
|
||||
|
||||
ERR_THREAD_MUTEX_INIT,
|
||||
ERR_THREAD_MUTEX_DESTROY,
|
||||
@ -68,6 +68,18 @@ 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 tprintf(MUTEX* mutex, const char* format, ...);
|
||||
static inline tfprintf(MUTEX* mutex, FILE* const stream, const char* format, ...);
|
||||
static inline tsprintf(MUTEX* mutex, char* const buffer, const char* format, ...);
|
||||
static inline tsnprintf(MUTEX* mutex, char* const buffer,
|
||||
const size_t buffer_count, const char* format, ...);
|
||||
|
||||
static inline tvprintf(MUTEX* mutex, const char* format, va_list arglist);
|
||||
static inline tvfprintf(MUTEX* mutex, FILE* const stream, const char* format, va_list arglist);
|
||||
static inline tvsprintf(MUTEX* mutex, char* const buffer, const char* format, va_list arglist);
|
||||
static inline tvsnprintf(MUTEX* mutex, char* const buffer,
|
||||
const size_t buffer_count, const char* format, va_list arglist);
|
||||
|
||||
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, (LPDWORD)tid);
|
||||
@ -90,7 +102,7 @@ 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 (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; }
|
||||
#elif _OS_LINUX
|
||||
@ -112,7 +124,6 @@ static inline TID thread_self(void) {
|
||||
static inline int thread_mutex_init(MUTEX* mutex) {
|
||||
#if _OS_WIN
|
||||
InitializeCriticalSection(mutex);
|
||||
//if (*mutex == NULL) { return ERR_THREAD_MUTEX_INIT; }
|
||||
#elif _OS_LINUX
|
||||
if (pthread_mutex_init() != 0) { return ERR_THREAD_MUTEX_INIT; }
|
||||
#endif
|
||||
@ -121,7 +132,6 @@ static inline int thread_mutex_init(MUTEX* mutex) {
|
||||
|
||||
static inline int thread_mutex_destroy(MUTEX* mutex) {
|
||||
#if _OS_WIN
|
||||
//if (!CloseHandle(mutex)) { return ERR_THREAD_MUTEX_DESTROY; }
|
||||
#elif _OS_LINUX
|
||||
if (pthread_mutex_destroy(mutex) != 0) { return ERR_THREAD_MUTEX_DESTROY; }
|
||||
#endif
|
||||
@ -131,7 +141,6 @@ static inline int thread_mutex_destroy(MUTEX* mutex) {
|
||||
static inline int thread_mutex_lock(MUTEX* mutex) {
|
||||
#if _OS_WIN
|
||||
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
|
||||
@ -141,7 +150,6 @@ static inline int thread_mutex_lock(MUTEX* mutex) {
|
||||
static inline int thread_mutex_unlock(MUTEX* mutex) {
|
||||
#if _OS_WIN
|
||||
LeaveCriticalSection(mutex);
|
||||
//ReleaseMutex(mutex);
|
||||
#elif _OS_LINUX
|
||||
if (pthread_mutex_unlock(mutex) != 0) { return ERR_THREAD_MUTEX_UNLOCK; }
|
||||
#endif
|
||||
@ -161,7 +169,6 @@ static inline int thread_cond_init(COND* cond) {
|
||||
|
||||
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
|
||||
@ -180,7 +187,6 @@ static inline int thread_cond_singal(COND* cond) {
|
||||
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
|
||||
@ -190,8 +196,6 @@ static inline int thread_cond_broadcast(COND* cond) {
|
||||
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
|
||||
@ -213,39 +217,57 @@ static inline int thread_cond_wait(COND* cond, MUTEX* mutex) {
|
||||
static inline tprintf(MUTEX* mutex, const char* format, ...) {
|
||||
va_list var;
|
||||
va_start(var, format);
|
||||
thread_mutex_lock(mutex);
|
||||
vprintf(format, var);
|
||||
thread_mutex_unlock(mutex);
|
||||
tvprintf(mutex, format, var);
|
||||
va_end(var);
|
||||
}
|
||||
|
||||
static inline tvprintf(MUTEX* mutex, const char* format, va_list arglist) {
|
||||
thread_mutex_lock(mutex);
|
||||
vprintf(format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
}
|
||||
|
||||
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);
|
||||
tvfprintf(mutex, stream, format, var);
|
||||
va_end(var);
|
||||
}
|
||||
|
||||
static inline tvfprintf(MUTEX* mutex, FILE* const stream, const char* format, va_list arglist) {
|
||||
thread_mutex_lock(mutex);
|
||||
vfprintf(stream, format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
}
|
||||
|
||||
static inline tsprintf(MUTEX* mutex, char* const buffer, const char* format, ...) {
|
||||
va_list var;
|
||||
va_start(var, format);
|
||||
thread_mutex_lock(mutex);
|
||||
vsprintf(buffer, format, var);
|
||||
thread_mutex_unlock(mutex);
|
||||
tvsprintf(mutex, buffer, format, var);
|
||||
va_end(var);
|
||||
}
|
||||
|
||||
static inline tsnprintf(MUTEX* mutex, char* const buffer, const size_t buffer_count, const char* format, ...) {
|
||||
static inline tvsprintf(MUTEX* mutex, char* const buffer, const char* format, va_list arglist) {
|
||||
thread_mutex_lock(mutex);
|
||||
vsprintf(buffer, format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
}
|
||||
|
||||
static inline tsnprintf(MUTEX* mutex, char* const buffer,
|
||||
const size_t buffer_count, const char* format, ...) {
|
||||
va_list var;
|
||||
va_start(var, format);
|
||||
thread_mutex_lock(mutex);
|
||||
vsnprintf(buffer,buffer_count, format, var);
|
||||
thread_mutex_unlock(mutex);
|
||||
tvsnprintf(mutex, buffer, buffer_count, format, var);
|
||||
va_end(var);
|
||||
}
|
||||
|
||||
static inline tvsnprintf(MUTEX* mutex, char* const buffer,
|
||||
const size_t buffer_count, const char* format, va_list arglist) {
|
||||
thread_mutex_lock(mutex);
|
||||
vsnprintf(buffer,buffer_count, format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user