📄 process_mutex.hpp
字号:
WINSTL_ASSERT(NULL != m_mx);
if(!::ReleaseMutex(m_mx))
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_exception("mutex release failed", ::GetLastError()));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
}
/// @}
/// \name Accessors
/// @{
public:
/// \brief The underlying kernel object handle
HANDLE handle()
{
return m_mx;
}
/// \brief The underlying kernel object handle
HANDLE get()
{
return m_mx;
}
/// @}
/// \name Attributes
/// @{
public:
/// \brief Indicates whether this object instance created the underlying mutex object
///
/// \return true The mutex object was created by this instance
/// \return false The mutex object was not created by this instance
/// \note For unnamed mutexes this will always be false
ws_bool_t created() const
{
return m_bCreated;
}
/// \brief Indicates whether a successful call to lock occurred because the underlying
/// mutex was previously held by a thread that abandoned.
///
/// \return true The mutex object was abandoned by its previous owning thread
/// \return false The mutex object was not abandoned by its previous owning thread
/// \note This attribute is meaningful with respect to the result of the last call to lock() or try_lock(). Subsequent calls to unlock() do not affect this attribute.
ws_bool_t abandoned() const
{
return m_bAbandoned;
}
/// @}
// Implementation
private:
static HANDLE create_mutex_(LPSECURITY_ATTRIBUTES psa, ws_bool_t bInitialOwner, ws_char_a_t const* name, ws_bool_t &bCreated)
{
HANDLE mx = ::CreateMutexA(psa, bInitialOwner, name);
if(NULL == mx)
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_exception("Failed to create kernel mutex object", ::GetLastError()));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
bCreated = (mx != NULL && ::GetLastError() != ERROR_ALREADY_EXISTS);
return mx;
}
static HANDLE create_mutex_(LPSECURITY_ATTRIBUTES psa, ws_bool_t bInitialOwner, ws_char_w_t const* name, ws_bool_t &bCreated)
{
HANDLE mx = ::CreateMutexW(psa, bInitialOwner, name);
if(NULL == mx)
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_exception("Failed to create kernel mutex object", ::GetLastError()));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
bCreated = (mx != NULL && ::GetLastError() != ERROR_ALREADY_EXISTS);
return mx;
}
static HANDLE open_mutex_(ws_dword_t access, ws_bool_t bInheritHandle, ws_char_a_t const* name, ws_bool_t &bCreated)
{
HANDLE mx = ::OpenMutexA(access, bInheritHandle, name);
if(NULL == mx)
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_exception("Failed to open kernel mutex object", ::GetLastError()));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
bCreated = (mx != NULL && ::GetLastError() != ERROR_ALREADY_EXISTS);
return mx;
}
static HANDLE open_mutex_(ws_dword_t access, ws_bool_t bInheritHandle, ws_char_w_t const* name, ws_bool_t &bCreated)
{
HANDLE mx = ::OpenMutexW(access, bInheritHandle, name);
if(NULL == mx)
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_exception("Failed to open kernel mutex object", ::GetLastError()));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
bCreated = (mx != NULL && ::GetLastError() != ERROR_ALREADY_EXISTS);
return mx;
}
// Members
private:
HANDLE m_mx; // The underlying mutex object
const ws_bool_t m_bOwnHandle; // Does the instance own the handle?
ws_bool_t m_bCreated; // Did this object (thread) create the underlying mutex object?
ws_bool_t m_bAbandoned; // Did the previous owner abandon the underlying mutex object?
// Not to be implemented
private:
process_mutex(class_type const& rhs);
process_mutex& operator =(class_type const& rhs);
};
/* /////////////////////////////////////////////////////////////////////////
* Control shims
*/
/** \brief Overload of the form of the winstl::get_synch_handle() shim for
* the winstl::process_mutex type.
*
* \param mx The winstl::process_mutex instance
*
* \retval The synchronisation handle of \c mx
*/
inline HANDLE get_synch_handle(process_mutex &mx)
{
return mx.get();
}
/** \brief Overload of the form of the winstl::get_kernel_handle() shim for
* the winstl::process_mutex type.
*
* \ingroup group__library__shims__kernel_handle_attribute
*
* \param mx The winstl::process_mutex instance
*
* \retval The synchronisation handle of \c mx
*/
inline HANDLE get_kernel_handle(process_mutex &mx)
{
return mx.get();
}
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/** \brief This \ref group__concept__shims "control shim" aquires a lock on the given mutex
*
* \ingroup group__concept__shim__synchronisation_control
*
* \param mx The mutex on which to aquire the lock.
*/
inline void lock_instance(winstl_ns_qual(process_mutex) &mx)
{
mx.lock();
}
/** \brief This \ref group__concept__shims "control shim" releases a lock on the given mutex
*
* \ingroup group__concept__shim__synchronisation_control
*
* \param mx The mutex on which to release the lock
*/
inline void unlock_instance(winstl_ns_qual(process_mutex) &mx)
{
mx.unlock();
}
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
namespace winstl {
# else
namespace winstl_project {
# if defined(STLSOFT_COMPILER_IS_BORLAND)
using ::stlsoft::lock_instance;
using ::stlsoft::unlock_instance;
# endif /* compiler */
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* lock_traits
*/
// class lock_traits
/** \brief Traits for the process_mutex class
*
* \ingroup group__library__synch
*/
struct process_mutex_lock_traits
{
public:
/// The lockable type
typedef process_mutex lock_type;
typedef process_mutex_lock_traits class_type;
// Operations
public:
/// Lock the given process_mutex instance
static void lock(process_mutex &c)
{
lock_instance(c);
}
/// Unlock the given process_mutex instance
static void unlock(process_mutex &c)
{
unlock_instance(c);
}
};
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/process_mutex_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !WINSTL_INCL_WINSTL_SYNCH_HPP_PROCESS_MUTEX */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -