dev 修复与Linux的兼容性问题
This commit is contained in:
parent
52742bbd09
commit
96b3289b2b
4
.gitignore
vendored
4
.gitignore
vendored
@ -9,4 +9,6 @@
|
||||
!test/server/server.c
|
||||
!test/server/CMakeLists.txt
|
||||
!test/thread/thread.c
|
||||
!test/thread/CMakeLists.txt
|
||||
!test/thread/CMakeLists.txt
|
||||
!test/kfifo/kfifo.c
|
||||
!test/kfifo/CMakeLists.txt
|
@ -27,10 +27,11 @@
|
||||
#elif LINUX_PART
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <pthread.h>
|
||||
#include <strings.h>
|
||||
#define SOCKET long long int
|
||||
//#define INVALID_SOCKET (SOCKET)(~0)
|
||||
#define INVALID_SOCKET (SOCKET)(~0)
|
||||
#endif
|
||||
|
||||
#include "tthread.h"
|
||||
@ -95,11 +96,11 @@ static inline int _bind(SOCKET sock, const char* ip, const char* port);
|
||||
static inline int _bindlisten(SOCKET sock, const char* ip, const char* port);
|
||||
|
||||
static inline int _sock(SOCKET* sock, int af, int type);
|
||||
static inline int make_sock(SOCKET* sock) { return make_sock_tcp4(sock); };
|
||||
static inline int make_sock_tcp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_STREAM); }
|
||||
static inline int make_sock_tcp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_STREAM); }
|
||||
static inline int make_sock_udp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_DGRAM); }
|
||||
static inline int make_sock_udp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_DGRAM); }
|
||||
static inline int make_sock(SOCKET* sock) { return make_sock_tcp4(sock); };
|
||||
|
||||
static inline int _sock(SOCKET* sock, int af, int type) {
|
||||
#ifdef WIN_PART
|
||||
|
@ -67,16 +67,16 @@ 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,
|
||||
static inline int tprintf(MUTEX* mutex, const char* format, ...);
|
||||
static inline int tfprintf(MUTEX* mutex, FILE* const stream, const char* format, ...);
|
||||
static inline int tsprintf(MUTEX* mutex, char* const buffer, const char* format, ...);
|
||||
static inline int 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,
|
||||
static inline int tvprintf(MUTEX* mutex, const char* format, va_list arglist);
|
||||
static inline int tvfprintf(MUTEX* mutex, FILE* const stream, const char* format, va_list arglist);
|
||||
static inline int tvsprintf(MUTEX* mutex, char* const buffer, const char* format, va_list arglist);
|
||||
static inline int 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) {
|
||||
@ -85,7 +85,7 @@ static inline int thread_create(TID* tid, void(*start_routine)(void*), void* arg
|
||||
if (h == NULL) { return ERR_THREAD_CREATE; }
|
||||
if (!CloseHandle(h)) { return ERR_THREAD_WIN_CLOSE_HANDLE; }
|
||||
#elif _OS_LINUX
|
||||
if (pthread_create(tid, NULL, start_routine, arg) != 0) { return ERR_THREAD_CREATE; }
|
||||
if (pthread_create(tid, NULL, (void*(*)(void*))start_routine, arg) != 0) { return ERR_THREAD_CREATE; }
|
||||
#endif
|
||||
return ERR_THREAD_SUCCESS;
|
||||
}
|
||||
@ -115,7 +115,7 @@ static inline TID thread_self(void) {
|
||||
#if _OS_WIN
|
||||
return GetCurrentThreadId();
|
||||
#elif _OS_LINUX
|
||||
return pthread_self(void);
|
||||
return pthread_self();
|
||||
#endif
|
||||
|
||||
}
|
||||
@ -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(mutex, NULL) != 0) { return -ERR_THREAD_MUTEX_INIT; }
|
||||
#endif
|
||||
return ERR_THREAD_SUCCESS;
|
||||
}
|
||||
@ -162,7 +162,7 @@ static inline int thread_cond_init(COND* cond) {
|
||||
InitializeConditionVariable(cond);
|
||||
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, NULL) != 0) { return -ERR_THREAD_COND_INIT; }
|
||||
#endif
|
||||
return ERR_THREAD_SUCCESS;
|
||||
}
|
||||
@ -214,58 +214,66 @@ static inline int thread_cond_wait(COND* cond, MUTEX* mutex) {
|
||||
//}
|
||||
|
||||
// thread safety stdio
|
||||
static inline tprintf(MUTEX* mutex, const char* format, ...) {
|
||||
static inline int tprintf(MUTEX* mutex, const char* format, ...) {
|
||||
va_list var;
|
||||
va_start(var, format);
|
||||
tvprintf(mutex, format, var);
|
||||
int res = tvprintf(mutex, format, var);
|
||||
va_end(var);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline tvprintf(MUTEX* mutex, const char* format, va_list arglist) {
|
||||
static inline int tvprintf(MUTEX* mutex, const char* format, va_list arglist) {
|
||||
thread_mutex_lock(mutex);
|
||||
vprintf(format, arglist);
|
||||
int res = vprintf(format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline tfprintf(MUTEX* mutex, FILE *const stream, const char* format, ...) {
|
||||
static inline int tfprintf(MUTEX* mutex, FILE *const stream, const char* format, ...) {
|
||||
va_list var;
|
||||
va_start(var, format);
|
||||
tvfprintf(mutex, stream, format, var);
|
||||
int res = tvfprintf(mutex, stream, format, var);
|
||||
va_end(var);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline tvfprintf(MUTEX* mutex, FILE* const stream, const char* format, va_list arglist) {
|
||||
static inline int tvfprintf(MUTEX* mutex, FILE* const stream, const char* format, va_list arglist) {
|
||||
thread_mutex_lock(mutex);
|
||||
vfprintf(stream, format, arglist);
|
||||
int res = vfprintf(stream, format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline tsprintf(MUTEX* mutex, char* const buffer, const char* format, ...) {
|
||||
static inline int tsprintf(MUTEX* mutex, char* const buffer, const char* format, ...) {
|
||||
va_list var;
|
||||
va_start(var, format);
|
||||
tvsprintf(mutex, buffer, format, var);
|
||||
int res = tvsprintf(mutex, buffer, format, var);
|
||||
va_end(var);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline tvsprintf(MUTEX* mutex, char* const buffer, const char* format, va_list arglist) {
|
||||
static inline int tvsprintf(MUTEX* mutex, char* const buffer, const char* format, va_list arglist) {
|
||||
thread_mutex_lock(mutex);
|
||||
vsprintf(buffer, format, arglist);
|
||||
int res = vsprintf(buffer, format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline tsnprintf(MUTEX* mutex, char* const buffer,
|
||||
static inline int tsnprintf(MUTEX* mutex, char* const buffer,
|
||||
const size_t buffer_count, const char* format, ...) {
|
||||
va_list var;
|
||||
va_start(var, format);
|
||||
tvsnprintf(mutex, buffer, buffer_count, format, var);
|
||||
int res = tvsnprintf(mutex, buffer, buffer_count, format, var);
|
||||
va_end(var);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline tvsnprintf(MUTEX* mutex, char* const buffer,
|
||||
static inline int 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);
|
||||
int res = vsnprintf(buffer,buffer_count, format, arglist);
|
||||
thread_mutex_unlock(mutex);
|
||||
return res;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../../ssocket.h"
|
||||
|
||||
#include <ssocket.h>
|
||||
SOCKET cfd;
|
||||
|
||||
void receive_message(void* param)
|
||||
|
@ -1,8 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../../ssocket.h"
|
||||
#include <limits.h>
|
||||
|
||||
#include <ssocket.h>
|
||||
#define CLIENT_SIZE 32
|
||||
SOCKET sfd;
|
||||
SOCKET cfds[CLIENT_SIZE];
|
||||
|
@ -26,15 +26,7 @@ int test1() {
|
||||
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) {
|
||||
@ -65,7 +57,7 @@ int test2() {
|
||||
if (getchar());
|
||||
//thread_mutex_unlock(&mutex);
|
||||
thread_cond_singal(&cond);
|
||||
Sleep(1000);
|
||||
sleeps(1000);
|
||||
|
||||
//thread_mutex_lock(&mutex);
|
||||
tfprintf(&stdio, stdout, "press enter to continue\n");
|
||||
@ -101,7 +93,7 @@ int main()
|
||||
tfprintf(&stdio, stdout, "test1 start...\n");
|
||||
res = test1();
|
||||
if (res != 0) {
|
||||
return GetLastError();
|
||||
// return GetLastError();
|
||||
}
|
||||
tfprintf(&stdio, stdout, "test1 end...\n");
|
||||
|
||||
@ -111,7 +103,7 @@ int main()
|
||||
tfprintf(&stdio, stdout, "test2 start...\n");
|
||||
res = test2();
|
||||
if (res != 0) {
|
||||
return GetLastError();
|
||||
// return GetLastError();
|
||||
}
|
||||
tfprintf(&stdio, stdout, "test2 end...\n");
|
||||
if(getchar());
|
||||
|
Loading…
x
Reference in New Issue
Block a user