2024-12-01 19:35:24 +00:00
|
|
|
//| Copyright: (C) 2020-2024 Kevin Larke <contact AT larke DOT org>
|
|
|
|
//| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
|
2020-03-04 19:05:54 +00:00
|
|
|
#include "cwCommon.h"
|
|
|
|
#include "cwLog.h"
|
|
|
|
#include "cwCommonImpl.h"
|
2024-05-29 16:36:57 +00:00
|
|
|
#include "cwTest.h"
|
2020-03-04 19:05:54 +00:00
|
|
|
#include "cwMem.h"
|
|
|
|
#include "cwMutex.h"
|
|
|
|
#include "cwTime.h"
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
|
|
|
namespace mutex
|
|
|
|
{
|
|
|
|
typedef struct mutex_str
|
|
|
|
{
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cvar;
|
|
|
|
} mutex_t;
|
|
|
|
|
|
|
|
|
|
|
|
mutex_t* _handleToPtr( handle_t h )
|
|
|
|
{ return handleToPtr<handle_t,mutex_t>(h); }
|
|
|
|
|
|
|
|
rc_t _destroy( mutex_t* p )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
int sysRC;
|
|
|
|
|
|
|
|
if((sysRC = pthread_cond_destroy(&p->cvar)) != 0)
|
2021-01-20 18:07:36 +00:00
|
|
|
rc = cwLogSysError(kObjFreeFailRC,sysRC,"Thread condition var. destroy failed.");
|
2020-03-04 19:05:54 +00:00
|
|
|
else
|
|
|
|
if((sysRC = pthread_mutex_destroy(&p->mutex)) != 0)
|
2021-01-20 18:07:36 +00:00
|
|
|
rc = cwLogSysError(kObjFreeFailRC,sysRC,"Thread mutex destroy failed.");
|
2020-03-04 19:05:54 +00:00
|
|
|
else
|
|
|
|
mem::release(p);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2024-02-18 13:50:49 +00:00
|
|
|
|
|
|
|
time::spec_t _future_ms(unsigned timeout_milliseconds)
|
|
|
|
{
|
|
|
|
time::spec_t ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME,&ts);
|
|
|
|
time::advanceMs(ts,timeout_milliseconds);
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::mutex::create( handle_t& h )
|
|
|
|
{
|
|
|
|
rc_t rc;
|
|
|
|
if((rc = destroy(h)) != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
mutex_t* p = mem::allocZ<mutex_t>();
|
|
|
|
int sysRC;
|
|
|
|
|
|
|
|
if((sysRC = pthread_mutex_init(&p->mutex,NULL)) != 0 )
|
|
|
|
cwLogSysError(kObjAllocFailRC,sysRC,"Thread mutex create failed.");
|
|
|
|
else
|
|
|
|
if((sysRC = pthread_cond_init(&p->cvar,NULL)) != 0 )
|
|
|
|
cwLogSysError(kObjAllocFailRC,sysRC,"Thread Condition var. create failed.");
|
2021-01-20 18:07:36 +00:00
|
|
|
|
|
|
|
h.set(p);
|
2020-03-04 19:05:54 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::mutex::destroy( handle_t& h )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
if( !h.isValid() )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
mutex_t* p = _handleToPtr(h);
|
|
|
|
if((rc = _destroy(p)) != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
h.clear();
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::mutex::tryLock( handle_t h, bool& lockFlRef )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
mutex_t* p = _handleToPtr(h);
|
|
|
|
int sysRc = pthread_mutex_trylock(&p->mutex);
|
|
|
|
|
|
|
|
switch(sysRc)
|
|
|
|
{
|
|
|
|
case EBUSY:
|
|
|
|
lockFlRef = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
lockFlRef = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rc = cwLogSysError(kInvalidOpRC,sysRc,"Thread mutex try-lock failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-12-12 17:19:56 +00:00
|
|
|
cw::rc_t cw::mutex::lock( handle_t h, unsigned timeout_milliseconds )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
mutex_t* p = _handleToPtr(h);
|
|
|
|
int sysRc;
|
2024-02-18 13:50:49 +00:00
|
|
|
time::spec_t ts = _future_ms(timeout_milliseconds);
|
2022-12-12 17:19:56 +00:00
|
|
|
|
2024-02-10 16:49:47 +00:00
|
|
|
// Apparently timedlock depends on using CLOCK_REALTIME vs CLOCK_MONOTONIC
|
|
|
|
// so we can't call to time::current_time() which uses CLOCK_MONOTONIC.
|
|
|
|
// TODO: See this: https://stackoverflow.com/questions/14248033/clock-monotonic-and-pthread-mutex-timedlock-pthread-cond-timedwait
|
|
|
|
// and consider changing this to use CLOCK_MONOTONIC.
|
2024-02-18 13:50:49 +00:00
|
|
|
//clock_gettime(CLOCK_REALTIME,&ts);
|
|
|
|
//time::advanceMs(ts,timeout_milliseconds);
|
|
|
|
|
2022-12-12 17:19:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
switch(sysRc = pthread_mutex_timedlock(&p->mutex,&ts) )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
rc = kOkRC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ETIMEDOUT:
|
|
|
|
rc = kTimeOutRC;
|
2023-02-19 22:11:39 +00:00
|
|
|
break;
|
2022-12-12 17:19:56 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
rc = cwLogSysError(kInvalidOpRC,sysRc,"Lock failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:05:54 +00:00
|
|
|
cw::rc_t cw::mutex::lock( handle_t h )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
mutex_t* p = _handleToPtr(h);
|
|
|
|
int sysRc;
|
|
|
|
|
|
|
|
if((sysRc = pthread_mutex_lock(&p->mutex)) != 0)
|
|
|
|
rc = cwLogSysError(kInvalidOpRC,sysRc,"Lock failed.");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::mutex::unlock( handle_t h )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
mutex_t* p = _handleToPtr(h);
|
|
|
|
int sysRc;
|
|
|
|
|
|
|
|
if((sysRc = pthread_mutex_unlock(&p->mutex)) != 0)
|
|
|
|
return cwLogSysError(kInvalidOpRC,sysRc,"Unlock failed.");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::mutex::waitOnCondVar( handle_t h, bool lockThenWaitFl, unsigned timeOutMs )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
mutex_t* p = _handleToPtr(h);
|
|
|
|
int sysRC;
|
|
|
|
|
|
|
|
if( lockThenWaitFl )
|
|
|
|
if((sysRC = pthread_mutex_lock(&p->mutex)) != 0)
|
|
|
|
return cwLogSysError(kInvalidOpRC,sysRC,"Lock failed.");
|
|
|
|
|
|
|
|
// if no timeout was given ....
|
|
|
|
if( timeOutMs == 0)
|
|
|
|
{
|
|
|
|
// ... then wait until the cond-var is triggered
|
|
|
|
if((sysRC = pthread_cond_wait(&p->cvar,&p->mutex)) != 0 )
|
|
|
|
return cwLogSysError(kInvalidOpRC,sysRC,"Thread cond. var. wait failed.");
|
|
|
|
}
|
|
|
|
else // ... otherwise use the cond. var. wait with timeout API
|
|
|
|
{
|
|
|
|
|
2024-02-18 13:50:49 +00:00
|
|
|
struct timespec ts = _future_ms(timeOutMs);
|
|
|
|
|
2020-03-04 19:05:54 +00:00
|
|
|
|
2024-02-18 13:50:49 +00:00
|
|
|
//if((rc = time::futureMs(ts,timeOutMs)) == kOkRC )
|
2020-03-04 19:05:54 +00:00
|
|
|
if((sysRC = pthread_cond_timedwait(&p->cvar,&p->mutex,&ts)) != 0 )
|
|
|
|
{
|
|
|
|
if( sysRC == ETIMEDOUT )
|
|
|
|
rc = kTimeOutRC;
|
|
|
|
else
|
|
|
|
return cwLogSysError(kInvalidOpRC,sysRC,"Thread cond. var. wait failed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-01-20 18:07:36 +00:00
|
|
|
|
|
|
|
cw::rc_t cw::mutex::signalCondVar( handle_t h)
|
|
|
|
{
|
|
|
|
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
mutex_t* p = _handleToPtr(h);
|
|
|
|
|
|
|
|
int sysRC;
|
|
|
|
if((sysRC = pthread_cond_signal(&p->cvar)) != 0 )
|
|
|
|
rc = cwLogSysError(kOpFailRC,sysRC,"Thread cond var. signaling failed.");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|