ソースを参照

move BMutex to base/ and make BADVPN_THREAD_SAFE control whether it actually does anything

ambrop7 13 年 前
コミット
a6fb29f45c
4 ファイル変更16 行追加19 行削除
  1. 2 0
      CMakeLists.txt
  2. 12 17
      base/BMutex.h
  3. 1 1
      compile-tun2sock.sh
  4. 1 1
      nspr_support/BSSLConnection.h

+ 2 - 0
CMakeLists.txt

@@ -144,6 +144,7 @@ endif ()
 # platform-specific stuff
 if (WIN32)
     add_definitions(-DBADVPN_USE_WINAPI -D_WIN32_WINNT=0x600 -DWIN32_LEAN_AND_MEAN)
+    add_definitions(-DBADVPN_THREAD_SAFE=0)
 
     set(CMAKE_REQUIRED_DEFINITIONS "-D_WIN32_WINNT=0x600")
     check_symbol_exists(WSAID_WSASENDMSG "winsock2.h;mswsock.h" HAVE_MSW_1)
@@ -162,6 +163,7 @@ if (WIN32)
 else ()
     set(BADVPN_THREADWORK_USE_PTHREAD 1)
     add_definitions(-DBADVPN_THREADWORK_USE_PTHREAD)
+    add_definitions(-DBADVPN_THREAD_SAFE=1)
 
     link_libraries(rt)
 

+ 12 - 17
threadwork/BMutex.h → base/BMutex.h

@@ -30,7 +30,11 @@
 #ifndef BADVPN_BMUTEX_H
 #define BADVPN_BMUTEX_H
 
-#ifdef BADVPN_THREADWORK_USE_PTHREAD
+#if !defined(BADVPN_THREAD_SAFE) || (BADVPN_THREAD_SAFE != 0 && BADVPN_THREAD_SAFE != 1)
+#error BADVPN_THREAD_SAFE is not defined or incorrect
+#endif
+
+#if BADVPN_THREAD_SAFE
 #include <pthread.h>
 #endif
 
@@ -38,10 +42,10 @@
 #include <base/DebugObject.h>
 
 typedef struct {
-#ifdef BADVPN_THREADWORK_USE_PTHREAD
+#if BADVPN_THREAD_SAFE
     pthread_mutex_t pthread_mutex;
-    DebugObject d_obj;
 #endif
+    DebugObject d_obj;
 } BMutex;
 
 static int BMutex_Init (BMutex *o) WARN_UNUSED;
@@ -51,55 +55,46 @@ static void BMutex_Unlock (BMutex *o);
 
 static int BMutex_Init (BMutex *o)
 {
-#ifdef BADVPN_THREADWORK_USE_PTHREAD
+#if BADVPN_THREAD_SAFE
     if (pthread_mutex_init(&o->pthread_mutex, NULL) != 0) {
         return 0;
     }
+#endif
     
     DebugObject_Init(&o->d_obj);
     return 1;
-#else
-    ASSERT(0)
-    return 0;
-#endif
 }
 
 static void BMutex_Free (BMutex *o)
 {
-#ifdef BADVPN_THREADWORK_USE_PTHREAD
     DebugObject_Free(&o->d_obj);
     
+#if BADVPN_THREAD_SAFE    
     int res = pthread_mutex_destroy(&o->pthread_mutex);
     B_USE(res)
     ASSERT(res == 0)
-#else
-    ASSERT(0)
 #endif
 }
 
 static void BMutex_Lock (BMutex *o)
 {
-#ifdef BADVPN_THREADWORK_USE_PTHREAD
     DebugObject_Access(&o->d_obj);
     
+#if BADVPN_THREAD_SAFE    
     int res = pthread_mutex_lock(&o->pthread_mutex);
     B_USE(res)
     ASSERT(res == 0)
-#else
-    ASSERT(0)
 #endif
 }
 
 static void BMutex_Unlock (BMutex *o)
 {
-#ifdef BADVPN_THREADWORK_USE_PTHREAD
     DebugObject_Access(&o->d_obj);
     
+#if BADVPN_THREAD_SAFE    
     int res = pthread_mutex_unlock(&o->pthread_mutex);
     B_USE(res)
     ASSERT(res == 0)
-#else
-    ASSERT(0)
 #endif
 }
 

+ 1 - 1
compile-tun2sock.sh

@@ -38,7 +38,7 @@ fi
 
 CFLAGS="${CFLAGS} -std=gnu99"
 INCLUDES=( "-I${SRCDIR}" "-I${SRCDIR}/lwip/src/include/ipv4" "-I${SRCDIR}/lwip/src/include" "-I${SRCDIR}/lwip/custom" )
-DEFS=( -DBADVPN_THREADWORK_USE_PTHREAD -DBADVPN_LINUX -DBADVPN_BREACTOR_BADVPN -D_GNU_SOURCE )
+DEFS=( -DBADVPN_THREAD_SAFE=0 -DBADVPN_LINUX -DBADVPN_BREACTOR_BADVPN -D_GNU_SOURCE )
 
 [[ $KERNEL = "2.4" ]] && DEFS=( "${DEFS[@]}" -DBADVPN_USE_SELFPIPE -DBADVPN_USE_POLL ) || DEFS=( "${DEFS[@]}" -DBADVPN_USE_SIGNALFD -DBADVPN_USE_EPOLL )
 

+ 1 - 1
nspr_support/BSSLConnection.h

@@ -37,10 +37,10 @@
 #include <misc/debugerror.h>
 #include <base/DebugObject.h>
 #include <base/BPending.h>
+#include <base/BMutex.h>
 #include <flow/StreamPassInterface.h>
 #include <flow/StreamRecvInterface.h>
 #include <threadwork/BThreadWork.h>
-#include <threadwork/BMutex.h>
 
 #define BSSLCONNECTION_EVENT_UP 1
 #define BSSLCONNECTION_EVENT_ERROR 2