//  // // ##### #### # # -= Threadux =-  // //  # # # ## ## Mutex.cpp  // //  # # # ##  // //  # # # ### Synchronizes thread access to mutually exclusive code sections  // //  # # # ## ##  // //  # #### # # R1 2004 by Markus Ewald  // //  // #include "Threadux/Mutex.h" #include #define WIN32_LEAN_AND_MEAN #include using namespace Threadux; // ############################################################################################# // // # Threadux::Mutex::Mutex() # // // ############################################################################################# // Mutex::Mutex() : m_pImpl(reinterpret_cast(::CreateMutex(NULL, FALSE, NULL))) { if(!m_pImpl) throw std::runtime_error("Failed to create Win32 mutex"); } // ############################################################################################# // // # Threadux::Mutex::~Mutex() # // // ############################################################################################# // Mutex::~Mutex() { ::CloseHandle(reinterpret_cast(m_pImpl)); } // ############################################################################################# // // # Threadux::ScopedUser::ScopedUser() # // // ############################################################################################# // Mutex::ScopedUser::ScopedUser(Mutex &TheMutex, size_t Timeout) : m_pMutexes(&TheMutex), m_Count(1), m_bLeft(true) { HANDLE hMutex = reinterpret_cast(TheMutex.getImpl()); switch(::WaitForSingleObject(hMutex, static_cast(Timeout))) { case WAIT_OBJECT_0: break; case WAIT_TIMEOUT: throw std::runtime_error("Timed out while waiting for mutex"); case WAIT_ABANDONED: throw std::logic_error("Mutex has been destroyed"); default: throw -1; } m_bLeft = false; } // ############################################################################################# // // # Threadux::ScopedUser::ScopedUser() # // // ############################################################################################# // Mutex::ScopedUser::ScopedUser(Mutex *pTheMutexes, size_t Count, size_t Timeout) : m_pMutexes(pTheMutexes), m_Count(Count), m_bLeft(true) { std::vector Mutexes(Count); for(size_t MutexIndex = 0; MutexIndex < Count; ++MutexIndex) Mutexes[MutexIndex] = reinterpret_cast(pTheMutexes[MutexIndex].getImpl()); switch(::WaitForMultipleObjects( static_cast(Count), &Mutexes[0], TRUE, static_cast(Timeout) )) { case WAIT_OBJECT_0: break; case WAIT_TIMEOUT: throw std::runtime_error("Timed out while waiting for mutex"); case WAIT_ABANDONED: throw std::logic_error("Mutex has been destroyed"); default: throw -1; } m_bLeft = false; } // ############################################################################################# // // # Threadux::ScopedUser::leave() # // // ############################################################################################# // void Mutex::ScopedUser::leave() { if(!m_bLeft) { for(size_t MutexIndex = 0; MutexIndex < m_Count; ++MutexIndex) ::ReleaseMutex(reinterpret_cast(m_pMutexes[MutexIndex].getImpl())); m_bLeft = true; } }