added the Condition class

This commit is contained in:
2018-07-16 04:18:02 +00:00
parent b510d11c55
commit a346d27d5f
8 changed files with 286 additions and 7 deletions

View File

@ -0,0 +1,68 @@
/*
* $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_CONDITION_CLASS_
#define _QSE_SI_CONDITION_CLASS_
#include <qse/si/cnd.h>
#include <qse/si/Mutex.hpp>
QSE_BEGIN_NAMESPACE(QSE)
class Condition: public Uncopyable
{
public:
Condition() QSE_CPP_NOEXCEPT
{
qse_cnd_init (&this->cnd, QSE_NULL);
}
~Condition() QSE_CPP_NOEXCEPT
{
qse_cnd_fini (&this->cnd);
}
void signal ()
{
qse_cnd_signal (&this->cnd);
}
void broadcast ()
{
qse_cnd_broadcast (&this->cnd);
}
void wait (Mutex& mtx, const qse_ntime_t* timeout = QSE_NULL)
{
qse_cnd_wait (&this->cnd, &mtx.mtx, timeout);
}
protected:
qse_cnd_t cnd;
};
QSE_END_NAMESPACE(QSE)
#endif

View File

@ -30,6 +30,7 @@ pkginclude_HEADERS = \
if ENABLE_CXX
pkginclude_HEADERS += \
AppRoot.hpp \
Condition.hpp \
Mutex.hpp \
SocketAddress.hpp \
Socket.hpp \

View File

@ -89,6 +89,7 @@ build_triplet = @build@
host_triplet = @host@
@ENABLE_CXX_TRUE@am__append_1 = \
@ENABLE_CXX_TRUE@ AppRoot.hpp \
@ENABLE_CXX_TRUE@ Condition.hpp \
@ENABLE_CXX_TRUE@ Mutex.hpp \
@ENABLE_CXX_TRUE@ SocketAddress.hpp \
@ENABLE_CXX_TRUE@ Socket.hpp \
@ -135,8 +136,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 os.h pio.h rwl.h sck.h sinfo.h sio.h spl.h task.h thr.h \
tio.h AppRoot.hpp Mutex.hpp SocketAddress.hpp Socket.hpp \
SpinLock.hpp TcpServer.hpp Thread.hpp
tio.h AppRoot.hpp Condition.hpp Mutex.hpp SocketAddress.hpp \
Socket.hpp SpinLock.hpp TcpServer.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/||"`;; \

View File

@ -36,6 +36,8 @@ QSE_BEGIN_NAMESPACE(QSE)
class Mutex: public Uncopyable
{
public:
friend class Condition;
Mutex() QSE_CPP_NOEXCEPT
{
qse_mtx_init (&this->mtx, QSE_NULL);

View File

@ -134,6 +134,11 @@ QSE_EXPORT void qse_cnd_broadcast (
qse_cnd_t* cond
);
/**
* The qse_cnd_wait() function blocks the calling thread until the condition
* variable is signaled. The caller must lock the mutex before calling this
* function and unlock the mutex after this function finishes.
*/
QSE_EXPORT void qse_cnd_wait (
qse_cnd_t* cond,
qse_mtx_t* mutex,