Skip to content

Commit

Permalink
Fix #516, Use absolute timeout for timedlock calls
Browse files Browse the repository at this point in the history
The pthread API defines the timeout parameter for
the pthread_mutex_timedlock call as an absolute value
based on CLOCK_REALTIME.

This introduces a wrapper function to calculate the absolute
timeout for this.
  • Loading branch information
jphickey committed Jun 19, 2020
1 parent bc73437 commit a871dec
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/os/posix/src/os-impl-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,36 @@
* not be relevant in a normally operating system. This only prevents a
* deadlock condition in off-nominal circumstances.
*/
static const struct timespec OS_POSIX_BINSEM_MAX_WAIT =
{
.tv_sec = 2,
.tv_nsec = 0
};
#define OS_POSIX_BINSEM_MAX_WAIT_SECONDS 2


/* Tables where the OS object information is stored */
OS_impl_binsem_internal_record_t OS_impl_bin_sem_table [OS_MAX_BIN_SEMAPHORES];

/*---------------------------------------------------------------------------------------
* Helper function for acquiring the mutex when beginning a binary sem operation
* This uses timedlock to avoid waiting forever, and is put into a wrapper function
* to avoid pending forever. The code should never pend on these for a long time.
----------------------------------------------------------------------------------------*/
int32 OS_Posix_BinSemAcquireMutex(pthread_mutex_t *mut)
{
struct timespec timeout;

if (clock_gettime(CLOCK_REALTIME, &timeout) != 0)
{
return OS_SEM_FAILURE;
}

timeout.tv_sec += OS_POSIX_BINSEM_MAX_WAIT_SECONDS;

if (pthread_mutex_timedlock(mut, &timeout) != 0)
{
return OS_SEM_FAILURE;
}

return OS_SUCCESS;
}

/*---------------------------------------------------------------------------------------
* Helper function for releasing the mutex in case the thread
* executing pthread_condwait() is canceled.
Expand Down Expand Up @@ -279,7 +299,7 @@ int32 OS_BinSemGive_Impl ( uint32 sem_id )
*/

/* Lock the mutex ( not the table! ) */
if ( pthread_mutex_timedlock(&sem->id, &OS_POSIX_BINSEM_MAX_WAIT) != 0 )
if ( OS_Posix_BinSemAcquireMutex(&sem->id) != OS_SUCCESS )
{
return(OS_SEM_FAILURE);
}
Expand Down Expand Up @@ -311,7 +331,7 @@ int32 OS_BinSemFlush_Impl (uint32 sem_id)
sem = &OS_impl_bin_sem_table[sem_id];

/* Lock the mutex ( not the table! ) */
if ( pthread_mutex_timedlock(&sem->id, &OS_POSIX_BINSEM_MAX_WAIT) != 0 )
if ( OS_Posix_BinSemAcquireMutex(&sem->id) != OS_SUCCESS )
{
return(OS_SEM_FAILURE);
}
Expand Down Expand Up @@ -348,7 +368,7 @@ static int32 OS_GenericBinSemTake_Impl (OS_impl_binsem_internal_record_t *sem, c
* The main delay is in the pthread_cond_wait() below.
*/
/* Lock the mutex ( not the table! ) */
if ( pthread_mutex_timedlock(&sem->id, &OS_POSIX_BINSEM_MAX_WAIT) != 0 )
if ( OS_Posix_BinSemAcquireMutex(&sem->id) != OS_SUCCESS )
{
return(OS_SEM_FAILURE);
}
Expand Down

0 comments on commit a871dec

Please sign in to comment.