[ wxwindows-Patches-1671637 ] Update for wxMutex

SourceForge.net noreply at sourceforge.net
Thu Mar 1 03:29:16 PST 2007


Patches item #1671637, was opened at 2007-03-01 14:29
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=309863&aid=1671637&group_id=9863

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Thread
Group: new feature
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Aleksandr (n_aleksandr)
Assigned to: Nobody/Anonymous (nobody)
Summary: Update for wxMutex

Initial Comment:
Add in wxMutex class function LockTimeout(unsigned long ms); This function lock mutex by timeout.

Function code:
wxMutexError wxMutex::LockTimeout(unsigned long ms)
{
    wxCHECK_MSG( m_internal, wxMUTEX_INVALID,
                 _T("wxMutex::Lock(): not initialized") );

    return m_internal->Lock(ms);
}


In class wxMutexInternal was add function
wxMutexError wxMutexInternal::Lock(unsigned long ms);

In wxMSW this function call private function LockTimeout.

In Unix-version used POSIX function pthread_mutex_timedlock. For check of the return code from pthread_mutex_lock or pthread_mutex_timedlock
add in wxMutexInternal function
private:
	wxMutexError HandleResult(int err)

In this function was moved code from wxMutexError wxMutexInternal::Lock();

Code of changed functions in Unix-version (in src/unix/threadpsx.cpp file):

wxMutexError wxMutexInternal::Lock()
{
    return HandleResult(pthread_mutex_lock(&m_mutex));
}

wxMutexError wxMutexInternal::Lock(unsigned long ms)
{
    static const long MSEC_IN_SEC   =       1000;
    static const long NSEC_IN_MSEC  =       1000000;
    static const long NSEC_IN_SEC   =       MSEC_IN_SEC * NSEC_IN_MSEC;

    time_t seconds=ms/MSEC_IN_SEC;
    long nanoseconds=(ms % MSEC_IN_SEC) * NSEC_IN_MSEC;
    timespec ts = { 0, 0 };

    if (clock_gettime(0,&ts)==0) {
        ts.tv_sec+=seconds;
        ts.tv_nsec+=nanoseconds;
        if (ts.tv_nsec>NSEC_IN_SEC) {
            ts.tv_sec+=1;
            ts.tv_nsec-=NSEC_IN_SEC;
        }
    }
    else {
            perror("clock_gettime() failed");
            ts.tv_sec=time(0)+seconds;
            ts.tv_nsec=nanoseconds;
    }

    return HandleResult(pthread_mutex_timedlock(&m_mutex,&ts));
}

wxMutexError wxMutexInternal::HandleResult(int err)
{
    switch ( err )
    {
        case EDEADLK:
            // only error checking mutexes return this value and so it's an
            // unexpected situation -- hence use assert, not wxLogDebug
            wxFAIL_MSG( _T("mutex deadlock prevented\n") );
            return wxMUTEX_DEAD_LOCK;

        case EINVAL:
            wxLogDebug(_T("pthread_mutex_lock() or pthread_mutex_timedlock(): mutex not initialized.\n"));
            break;

        case ETIMEDOUT:
            wxLogDebug(_T("pthread_mutex_timedlock(): The mutex could not be locked before the specified timeout expired.\n"));
            break;

        case 0:
            return wxMUTEX_NO_ERROR;

        default:
            wxLogApiError(_T("pthread_mutex_timedlock() or pthread_mutex_lock()"), err);
    }

    return wxMUTEX_MISC_ERROR;
}


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=309863&aid=1671637&group_id=9863




More information about the wx-dev mailing list