Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #516, Use absolute timeout for timedlock calls #518

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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