Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Dec 9, 2019
1 parent 74bc52a commit cd8dfbb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ compiler: gcc

os:
- linux
#- osx
- osx

#before_install:
#- pip install --user cpp-coveralls
Expand Down
10 changes: 5 additions & 5 deletions base/hmutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions hconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 12 additions & 0 deletions unittest/hmutex_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,6 +37,7 @@ HTHREAD_ROUTINE(test_spinlock) {
printf("hspinlock test OK!\n");
return 0;
}
#endif

HTHREAD_ROUTINE(test_rwlock) {
hrwlock_t rwlock;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit cd8dfbb

Please sign in to comment.