added an experimental SpinLock class
This commit is contained in:
@ -30,6 +30,7 @@ pkginclude_HEADERS += \
|
||||
AppRoot.hpp \
|
||||
SocketAddress.hpp \
|
||||
Socket.hpp \
|
||||
SpinLock.hpp \
|
||||
Thread.hpp
|
||||
endif
|
||||
|
||||
|
@ -91,6 +91,7 @@ host_triplet = @host@
|
||||
@ENABLE_CXX_TRUE@ AppRoot.hpp \
|
||||
@ENABLE_CXX_TRUE@ SocketAddress.hpp \
|
||||
@ENABLE_CXX_TRUE@ Socket.hpp \
|
||||
@ENABLE_CXX_TRUE@ SpinLock.hpp \
|
||||
@ENABLE_CXX_TRUE@ Thread.hpp
|
||||
|
||||
subdir = include/qse/si
|
||||
@ -133,7 +134,8 @@ am__can_run_installinfo = \
|
||||
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 Thread.hpp
|
||||
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 \
|
||||
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
|
||||
|
77
qse/include/qse/si/SpinLock.hpp
Normal file
77
qse/include/qse/si/SpinLock.hpp
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* $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_SPINLOCK_CLASS_
|
||||
#define _QSE_SI_SPINLOCK_CLASS_
|
||||
|
||||
#include <qse/Types.hpp>
|
||||
#include <qse/Uncopyable.hpp>
|
||||
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
|
||||
|
||||
class SpinLock
|
||||
{
|
||||
public:
|
||||
bool tryock()
|
||||
{
|
||||
return !__sync_lock_test_and_set(&this->flag, 1);
|
||||
}
|
||||
|
||||
void lock ()
|
||||
{
|
||||
//while (!this->tryLock());
|
||||
while (__sync_lock_test_and_set(&this->flag, 1));
|
||||
}
|
||||
|
||||
void unlock ()
|
||||
{
|
||||
__sync_lock_release (&this->flag);
|
||||
}
|
||||
|
||||
protected:
|
||||
volatile int flag;
|
||||
};
|
||||
|
||||
class ScopedSpinLock: public Uncopyable
|
||||
{
|
||||
ScopedSpinLock (SpinLock& spl): spl(spl)
|
||||
{
|
||||
this->spl.lock ();
|
||||
}
|
||||
|
||||
~ScopedSpinLock ()
|
||||
{
|
||||
this->spl.unlock ();
|
||||
}
|
||||
|
||||
protected:
|
||||
SpinLock& spl;
|
||||
};
|
||||
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user