added qse_mtx_trylock() and QSE::Mutex::trylock()

This commit is contained in:
hyung-hwan 2018-08-16 03:45:17 +00:00
parent 0aca321aee
commit 73567527f1
8 changed files with 99 additions and 9 deletions

44
qse/configure vendored
View File

@ -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 $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 fi

View File

@ -257,6 +257,9 @@ AC_CHECK_FUNCS([kqueue kqueue1 kevent])
AC_CHECK_LIB([pthread], [pthread_mutex_timedlock], [ AC_CHECK_LIB([pthread], [pthread_mutex_timedlock], [
AC_DEFINE([HAVE_PTHREAD_MUTEX_TIMEDLOCK],1,[pthreads has 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 check is the import library for unicows.dll exists
dnl this check doesn't look for a particular symbol dnl this check doesn't look for a particular symbol

View File

@ -421,6 +421,9 @@
/* pthreads has pthread_mutex_timedlock() */ /* pthreads has pthread_mutex_timedlock() */
#undef HAVE_PTHREAD_MUTEX_TIMEDLOCK #undef HAVE_PTHREAD_MUTEX_TIMEDLOCK
/* pthreads has pthread_mutex_trylock() */
#undef HAVE_PTHREAD_MUTEX_TRYLOCK
/* Have PTHREAD_PRIO_INHERIT. */ /* Have PTHREAD_PRIO_INHERIT. */
#undef HAVE_PTHREAD_PRIO_INHERIT #undef HAVE_PTHREAD_PRIO_INHERIT

View File

@ -47,12 +47,6 @@ public:
qse_mtx_fini (&this->mtx); qse_mtx_fini (&this->mtx);
} }
#if 0
bool tryock() QSE_CPP_NOEXCEPT
{
}
#endif
void lock () QSE_CPP_NOEXCEPT void lock () QSE_CPP_NOEXCEPT
{ {
qse_mtx_lock (&this->mtx, QSE_NULL); qse_mtx_lock (&this->mtx, QSE_NULL);
@ -63,6 +57,11 @@ public:
qse_mtx_unlock (&this->mtx); qse_mtx_unlock (&this->mtx);
} }
bool trylock () QSE_CPP_NOEXCEPT
{
return qse_mtx_trylock (&this->mtx) >= 0;
}
protected: protected:
qse_mtx_t mtx; qse_mtx_t mtx;
}; };

View File

@ -127,6 +127,9 @@ QSE_EXPORT int qse_mtx_unlock (
qse_mtx_t* mtx qse_mtx_t* mtx
); );
QSE_EXPORT int qse_mtx_trylock (
qse_mtx_t* mtx
);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -172,7 +172,7 @@ protected:
int put_char (qse_char_t ch) QSE_CPP_NOEXCEPT int put_char (qse_char_t ch) QSE_CPP_NOEXCEPT
{ {
#if defined(QSE_CHAR_IS_MCHAR) #if defined(QSE_CHAR_IS_MCHAR)
return this->put_mchar(ch) return this->put_mchar(ch);
#else #else
return this->put_wchar(ch); return this->put_wchar(ch);
#endif #endif

View File

@ -202,9 +202,8 @@ int qse_mtx_lock (qse_mtx_t* mtx, const qse_ntime_t* waiting_time)
/* TODO: check for B_WOULD_BLOCK */ /* TODO: check for B_WOULD_BLOCK */
/*if (acquire_sem_etc(mtx->hnd, 1, B_ABSOLUTE_TIMEOUT, 0) != B_NO_ERROR) return -1;*/ /*if (acquire_sem_etc(mtx->hnd, 1, B_ABSOLUTE_TIMEOUT, 0) != B_NO_ERROR) return -1;*/
bigtime_t usec; bigtime_t usec;
usec = QSE_SECNSEC_TO_USEC (waiting_time->sec, waiting_time->nsec); 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 else
{ {
@ -261,4 +260,33 @@ int qse_mtx_unlock (qse_mtx_t* mtx)
return 0; 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 #endif

View File

@ -38,7 +38,17 @@ public:
while (1) while (1)
{ {
#if 0
rqdata->mtx.lock (); 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) if (rqdata->stop)
{ {
rqdata->mtx.unlock (); rqdata->mtx.unlock ();