请打印freertos中关于实现基于优先级的互斥信号量的代码,并加以注释
时间: 2023-06-02 11:02:22 浏览: 161
下面是FreeRTOS中实现基于优先级的互斥信号量的代码,注释已经加上:
```
typedef struct xQUEUE { // 定义队列结构体
volatile unsigned portBASE_TYPE uxMessagesWaiting;
// 队列中等待的消息数目
List_t xWaitingTasks; // 等待队列
} xQUEUE;
typedef struct xTIME_OUT { // 定义超时结构体
portTickType xTicksToWait; // 等待时间
portTickType xTimeOnEntering; // 进入等待状态时的时间
} xTIME_OUT;
typedef struct xSEM { // 定义信号量结构体
xQUEUE xTasksWaitingToSend; // 任务等待发送信号量
xQUEUE xTasksWaitingToReceive; // 任务等待接收信号量
volatile unsigned portBASE_TYPE uxSemaphoreCount; // 信号量计数
} xSemaphore;
#define queueQUEUE_TYPE_BASE ( 0U )
#define queueQUEUE_TYPE_MUTEX ( 1U << 1 ) // 定义互斥信号量类型
#define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( 2U << 1 )
#define queueQUEUE_TYPE_BINARY_SEMAPHORE ( 3U << 1 )
#define queueQUEUE_TYPE_RECURSIVE_MUTEX ( 4U << 1 )
#define queueQUEUE_TYPE_SET ( 5U << 1 )
#define queueQUEUE_TYPE_IS_MUTEX( pxQueue ) ( ( ( pxQueue )->ucQueueType & queueQUEUE_TYPE_MUTEX ) != 0 ) // 判断队列是否是互斥信号量类型
#define queueQUEUE_TYPE_IS_SEMAPHORE( pxQueue ) ( ( ( pxQueue )->ucQueueType & queueQUEUE_TYPE_MUTEX ) == 0 )
#define queueSET_QUEUE_TYPE( pxQueue, uxType ) ( ( pxQueue )->ucQueueType = ( uxType ) )
#define queueGET_MUTEX_OWNER( pxQueue ) ( ( pxQueue )->pxMutexHolder )
#define queueSET_MUTEX_OWNER( pxQueue, pxHolder ) ( ( pxQueue )->pxMutexHolder = ( pxHolder ) )
#define queueYIELD_FROM_ISR( x ) { if( x == pdTRUE ) { portEND_SWITCHING_ISR( pdTRUE ); } }
BaseType_t xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait ) // 获取互斥信号量
{
BaseType_t xReturn = pdFALSE;
xSemaphore = prvGetMutexSemaphoreHandle( xSemaphore ); // 获取信号量句柄
configASSERT( xSemaphore );
if( xSemaphore != NULL ) {
if( xSemaphore->uxSemaphoreCount == 0 ) {
if( xTicksToWait == ( TickType_t ) 0 ) {
/* The semaphore was not immediately available and no block time
* was specified (or the block time has expired) so simply return
* false to indicate that the take could not be accomplished. */
xReturn = pdFALSE;
}
else {
if( xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED ) {
/* Schedule has not started yet, so just wait in a loop until
semaphore becomes available. This is a special case that only
arises during the startup of the scheduler. */
while( xSemaphore->uxSemaphoreCount == 0 ) {
asm volatile ( "NOP" );
}
xSemaphore->uxSemaphoreCount--; // 获取信号量
xReturn = pdTRUE;
}
else {
vTaskPlaceOnEventList( &( xSemaphore->xTasksWaitingToReceive ), xTicksToWait );
tracePEND_SEMAPHORE_TAKE( xSemaphore );
portENABLE_INTERRUPTS();
if( xSemaphore->uxSemaphoreCount == 0 ) {
/* The semaphore was not available immediately so the
calling task must have been placed in the queue of tasks that
are waiting for the semaphore to become available. It must
now block to wait for the semaphore to become available. */
configASSERT( ( ( volatile uOS32_t * ) &( xSemaphore->uxSemaphoreCount ) )->ucCount == 0U );
portRESET_READY_PRIORITY( pxCurrentTCB );
prvAddCurrentTaskToDelayedList( &( xSemaphore->xTasksWaitingToReceive ), xTicksToWait, pdFALSE );
portYIELD_WITHIN_API(); // 切换到其他任务
if( pxCurrentTCB->uxMutexesHeld == 0U ) {
xReturn = pdTRUE;
}
else {
xReturn = pdFALSE;
}
}
else {
/* The semaphore was obtained. */
( xSemaphore->uxSemaphoreCount )--;
traceSEMAPHORE_TAKE( xSemaphore );
prvUpdateTickCounts();
xReturn = pdTRUE;
}
portDISABLE_INTERRUPTS();
}
}
}
else {
/* The semaphore was obtained. */
( xSemaphore->uxSemaphoreCount )--;
traceSEMAPHORE_TAKE( xSemaphore );
prvUpdateTickCounts();
xReturn = pdTRUE;
}
if( xReturn == pdTRUE ) {
queueSET_MUTEX_OWNER( &( xSemaphore->xTasksWaitingToSend ), pxCurrentTCB ); // 设置当前任务为互斥信号量的持有者
}
}
return xReturn;
}
void vSemaphoreCreateMutex( SemaphoreHandle_t xMutex ) // 创建互斥信号量
{
xSemaphore *pxNewQueue;
pxNewQueue = ( xSemaphore * ) pvPortMalloc( sizeof( xSemaphore ) );
if( pxNewQueue != NULL ) {
memset( pxNewQueue, 0x00, sizeof( xSemaphore ) );
/* The semaphore count starts at 1 as it is assumed the semaphore
* is being created to guard access to a resource. */
pxNewQueue->uxSemaphoreCount = ( unsigned portBASE_TYPE ) 1;
/* The semaphore is a mutex, so the queue can only hold one item. */
pxNewQueue->xTasksWaitingToSend.uxMessagesWaiting = ( unsigned portBASE_TYPE ) 1;
queueSET_QUEUE_TYPE( &( pxNewQueue->xTasksWaitingToSend ), queueQUEUE_TYPE_MUTEX ); // 设置队列类型为互斥信号量类型
/* Store the handle of the mutex in the TCB of the creating task so the
* handle gets cleaned up when the task is deleted. */
vListInitialise( &( pxNewQueue->xTasksWaitingToSend.xWaitingTasks ) ); // 初始化等待队列
vListInitialise( &( pxNewQueue->xTasksWaitingToReceive.xWaitingTasks ) ); // 初始化等待队列
vListInitialise( &( pxNewQueue->xTasksWaitingToSend.xTasksWaitingToSend ) ); // 初始化等待队列
vListInitialise( &( pxNewQueue->xTasksWaitingToReceive.xTasksWaitingToSend ) ); // 初始化等待队列
*( SemaphoreHandle_t * ) xMutex = pxNewQueue; // 设置互斥信号量的句柄
traceSEMAPHORE_CREATE( xMutex ); // 跟踪互斥信号量的创建
}
}
```
阅读全文