added spl.h
changed SpinLock.hpp to use spl.h
This commit is contained in:
@ -21,6 +21,7 @@ pkginclude_HEADERS = \
|
||||
sck.h \
|
||||
sinfo.h \
|
||||
sio.h \
|
||||
spl.h \
|
||||
task.h \
|
||||
thr.h \
|
||||
tio.h
|
||||
|
@ -133,8 +133,8 @@ am__can_run_installinfo = \
|
||||
esac
|
||||
am__pkginclude_HEADERS_DIST = aio.h aio-pro.h aio-sck.h cnd.h dir.h \
|
||||
fio.h fs.h glob.h intr.h log.h mtx.h mux.h nwad.h nwif.h \
|
||||
nwio.h pio.h rwl.h sck.h sinfo.h sio.h task.h thr.h tio.h \
|
||||
AppRoot.hpp SocketAddress.hpp Socket.hpp SpinLock.hpp \
|
||||
nwio.h pio.h rwl.h sck.h sinfo.h sio.h spl.h task.h thr.h \
|
||||
tio.h AppRoot.hpp SocketAddress.hpp Socket.hpp SpinLock.hpp \
|
||||
Thread.hpp
|
||||
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
|
||||
am__vpath_adj = case $$p in \
|
||||
@ -369,7 +369,8 @@ top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
pkginclude_HEADERS = aio.h aio-pro.h aio-sck.h cnd.h dir.h fio.h fs.h \
|
||||
glob.h intr.h log.h mtx.h mux.h nwad.h nwif.h nwio.h pio.h \
|
||||
rwl.h sck.h sinfo.h sio.h task.h thr.h tio.h $(am__append_1)
|
||||
rwl.h sck.h sinfo.h sio.h spl.h task.h thr.h tio.h \
|
||||
$(am__append_1)
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
|
@ -30,8 +30,13 @@
|
||||
#include <qse/Types.hpp>
|
||||
#include <qse/Uncopyable.hpp>
|
||||
|
||||
#if defined(QSE_HAVE_SYNC_LOCK_TEST_AND_SET) && defined(QSE_HAVE_SYNC_LOCK_RELEASE)
|
||||
// don't include anything
|
||||
#undef QSE_SPL_NO_UNSUPPORTED_ERROR
|
||||
#define QSE_SPL_NO_UNSUPPORTED_ERROR
|
||||
#include <qse/si/spl.h>
|
||||
#undef QSE_SPL_NO_UNSUPPORTED_ERROR
|
||||
|
||||
#if defined(QSE_SUPPORT_SPL)
|
||||
// don't include anything else
|
||||
#elif defined(QSE_LANG_CPP11)
|
||||
// NOTE: <stdatomic.h> in C11 doesn't seem compatible due to lack of
|
||||
// the keyword _Atomic in C++11
|
||||
@ -43,12 +48,16 @@ QSE_BEGIN_NAMESPACE(QSE)
|
||||
class SpinLock
|
||||
{
|
||||
public:
|
||||
#if defined(QSE_SUPPORT_SPL)
|
||||
SpinLock() QSE_CPP_NOEXCEPT: flag(QSE_SPL_INIT) {}
|
||||
#else
|
||||
SpinLock() QSE_CPP_NOEXCEPT: flag(0) {}
|
||||
#endif
|
||||
|
||||
bool tryock() QSE_CPP_NOEXCEPT
|
||||
{
|
||||
#if defined(QSE_HAVE_SYNC_LOCK_TEST_AND_SET) && defined(QSE_HAVE_SYNC_LOCK_RELEASE)
|
||||
return !__sync_lock_test_and_set(&this->flag, 1);
|
||||
#if defined(QSE_SUPPORT_SPL)
|
||||
return !qse_spl_trylock(&this->flag);
|
||||
#elif defined(QSE_LANG_CPP11)
|
||||
return !this->flag.test_and_set();
|
||||
#else
|
||||
@ -58,8 +67,8 @@ public:
|
||||
|
||||
void lock () QSE_CPP_NOEXCEPT
|
||||
{
|
||||
#if defined(QSE_HAVE_SYNC_LOCK_TEST_AND_SET) && defined(QSE_HAVE_SYNC_LOCK_RELEASE)
|
||||
while (__sync_lock_test_and_set(&this->flag, 1)) { /* do nothing special */ }
|
||||
#if defined(QSE_SUPPORT_SPL)
|
||||
qse_spl_lock (&this->flag);
|
||||
#elif defined(QSE_LANG_CPP11)
|
||||
while (flag.test_and_set()) { /* do nothing sepcial */ }
|
||||
#else
|
||||
@ -69,8 +78,8 @@ public:
|
||||
|
||||
void unlock () QSE_CPP_NOEXCEPT
|
||||
{
|
||||
#if defined(QSE_HAVE_SYNC_LOCK_TEST_AND_SET) && defined(QSE_HAVE_SYNC_LOCK_RELEASE)
|
||||
__sync_lock_release (&this->flag);
|
||||
#if defined(QSE_SUPPORT_SPL)
|
||||
qse_spl_unlock (&this->flag);
|
||||
#elif defined(QSE_LANG_CPP11)
|
||||
flag.clear ();
|
||||
#else
|
||||
@ -79,8 +88,8 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
#if defined(QSE_HAVE_SYNC_LOCK_TEST_AND_SET) && defined(QSE_HAVE_SYNC_LOCK_RELEASE)
|
||||
volatile int flag;
|
||||
#if defined(QSE_SUPPORT_SPL)
|
||||
qse_spl_t flag;
|
||||
#elif defined(QSE_LANG_CPP11)
|
||||
std::atomic_flag flag;
|
||||
#else
|
||||
|
147
qse/include/qse/si/spl.h
Normal file
147
qse/include/qse/si/spl.h
Normal file
@ -0,0 +1,147 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EQSERESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _QSE_SI_SPL_H_
|
||||
#define _QSE_SI_SPL_H_
|
||||
|
||||
#include <qse/types.h>
|
||||
#include <qse/macros.h>
|
||||
|
||||
#define QSE_SUPPORT_SPL
|
||||
|
||||
typedef volatile qse_uint32_t qse_spl_t;
|
||||
|
||||
#define QSE_SPL_INIT (0)
|
||||
|
||||
#if defined(QSE_HAVE_INLINE)
|
||||
static QSE_INLINE void qse_spl_init (qse_spl_t* spl) { *spl = QSE_SPL_INIT; }
|
||||
#else
|
||||
# define qse_spl_init(spl) ((*(spl)) = QSE_SPL_INIT)
|
||||
#endif
|
||||
|
||||
#if defined(QSE_HAVE_SYNC_LOCK_TEST_AND_SET) && defined(QSE_HAVE_SYNC_LOCK_RELEASE)
|
||||
/* =======================================================================
|
||||
* MODERN COMPILERS WITH BUILTIN ATOMICS
|
||||
* ======================================================================= */
|
||||
|
||||
#if defined(QSE_HAVE_INLINE)
|
||||
static QSE_INLINE int qse_spl_trylock (qse_spl_t* spl) { return !__sync_lock_test_and_set(spl, 1); }
|
||||
static QSE_INLINE void qse_spl_lock (qse_spl_t* spl) { do {} while(__sync_lock_test_and_set(spl, 1)); }
|
||||
static QSE_INLINE void qse_spl_unlock (qse_spl_t* spl) { __sync_lock_release(spl); }
|
||||
#else
|
||||
# define qse_spl_trylock(spl) (!__sync_lock_test_and_set(spl, 1))
|
||||
# define qse_spl_lock(spl) do {} while(__sync_lock_test_and_set(spl, 1))
|
||||
# define qse_spl_unlock(spl) (__sync_lock_release(spl))
|
||||
#endif
|
||||
|
||||
#elif defined(_SCO_DS)
|
||||
/* =======================================================================
|
||||
* SCO DEVELOPEMENT SYSTEM
|
||||
*
|
||||
* NOTE: when the asm macros were indented, the compiler/linker ended up
|
||||
* with undefined symbols. never indent qse_spl_xxx macros.
|
||||
* ======================================================================= */
|
||||
asm int qse_spl_trylock (qse_spl_t* spl)
|
||||
{
|
||||
%reg spl
|
||||
movl $1, %eax
|
||||
xchgl (spl), %eax
|
||||
xorl $1, %eax / return zero on failure, non-zero on success
|
||||
|
||||
%mem spl
|
||||
movl spl, %ecx
|
||||
movl $1, %eax
|
||||
xchgl (%ecx), %eax
|
||||
xorl $1, %eax / return zero on failure, non-zero on success
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* i can't figure out how to make jump labels unique when there are
|
||||
* multiple occurrences of qse_spl_lock(). so let me just use the while loop
|
||||
* instead. */
|
||||
asm void qse_spl_lock (qse_spl_t* spl)
|
||||
{
|
||||
%reg spl
|
||||
.lock_set_loop:
|
||||
movl $1, %eax
|
||||
xchgl (spl), %eax
|
||||
testl %eax, %eax / set ZF to 1 if eax is zero, 0 if eax is non-zero
|
||||
jne .lock_set_loop / if ZF is 0(eax is non-zero), loop around
|
||||
|
||||
%mem spl
|
||||
.lock_set_loop:
|
||||
movl spl, %ecx
|
||||
movl $1, %eax
|
||||
xchgl (%ecx), %eax
|
||||
testl %eax, %eax / set ZF to 1 if eax is zero, 0 if eax is non-zero
|
||||
jne .lock_set_loop / if ZF is 0(eax is non-zero), loop around
|
||||
}
|
||||
#else
|
||||
#define qse_spl_lock(x) do {} while(!spl_trylock(x))
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
asm void qse_spl_unlock (moo_uint8_t* spl)
|
||||
{
|
||||
%reg spl
|
||||
movl $0, %eax
|
||||
xchgl (spl), %eax
|
||||
|
||||
%mem spl
|
||||
movl spl, %ecx
|
||||
movl $0, %eax
|
||||
xchgl (%ecx), %eax
|
||||
}
|
||||
#else
|
||||
asm void qse_spl_unlock (qse_spl_t* spl)
|
||||
{
|
||||
/* don't need xchg as movl on an aligned data is atomic */
|
||||
/* mfence is 0F AE F0 */
|
||||
%reg spl
|
||||
.byte 0x0F
|
||||
.byte 0xAE
|
||||
.byte 0xF0
|
||||
movl $0, (spl)
|
||||
|
||||
%mem spl
|
||||
.byte 0x0F
|
||||
.byte 0xAE
|
||||
.byte 0xF0
|
||||
movl spl, %ecx
|
||||
movl $0, (%ecx)
|
||||
}
|
||||
#endif
|
||||
|
||||
#elif defined(QSE_SPL_NO_UNSUPPORTED_ERROR)
|
||||
/* don't raise the compile time error */
|
||||
#undef QSE_SUPPORT_SPL
|
||||
#else
|
||||
#undef QSE_SUPPORT_SPL
|
||||
# error UNSUPPORTED
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user