Skip to content

Commit

Permalink
Add semaphore APIs for benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
kar-rahul-aws committed Jan 17, 2025
1 parent 1d9f239 commit 8d6c46c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/light-weight-mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void vRemoveMutexItemFromList( void * pvMutexHandle );
BaseType_t lightMutexTake( LightWeightMutex_t * xMutex,
TickType_t xTicksToWait );
BaseType_t lightMutexGive( LightWeightMutex_t * xMutex );
BaseType_t lightSemaphoreTake( LightWeightMutex_t * xMutex,
TickType_t xTicksToWait );
BaseType_t lightSemaphoreGive( LightWeightMutex_t * xMutex );

/* *INDENT-OFF* */
#if defined( __cplusplus )
Expand Down
112 changes: 102 additions & 10 deletions light-weight-mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
{
TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
BaseType_t xReturn = pdFALSE;
BaseType_t xInheritanceOccurred = pdFALSE;
TickType_t startTime = xTaskGetTickCount();

/* Check the pxMutex pointer is not NULL. */
Expand All @@ -51,7 +52,6 @@
pxMutex->lock_count = 1;
if( uxTaskPriorityGet( currentTask ) < pxMutex->uxCeilingPriority )
{
BaseType_t xInheritanceOccurred = pdFALSE;
vInsertMutexToHolderList( currentTask, &( pxMutex->xMutexHolderListItem ) );
xInheritanceOccurred = xTaskCeilingPriorityInherit( pxMutex->uxCeilingPriority );
}
Expand All @@ -77,16 +77,16 @@
/* Get the ceiling priority of next mutex held.
* If it not there set to base priority.
*/
// LightWeightMutex_t * pxNextMutex = pvRemoveMutexToHolderList( ( void * const ) pxMutex );
LightWeightMutex_t * pxNextMutex = pvRemoveMutexToHolderList( ( void * const ) pxMutex );

// if( pxNextMutex != NULL )
// {
// xTaskCeilingPriorityDisInherit( pxNextMutex->uxCeilingPriority );
// }
// else
// {
// xTaskCeilingPriorityDisInheritToBasePrio();
// }
if( pxNextMutex != NULL )
{
xTaskCeilingPriorityDisInherit( pxNextMutex->uxCeilingPriority );
}
else
{
xTaskCeilingPriorityDisInheritToBasePrio();
}

xReturn = pdFALSE;
taskEXIT_CRITICAL();
Expand Down Expand Up @@ -154,6 +154,98 @@
return xReturn;
}

/*-----------------------------------------------------------*/

BaseType_t lightSemaphoreTake( LightWeightMutex_t * pxMutex, TickType_t xTicksToWait )
{
TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
BaseType_t xReturn = pdFALSE;
TickType_t startTime = xTaskGetTickCount();

/* Check the pxMutex pointer is not NULL. */
configASSERT( ( pxMutex != NULL ) && ( xTicksToWait != 0U ) );

for( ; ; )
{
taskENTER_CRITICAL();
{
if( pxMutex->lock_count == 0U )
{
/*Atomic_Store_u32( &pxMutex->owner, ( uintptr_t ) currentTask ); */
pxMutex->owner = ( uintptr_t ) currentTask;
pxMutex->lock_count = 1;
xReturn = pdTRUE;
taskEXIT_CRITICAL();
break;
}

/*if( ( uintptr_t ) Atomic_Load_u32( &pxMutex->owner ) == ( uintptr_t ) currentTask ) */
if( pxMutex->owner == ( uintptr_t ) currentTask )
{
pxMutex->lock_count++;
xReturn = pdTRUE;
taskEXIT_CRITICAL();
break;
}

if( xTicksToWait != portMAX_DELAY )
{
/* Timed out */
if( ( xTaskGetTickCount() - startTime ) >= xTicksToWait )
{
xReturn = pdFALSE;
taskEXIT_CRITICAL();
break;
}
}

vTaskPlaceOnEventList( &( pxMutex->xTasksWaitingForMutex ), xTicksToWait );
taskYIELD();
}
taskEXIT_CRITICAL();
}

return xReturn;
}

/*-----------------------------------------------------------*/

BaseType_t lightSemaphoreGive( LightWeightMutex_t * pxMutex )
{
/* Check the pxMutex pointer is not NULL and the mutex has already been taken earlier. */
BaseType_t xReturn = pdTRUE;

configASSERT( ( pxMutex != NULL ) || ( pxMutex->lock_count != 0U ) );

/*if( ( uintptr_t ) Atomic_Load_u32( &pxMutex->owner ) != ( uintptr_t ) xTaskGetCurrentTaskHandle() ) */
if( pxMutex->owner != ( uintptr_t ) xTaskGetCurrentTaskHandle() )
{
xReturn = pdFALSE;
}
else
{
taskENTER_CRITICAL();
{
pxMutex->lock_count--;

if( pxMutex->lock_count == 0U )
{
/* Update the current owner of the mutex to 0. */

/*Atomic_Store_u32( &pxMutex->owner, ( uintptr_t ) 0U ); */
pxMutex->owner = ( uintptr_t ) 0U;
/* The mutex is no longer being held. */
xReturn = pdTRUE;
/* Get the new owner, if any. */
prvAssignLWMutexOwner( pxMutex );
}
}
taskEXIT_CRITICAL();
}

return xReturn;
}

/*-----------------------------------------------------------*/

/* This entire source file will be skipped if the application is not configured
Expand Down

0 comments on commit 8d6c46c

Please sign in to comment.