syncprims.h
Go to the documentation of this file.
// -*- C++ -*-
// Copyright (C) 2010-2017, Vaclav Haisman. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, 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 ``AS IS'' AND ANY EXPRESSED 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
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, 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 LOG4CPLUS_THREAD_SYNCPRIMS_H
#define LOG4CPLUS_THREAD_SYNCPRIMS_H
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <mutex>
#include <condition_variable>
namespace log4cplus { namespace thread {
template <typename SyncPrim>
class SyncGuard
{
public:
SyncGuard ();
SyncGuard (SyncPrim const &);
~SyncGuard ();
SyncGuard (SyncGuard const &) = delete;
SyncGuard & operator = (SyncGuard const &) = delete;
void lock ();
void unlock ();
void attach (SyncPrim const &);
void attach_and_lock (SyncPrim const &);
void detach ();
private:
SyncPrim const * sp;
};
class LOG4CPLUS_EXPORT SimpleMutex
{
public:
SimpleMutex ();
~SimpleMutex ();
SimpleMutex (SimpleMutex const &) = delete;
SimpleMutex & operator = (SimpleMutex const &) = delete;
void lock () const;
bool try_lock () const;
void unlock () const;
private:
LOG4CPLUS_THREADED (mutable std::mutex mtx;)
};
typedef SyncGuard<SimpleMutex> SimpleMutexGuard;
class LOG4CPLUS_EXPORT Mutex
{
public:
Mutex ();
~Mutex ();
Mutex (Mutex const &) = delete;
Mutex & operator = (Mutex const &) = delete;
void lock () const;
void unlock () const;
private:
LOG4CPLUS_THREADED (mutable std::recursive_mutex mtx;)
};
typedef SyncGuard<Mutex> MutexGuard;
class LOG4CPLUS_EXPORT Semaphore
{
public:
Semaphore (unsigned max, unsigned initial);
~Semaphore ();
Semaphore (Semaphore const &) = delete;
Semaphore & operator = (Semaphore const &) = delete;
void lock () const;
void unlock () const;
private:
#if ! defined (LOG4CPLUS_SINGLE_THREADED)
mutable std::mutex mtx;
mutable std::condition_variable cv;
mutable unsigned maximum;
mutable unsigned val;
#endif
};
typedef SyncGuard<Semaphore> SemaphoreGuard;
class LOG4CPLUS_EXPORT ManualResetEvent
{
public:
explicit ManualResetEvent (bool = false);
~ManualResetEvent ();
ManualResetEvent (ManualResetEvent const &) = delete;
ManualResetEvent & operator = (ManualResetEvent const &) = delete;
void signal () const;
void wait () const;
bool timed_wait (unsigned long msec) const;
void reset () const;
private:
#if ! defined (LOG4CPLUS_SINGLE_THREADED)
mutable std::mutex mtx;
mutable std::condition_variable cv;
mutable bool signaled;
mutable unsigned sigcount;
#endif
};
class SharedMutexImplBase
{
protected:
~SharedMutexImplBase ();
};
template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
void (SyncPrim:: * unlock_func) () const>
class SyncGuardFunc
{
public:
SyncGuardFunc (SyncPrim const &);
~SyncGuardFunc ();
void lock ();
void unlock ();
void attach (SyncPrim const &);
void detach ();
private:
SyncPrim const * sp;
SyncGuardFunc (SyncGuardFunc const &);
SyncGuardFunc & operator = (SyncGuardFunc const &);
};
class LOG4CPLUS_EXPORT SharedMutex
{
public:
SharedMutex ();
~SharedMutex ();
void rdlock () const;
void rdunlock () const;
void wrlock () const;
void wrunlock () const;
private:
SharedMutexImplBase * sm;
SharedMutex (SharedMutex const &);
SharedMutex & operator = (SharedMutex const &);
};
typedef SyncGuardFunc<SharedMutex, &SharedMutex::rdlock,
&SharedMutex::rdunlock> SharedMutexReaderGuard;
typedef SyncGuardFunc<SharedMutex, &SharedMutex::wrlock,
&SharedMutex::wrunlock> SharedMutexWriterGuard;
//
//
//
template <typename SyncPrim>
inline
SyncGuard<SyncPrim>::SyncGuard ()
: sp (0)
{ }
template <typename SyncPrim>
inline
SyncGuard<SyncPrim>::SyncGuard (SyncPrim const & m)
: sp (&m)
{
sp->lock ();
}
template <typename SyncPrim>
inline
SyncGuard<SyncPrim>::~SyncGuard ()
{
if (sp)
sp->unlock ();
}
template <typename SyncPrim>
inline
void
SyncGuard<SyncPrim>::lock ()
{
sp->lock ();
}
template <typename SyncPrim>
inline
void
SyncGuard<SyncPrim>::unlock ()
{
sp->unlock ();
}
template <typename SyncPrim>
inline
void
SyncGuard<SyncPrim>::attach (SyncPrim const & m)
{
sp = &m;
}
template <typename SyncPrim>
inline
void
SyncGuard<SyncPrim>::attach_and_lock (SyncPrim const & m)
{
attach (m);
try
{
lock();
}
catch (...)
{
detach ();
throw;
}
}
template <typename SyncPrim>
inline
void
SyncGuard<SyncPrim>::detach ()
{
sp = 0;
}
//
//
//
template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
void (SyncPrim:: * unlock_func) () const>
inline
SyncGuardFunc<SyncPrim, lock_func, unlock_func>::SyncGuardFunc (SyncPrim const & m)
: sp (&m)
{
(sp->*lock_func) ();
}
template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
void (SyncPrim:: * unlock_func) () const>
inline
SyncGuardFunc<SyncPrim, lock_func, unlock_func>::~SyncGuardFunc ()
{
if (sp)
(sp->*unlock_func) ();
}
template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
void (SyncPrim:: * unlock_func) () const>
inline
void
SyncGuardFunc<SyncPrim, lock_func, unlock_func>::lock ()
{
(sp->*lock_func) ();
}
template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
void (SyncPrim:: * unlock_func) () const>
inline
void
SyncGuardFunc<SyncPrim, lock_func, unlock_func>::unlock ()
{
(sp->*unlock_func) ();
}
template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
void (SyncPrim:: * unlock_func) () const>
inline
void
SyncGuardFunc<SyncPrim, lock_func, unlock_func>::attach (SyncPrim const & m)
{
sp = &m;
}
template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
void (SyncPrim:: * unlock_func) () const>
inline
void
SyncGuardFunc<SyncPrim, lock_func, unlock_func>::detach ()
{
sp = 0;
}
} } // namespace log4cplus { namespace thread {
#endif // LOG4CPLUS_THREAD_SYNCPRIMS_H
log4cplus::thread::ManualResetEvent::~ManualResetEvent
~ManualResetEvent()
log4cplus::thread::ManualResetEvent::reset
void reset() const
log4cplus::thread::ManualResetEvent::ManualResetEvent
ManualResetEvent(ManualResetEvent const &)=delete
log4cplus::thread::ManualResetEvent::wait
void wait() const
log4cplus::thread::ManualResetEvent::signal
void signal() const
log4cplus::thread::ManualResetEvent::ManualResetEvent
ManualResetEvent(bool=false)
log4cplus::thread::ManualResetEvent::timed_wait
bool timed_wait(unsigned long msec) const
log4cplus::thread::Mutex::unlock
void unlock() const
log4cplus::thread::Mutex::~Mutex
~Mutex()
log4cplus::thread::Mutex::Mutex
Mutex()
log4cplus::thread::Mutex::Mutex
Mutex(Mutex const &)=delete
log4cplus::thread::Mutex::lock
void lock() const
log4cplus::thread::Semaphore::~Semaphore
~Semaphore()
log4cplus::thread::Semaphore::Semaphore
Semaphore(unsigned max, unsigned initial)
log4cplus::thread::Semaphore::Semaphore
Semaphore(Semaphore const &)=delete
log4cplus::thread::Semaphore::lock
void lock() const
log4cplus::thread::Semaphore::unlock
void unlock() const
log4cplus::thread::SharedMutexImplBase
Definition syncprims.h:149
log4cplus::thread::SharedMutexImplBase::~SharedMutexImplBase
~SharedMutexImplBase()
log4cplus::thread::SharedMutex
Definition syncprims.h:177
log4cplus::thread::SharedMutex::rdunlock
void rdunlock() const
Definition syncprims-pmsm.h:75
log4cplus::thread::SharedMutex::wrunlock
void wrunlock() const
Definition syncprims-pmsm.h:111
log4cplus::thread::SharedMutex::wrlock
void wrlock() const
Definition syncprims-pmsm.h:87
log4cplus::thread::SharedMutex::SharedMutex
SharedMutex()
Definition syncprims-pmsm.h:43
log4cplus::thread::SharedMutex::rdlock
void rdlock() const
Definition syncprims-pmsm.h:61
log4cplus::thread::SimpleMutex::unlock
void unlock() const
log4cplus::thread::SimpleMutex::SimpleMutex
SimpleMutex(SimpleMutex const &)=delete
log4cplus::thread::SimpleMutex::try_lock
bool try_lock() const
log4cplus::thread::SimpleMutex::lock
void lock() const
log4cplus::thread::SimpleMutex::SimpleMutex
SimpleMutex()
log4cplus::thread::SimpleMutex::~SimpleMutex
~SimpleMutex()
log4cplus::thread::SyncGuardFunc
Definition syncprims.h:158
log4cplus::thread::SyncGuardFunc::SyncGuardFunc
SyncGuardFunc(SyncPrim const &)
Definition syncprims.h:294
log4cplus::thread::SyncGuardFunc::detach
void detach()
Definition syncprims.h:345
log4cplus::thread::SyncGuardFunc::lock
void lock()
Definition syncprims.h:315
log4cplus::thread::SyncGuardFunc::attach
void attach(SyncPrim const &)
Definition syncprims.h:335
log4cplus::thread::SyncGuardFunc::~SyncGuardFunc
~SyncGuardFunc()
Definition syncprims.h:304
log4cplus::thread::SyncGuardFunc::unlock
void unlock()
Definition syncprims.h:325
log4cplus::thread::SyncGuard
Definition syncprims.h:43
log4cplus::thread::SyncGuard::operator=
SyncGuard & operator=(SyncGuard const &)=delete
log4cplus::thread::SyncGuard::detach
void detach()
Definition syncprims.h:281
log4cplus::thread::SyncGuard::SyncGuard
SyncGuard()
Definition syncprims.h:210
log4cplus::thread::SyncGuard::attach_and_lock
void attach_and_lock(SyncPrim const &)
Definition syncprims.h:263
log4cplus::thread::SyncGuard::lock
void lock()
Definition syncprims.h:236
log4cplus::thread::SyncGuard::unlock
void unlock()
Definition syncprims.h:245
log4cplus::thread::SyncGuard::~SyncGuard
~SyncGuard()
Definition syncprims.h:226
log4cplus::thread::SyncGuard::SyncGuard
SyncGuard(SyncPrim const &)
Definition syncprims.h:217
log4cplus::thread::SyncGuard::SyncGuard
SyncGuard(SyncGuard const &)=delete
log4cplus::thread::SyncGuard::attach
void attach(SyncPrim const &)
Definition syncprims.h:254
config.hxx
LOG4CPLUS_THREADED
#define LOG4CPLUS_THREADED(x)
Definition config.hxx:178
log4cplus::thread
Definition queue.h:42
log4cplus::thread::SharedMutexReaderGuard
SyncGuardFunc< SharedMutex, &SharedMutex::rdlock, &SharedMutex::rdunlock > SharedMutexReaderGuard
Definition syncprims.h:197
log4cplus::thread::MutexGuard
SyncGuard< Mutex > MutexGuard
Definition syncprims.h:98
log4cplus::thread::SimpleMutexGuard
SyncGuard< SimpleMutex > SimpleMutexGuard
Definition syncprims.h:79
log4cplus::thread::SharedMutexWriterGuard
SyncGuardFunc< SharedMutex, &SharedMutex::wrlock, &SharedMutex::wrunlock > SharedMutexWriterGuard
Definition syncprims.h:201
log4cplus::thread::SemaphoreGuard
SyncGuard< Semaphore > SemaphoreGuard
Definition syncprims.h:122
log4cplus
Definition appender.h:46
LOG4CPLUS_EXPORT
#define LOG4CPLUS_EXPORT
Definition win32.h:141