ithewei 6 năm trước cách đây
mục cha
commit
cd8dfbb4b2
5 tập tin đã thay đổi với 28 bổ sung6 xóa
  1. 1 1
      .travis.yml
  2. 5 5
      base/hmutex.h
  3. 2 0
      configure
  4. 8 0
      hconfig.h
  5. 12 0
      unittest/hmutex_test.c

+ 1 - 1
.travis.yml

@@ -4,7 +4,7 @@ compiler: gcc
 
 os:
     - linux
-    #- osx
+    - osx
 
 #before_install:
   #- pip install --user cpp-coveralls

+ 5 - 5
base/hmutex.h

@@ -32,7 +32,7 @@
 #define htimed_mutex_unlock(pmutex)     ReleaseMutex(*(pmutex))
 // true:  WAIT_OBJECT_0
 // false: WAIT_OBJECT_TIMEOUT
-#define htimed_mutex_lock_for(pmutex, ms)   WaitForSingleObject(*(pmutex), ms) == WAIT_OBJECT_0
+#define htimed_mutex_lock_for(pmutex, ms)   ( WaitForSingleObject(*(pmutex), ms) == WAIT_OBJECT_0 )
 
 #define hcondvar_t                      CONDITION_VARIABLE
 #define hcondvar_init                   InitializeConditionVariable
@@ -55,21 +55,19 @@ static inline void honce(honce_t* once, honce_fn fn) {
     InitOnceExecuteOnce(once, s_once_func, (PVOID)fn, &dummy);
 }
 #else
-#ifndef __USE_XOPEN2K
-#define __USE_XOPEN2K // for pthread_mutex_timedlock, pthread_spinlock, pthread_rwlock...
-#endif
-
 #define hmutex_t                pthread_mutex_t
 #define hmutex_init(pmutex)     pthread_mutex_init(pmutex, NULL)
 #define hmutex_destroy          pthread_mutex_destroy
 #define hmutex_lock             pthread_mutex_lock
 #define hmutex_unlock           pthread_mutex_unlock
 
+#if HAVE_PTHREAD_SPIN_LOCK
 #define hspinlock_t             pthread_spinlock_t
 #define hspinlock_init(pspin)   pthread_spin_init(pspin, PTHREAD_PROCESS_PRIVATE)
 #define hspinlock_destroy       pthread_spin_destroy
 #define hspinlock_lock          pthread_spin_lock
 #define hspinlock_unlock        pthread_spin_unlock
+#endif
 
 #define hrwlock_t               pthread_rwlock_t
 #define hrwlock_init(prwlock)   pthread_rwlock_init(prwlock, NULL)
@@ -79,6 +77,7 @@ static inline void honce(honce_t* once, honce_fn fn) {
 #define hrwlock_wrlock          pthread_rwlock_wrlock
 #define hrwlock_wrunlock        pthread_rwlock_unlock
 
+#if HAVE_PTHREAD_MUTEX_TIMEDLOCK
 #define htimed_mutex_t              pthread_mutex_t
 #define htimed_mutex_init(pmutex)   pthread_mutex_init(pmutex, NULL)
 #define htimed_mutex_destroy        pthread_mutex_destroy
@@ -98,6 +97,7 @@ static inline int htimed_mutex_lock_for(htimed_mutex_t* mutex, unsigned long ms)
     }
     return pthread_mutex_timedlock(mutex, &ts) != ETIMEDOUT;
 }
+#endif
 
 #define hcondvar_t              pthread_cond_t
 #define hcondvar_init(pcond)    pthread_cond_init(pcond, NULL)

+ 2 - 0
configure

@@ -122,6 +122,8 @@ function=strlcpy && header=string.h && check_funtion
 function=strlcat && header=string.h && check_funtion
 function=clock_gettime && header=time.h && check_funtion
 function=gettimeofday && header=sys/time.h && check_funtion
+function=pthread_spin_lock && header=pthread.h && check_funtion
+function=pthread_mutex_timedlock && header=pthread.h && check_funtion
 
 # end confile
 cat << END >> $confile

+ 8 - 0
hconfig.h

@@ -45,4 +45,12 @@
 #define HAVE_GETTIMEOFDAY 1
 #endif
 
+#ifndef HAVE_PTHREAD_SPIN_LOCK
+#define HAVE_PTHREAD_SPIN_LOCK 0
+#endif
+
+#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
+#define HAVE_PTHREAD_MUTEX_TIMEDLOCK 0
+#endif
+
 #endif  // HW_CONFIG_H_

+ 12 - 0
unittest/hmutex_test.c

@@ -27,6 +27,7 @@ HTHREAD_ROUTINE(test_mutex) {
     return 0;
 }
 
+#if HAVE_PTHREAD_SPIN_LOCK
 HTHREAD_ROUTINE(test_spinlock) {
     hspinlock_t spin;
     hspinlock_init(&spin);
@@ -36,6 +37,7 @@ HTHREAD_ROUTINE(test_spinlock) {
     printf("hspinlock test OK!\n");
     return 0;
 }
+#endif
 
 HTHREAD_ROUTINE(test_rwlock) {
     hrwlock_t rwlock;
@@ -49,6 +51,7 @@ HTHREAD_ROUTINE(test_rwlock) {
     return 0;
 }
 
+#if HAVE_PTHREAD_MUTEX_TIMEDLOCK
 HTHREAD_ROUTINE(test_timed_mutex) {
     htimed_mutex_t mutex;
     htimed_mutex_init(&mutex);
@@ -62,6 +65,7 @@ HTHREAD_ROUTINE(test_timed_mutex) {
     printf("htimed_mutex test OK!\n");
     return 0;
 }
+#endif
 
 HTHREAD_ROUTINE(test_condvar) {
     hmutex_t mutex;
@@ -87,16 +91,24 @@ HTHREAD_ROUTINE(test_condvar) {
 int main(int argc, char* argv[]) {
     hthread_t thread_once = hthread_create(test_once, NULL);
     hthread_t thread_mutex = hthread_create(test_mutex, NULL);
+#if HAVE_PTHREAD_SPIN_LOCK
     hthread_t thread_spinlock = hthread_create(test_spinlock, NULL);
+#endif
     hthread_t thread_rwlock = hthread_create(test_rwlock, NULL);
+#if HAVE_PTHREAD_MUTEX_TIMEDLOCK
     hthread_t thread_timed_mutex = hthread_create(test_timed_mutex, NULL);
+#endif
     hthread_t thread_condvar = hthread_create(test_condvar, NULL);
 
     hthread_join(thread_once);
     hthread_join(thread_mutex);
+#if HAVE_PTHREAD_SPIN_LOCK
     hthread_join(thread_spinlock);
+#endif
     hthread_join(thread_rwlock);
+#if HAVE_PTHREAD_MUTEX_TIMEDLOCK
     hthread_join(thread_timed_mutex);
+#endif
     hthread_join(thread_condvar);
     printf("hthread test OK!\n");
     return 0;