cwMutex.h/cpp : Added lock() with a timeout option.

This commit is contained in:
kevin 2022-12-12 12:19:56 -05:00
parent a3b395df4a
commit d700a0e8b1
2 changed files with 29 additions and 0 deletions

View File

@ -97,6 +97,34 @@ cw::rc_t cw::mutex::tryLock( handle_t h, bool& lockFlRef )
return rc; return rc;
} }
cw::rc_t cw::mutex::lock( handle_t h, unsigned timeout_milliseconds )
{
rc_t rc = kOkRC;
mutex_t* p = _handleToPtr(h);
int sysRc;
time::spec_t ts;
time::get(ts);
time::advanceMs(ts,timeout_milliseconds);
switch(sysRc = pthread_mutex_timedlock(&p->mutex,&ts) )
{
case 0:
rc = kOkRC;
break;
case ETIMEDOUT:
rc = kTimeOutRC;
default:
rc = cwLogSysError(kInvalidOpRC,sysRc,"Lock failed.");
}
return rc;
}
cw::rc_t cw::mutex::lock( handle_t h ) cw::rc_t cw::mutex::lock( handle_t h )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;

View File

@ -11,6 +11,7 @@ namespace cw
rc_t destroy( handle_t& h ); rc_t destroy( handle_t& h );
rc_t tryLock( handle_t h, bool& lockFlRef ); rc_t tryLock( handle_t h, bool& lockFlRef );
rc_t lock( handle_t h, unsigned timeout_milliseconds );
rc_t lock( handle_t h ); rc_t lock( handle_t h );
rc_t unlock( handle_t h ); rc_t unlock( handle_t h );