From 73567527f1b4b5f350848d9d697c2410004133c7 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 16 Aug 2018 03:45:17 +0000 Subject: [PATCH] added qse_mtx_trylock() and QSE::Mutex::trylock() --- qse/configure | 44 +++++++++++++++++++++++++++++++++++ qse/configure.ac | 3 +++ qse/include/qse/config.h.in | 3 +++ qse/include/qse/si/Mutex.hpp | 11 ++++----- qse/include/qse/si/mtx.h | 3 +++ qse/include/qse/sttp/Sttp.hpp | 2 +- qse/lib/si/mtx.c | 32 +++++++++++++++++++++++-- qse/samples/si/thr03.cpp | 10 ++++++++ 8 files changed, 99 insertions(+), 9 deletions(-) diff --git a/qse/configure b/qse/configure index eaf75aec..2d76704c 100755 --- a/qse/configure +++ b/qse/configure @@ -18843,6 +18843,50 @@ if test "x$ac_cv_lib_pthread_pthread_mutex_timedlock" = xyes; then : $as_echo "#define HAVE_PTHREAD_MUTEX_TIMEDLOCK 1" >>confdefs.h +fi + +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for pthread_mutex_trylock in -lpthread" >&5 +$as_echo_n "checking for pthread_mutex_trylock in -lpthread... " >&6; } +if ${ac_cv_lib_pthread_pthread_mutex_trylock+:} false; then : + $as_echo_n "(cached) " >&6 +else + ac_check_lib_save_LIBS=$LIBS +LIBS="-lpthread $LIBS" +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char pthread_mutex_trylock (); +int +main () +{ +return pthread_mutex_trylock (); + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + ac_cv_lib_pthread_pthread_mutex_trylock=yes +else + ac_cv_lib_pthread_pthread_mutex_trylock=no +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +LIBS=$ac_check_lib_save_LIBS +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_pthread_pthread_mutex_trylock" >&5 +$as_echo "$ac_cv_lib_pthread_pthread_mutex_trylock" >&6; } +if test "x$ac_cv_lib_pthread_pthread_mutex_trylock" = xyes; then : + + +$as_echo "#define HAVE_PTHREAD_MUTEX_TRYLOCK 1" >>confdefs.h + + fi diff --git a/qse/configure.ac b/qse/configure.ac index 94ab8324..10d83a22 100644 --- a/qse/configure.ac +++ b/qse/configure.ac @@ -257,6 +257,9 @@ AC_CHECK_FUNCS([kqueue kqueue1 kevent]) AC_CHECK_LIB([pthread], [pthread_mutex_timedlock], [ AC_DEFINE([HAVE_PTHREAD_MUTEX_TIMEDLOCK],1,[pthreads has pthread_mutex_timedlock()]) ]) +AC_CHECK_LIB([pthread], [pthread_mutex_trylock], [ + AC_DEFINE([HAVE_PTHREAD_MUTEX_TRYLOCK],1,[pthreads has pthread_mutex_trylock()]) +]) dnl check is the import library for unicows.dll exists dnl this check doesn't look for a particular symbol diff --git a/qse/include/qse/config.h.in b/qse/include/qse/config.h.in index be617406..f0db0e74 100644 --- a/qse/include/qse/config.h.in +++ b/qse/include/qse/config.h.in @@ -421,6 +421,9 @@ /* pthreads has pthread_mutex_timedlock() */ #undef HAVE_PTHREAD_MUTEX_TIMEDLOCK +/* pthreads has pthread_mutex_trylock() */ +#undef HAVE_PTHREAD_MUTEX_TRYLOCK + /* Have PTHREAD_PRIO_INHERIT. */ #undef HAVE_PTHREAD_PRIO_INHERIT diff --git a/qse/include/qse/si/Mutex.hpp b/qse/include/qse/si/Mutex.hpp index 0c87affc..53083883 100644 --- a/qse/include/qse/si/Mutex.hpp +++ b/qse/include/qse/si/Mutex.hpp @@ -47,12 +47,6 @@ public: qse_mtx_fini (&this->mtx); } -#if 0 - bool tryock() QSE_CPP_NOEXCEPT - { - } -#endif - void lock () QSE_CPP_NOEXCEPT { qse_mtx_lock (&this->mtx, QSE_NULL); @@ -63,6 +57,11 @@ public: qse_mtx_unlock (&this->mtx); } + bool trylock () QSE_CPP_NOEXCEPT + { + return qse_mtx_trylock (&this->mtx) >= 0; + } + protected: qse_mtx_t mtx; }; diff --git a/qse/include/qse/si/mtx.h b/qse/include/qse/si/mtx.h index a9af5c0b..2a98fb21 100644 --- a/qse/include/qse/si/mtx.h +++ b/qse/include/qse/si/mtx.h @@ -127,6 +127,9 @@ QSE_EXPORT int qse_mtx_unlock ( qse_mtx_t* mtx ); +QSE_EXPORT int qse_mtx_trylock ( + qse_mtx_t* mtx +); #ifdef __cplusplus } diff --git a/qse/include/qse/sttp/Sttp.hpp b/qse/include/qse/sttp/Sttp.hpp index 331f90a8..4b8ef796 100644 --- a/qse/include/qse/sttp/Sttp.hpp +++ b/qse/include/qse/sttp/Sttp.hpp @@ -172,7 +172,7 @@ protected: int put_char (qse_char_t ch) QSE_CPP_NOEXCEPT { #if defined(QSE_CHAR_IS_MCHAR) - return this->put_mchar(ch) + return this->put_mchar(ch); #else return this->put_wchar(ch); #endif diff --git a/qse/lib/si/mtx.c b/qse/lib/si/mtx.c index 6ece1d5d..e70cdafd 100644 --- a/qse/lib/si/mtx.c +++ b/qse/lib/si/mtx.c @@ -202,9 +202,8 @@ int qse_mtx_lock (qse_mtx_t* mtx, const qse_ntime_t* waiting_time) /* TODO: check for B_WOULD_BLOCK */ /*if (acquire_sem_etc(mtx->hnd, 1, B_ABSOLUTE_TIMEOUT, 0) != B_NO_ERROR) return -1;*/ bigtime_t usec; - usec = QSE_SECNSEC_TO_USEC (waiting_time->sec, waiting_time->nsec); - if (acquire_sem_etc(mtx->hnd, 1, B_TIMEOUT, 0) != B_NO_ERROR) return -1; + if (acquire_sem_etc(mtx->hnd, 1, B_TIMEOUT, usec) != B_NO_ERROR) return -1; } else { @@ -261,4 +260,33 @@ int qse_mtx_unlock (qse_mtx_t* mtx) return 0; } +int qse_mtx_trylock (qse_mtx_t* mtx) +{ +#if defined(_WIN32) + if (WaitForSingleObject(mtx->hnd, 0) != WAIT_OBJECT_0) return -1; +#elif defined(__OS2__) + if (DosRequestMutexSem(mtx->hnd, 0) != NO_ERROR) return -1; +#elif defined(__DOS__) + /* nothing to do */ +#elif defined(__BEOS__) + if (acquire_sem_etc(mtx->hnd, 1, B_TIMEOUT, 0) != B_NO_ERROR) return -1; +#else + #if defined(HAVE_PTHREAD_MUTEX_TRYLOCK) + if (pthread_mutex_trylock((pthread_mutex_t*)&mtx->hnd) != 0) return -1; + #elif defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) + qse_ntime_t t; + struct timespec ts; + + qse_gettime (&t); + ts.tv_sec = t.sec; + ts.tv_nsec = t.nsec; + if (pthread_mutex_timedlock((pthread_mutex_t*)&mtx->hnd, &ts) != 0) return -1; + #else + /* not supported. fallback to normal pthread_mutex_lock(). <--- is this really desirable? */ + if (pthread_mutex_lock ((pthread_mutex_t*)&mtx->hnd) != 0) return -1; + #endif +#endif + return 0; +} + #endif diff --git a/qse/samples/si/thr03.cpp b/qse/samples/si/thr03.cpp index 3b095dfe..577b8672 100644 --- a/qse/samples/si/thr03.cpp +++ b/qse/samples/si/thr03.cpp @@ -38,7 +38,17 @@ public: while (1) { + #if 0 rqdata->mtx.lock (); + #else + while (!rqdata->mtx.trylock()) + { + qse_mtx_lock (g_prmtx, QSE_NULL); + qse_printf (QSE_T("[%p] -> retrying to lock\n"), this, i); + qse_mtx_unlock (g_prmtx); + } + #endif + if (rqdata->stop) { rqdata->mtx.unlock ();