/// \brief Releases an aquired lock on the semaphore, increasing the
    ///  semaphore's counter by one.
    void unlock()
    {
        UNIXSTL_ASSERT(NULL != m_sem);

        if(::sem_post(m_sem) < 0)
        {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
            STLSOFT_THROW_X(synchronisation_exception("semaphore release failed", errno));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
        }
    }
Exemple #2
0
    /// \brief Sets the state of the event to signalled
    void reset()
    {
        WINSTL_ASSERT(NULL != m_ev);

        if(!::ResetEvent(m_ev))
        {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
            STLSOFT_THROW_X(synchronisation_exception("event reset operation failed", ::GetLastError()));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
        }
    }
    /// \brief Releases an aquired lock on the mutex
    void unlock()
    {
        WINSTL_ASSERT(NULL != m_mx);

        if(!::ReleaseMutex(m_mx))
        {
            DWORD const e = ::GetLastError();

#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
# if STLSOFT_LEAD_VER >= 0x010a0000
            STLSOFT_THROW_X(synchronisation_object_state_change_failed_exception(e, "mutex release failed", Synchronisation_MutexReleaseFailed));
# else /* ? STLSOFT_LEAD_VER >= 1.10 */
            STLSOFT_THROW_X(synchronisation_exception("mutex release failed", e));
# endif /* STLSOFT_LEAD_VER >= 1.10 */
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
        }
    }
Exemple #4
0
    /// \brief Attempts to lock the mutex
    ///
    /// \return <b>true</b> if the mutex was aquired, or <b>false</b> if not.
    ///
    /// \exception unixstl::synchronisation_exception When compiling with exception support, this will throw
    /// unixstl::synchronisation_exception if the lock cannot be acquired for a reason
    /// other than a timeout (<code>EBUSY</code>). When compiling absent
    /// exception support, failure to acquire the lock (for any other
    /// reason) will be reflected in a non-zero return from get_error().
    bool try_lock()
    {
        m_error = ::pthread_mutex_trylock(m_mx);

        if(0 == m_error)
        {
            return true;
        }
        else
        {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
            if(EBUSY != m_error)
            {
                STLSOFT_THROW_X(synchronisation_exception("Mutex try-lock failed", m_error));
            }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */

            return false;
        }
    }
    /// \brief Attempts to lock the semaphore
    ///
    /// \return <b>true</b> if the semaphore was aquired, or <b>false</b> if not
    bool_type try_lock()
    {
        UNIXSTL_ASSERT(NULL != m_sem);

        int res =   ::sem_trywait(m_sem);

        if(0 == res)
        {
            return true;
        }
        else
        {
            if(EAGAIN != res)
            {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
                STLSOFT_THROW_X(synchronisation_exception("semaphore wait failed", errno));
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
            }
        }

        return false;
    }
    /// \brief Acquires a lock on the mutex, pending the thread until the lock is aquired
    ws_bool_t lock(ws_dword_t wait)
    {
        WINSTL_ASSERT(NULL != m_mx);

        DWORD const dwRes = ::WaitForSingleObject(m_mx, wait);

        if(WAIT_ABANDONED == dwRes)
        {
            m_bAbandoned = true;

            return true;
        }
        else
        {
            m_bAbandoned = false;

            if(WAIT_TIMEOUT == dwRes)
            {
                return false;
            }
            else
            {
                if(WAIT_OBJECT_0 != dwRes)
                {
                    DWORD const e = ::GetLastError();

#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
# if STLSOFT_LEAD_VER >= 0x010a0000
                    STLSOFT_THROW_X(wait_failed_logic_exception(e, "mutex wait failed"));
# else /* ? STLSOFT_LEAD_VER >= 1.10 */
                    STLSOFT_THROW_X(synchronisation_exception("mutex wait failed", e));
# endif /* STLSOFT_LEAD_VER >= 1.10 */
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
                }

                return true;
            }
        }
    }