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