Skip to content

Commit e6b133f

Browse files
authored
Update pthread_cond_timedwait to cope with clock failure (#33)
1 parent d761d18 commit e6b133f

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_cond.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,38 +249,38 @@ int pthread_cond_timedwait( pthread_cond_t * cond,
249249
iLocalWaitingThreads = Atomic_Increment_u32( ( uint32_t * ) &pxCond->iWaitingThreads );
250250

251251
iStatus = pthread_mutex_unlock( mutex );
252-
}
253252

254-
/* Wait on the condition variable. */
255-
if( iStatus == 0 )
256-
{
257-
if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore,
258-
xDelay ) == pdPASS )
253+
/* Wait on the condition variable. */
254+
if( iStatus == 0 )
259255
{
260-
/* When successful, relock mutex. */
261-
iStatus = pthread_mutex_lock( mutex );
256+
if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore,
257+
xDelay ) == pdPASS )
258+
{
259+
/* When successful, relock mutex. */
260+
iStatus = pthread_mutex_lock( mutex );
261+
}
262+
else
263+
{
264+
/* Timeout. Relock mutex and decrement number of waiting threads. */
265+
iStatus = ETIMEDOUT;
266+
( void ) pthread_mutex_lock( mutex );
267+
268+
/* Atomically decrements thread waiting by 1.
269+
* If iLocalWaitingThreads is updated by other thread(s) in between,
270+
* this implementation guarantees to decrement by 1 based on the
271+
* value currently in pxCond->iWaitingThreads. */
272+
prvTestAndDecrement( pxCond, iLocalWaitingThreads + 1 );
273+
}
262274
}
263275
else
264276
{
265-
/* Timeout. Relock mutex and decrement number of waiting threads. */
266-
iStatus = ETIMEDOUT;
267-
( void ) pthread_mutex_lock( mutex );
268-
269277
/* Atomically decrements thread waiting by 1.
270278
* If iLocalWaitingThreads is updated by other thread(s) in between,
271279
* this implementation guarantees to decrement by 1 based on the
272280
* value currently in pxCond->iWaitingThreads. */
273281
prvTestAndDecrement( pxCond, iLocalWaitingThreads + 1 );
274282
}
275283
}
276-
else
277-
{
278-
/* Atomically decrements thread waiting by 1.
279-
* If iLocalWaitingThreads is updated by other thread(s) in between,
280-
* this implementation guarantees to decrement by 1 based on the
281-
* value currently in pxCond->iWaitingThreads. */
282-
prvTestAndDecrement( pxCond, iLocalWaitingThreads + 1 );
283-
}
284284

285285
return iStatus;
286286
}

0 commit comments

Comments
 (0)