Fix Use absolute timeout for timedlock calls
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.
* 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.
@@ -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);
}
@@ -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);
}
@@ -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);
}
