📄 queue.c
字号:
/* Comments regarding mutual exclusion as per those within
xQueueGiveMutexRecursive(). */
traceTAKE_MUTEX_RECURSIVE( pxMutex );
if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
{
( pxMutex->uxRecursiveCallCount )++;
xReturn = pdPASS;
}
else
{
xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
/* pdPASS will only be returned if we successfully obtained the mutex,
we may have blocked to reach here. */
if( xReturn == pdPASS )
{
( pxMutex->uxRecursiveCallCount )++;
}
}
return xReturn;
}
#endif /* configUSE_RECURSIVE_MUTEXES */
/*-----------------------------------------------------------*/
#if configUSE_COUNTING_SEMAPHORES == 1
xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
{
xQueueHandle pxHandle;
pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
if( pxHandle != NULL )
{
pxHandle->uxMessagesWaiting = uxInitialCount;
traceCREATE_COUNTING_SEMAPHORE();
}
else
{
traceCREATE_COUNTING_SEMAPHORE_FAILED();
}
return pxHandle;
}
#endif /* configUSE_COUNTING_SEMAPHORES */
/*-----------------------------------------------------------*/
signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
{
signed portBASE_TYPE xReturn = pdTRUE;
xTimeOutType xTimeOut;
do
{
/* If xTicksToWait is zero then we are not going to block even
if there is no room in the queue to post. */
if( xTicksToWait > ( portTickType ) 0 )
{
vTaskSuspendAll();
prvLockQueue( pxQueue );
if( xReturn == pdTRUE )
{
/* This is the first time through - we need to capture the
time while the scheduler is locked to ensure we attempt to
block at least once. */
vTaskSetTimeOutState( &xTimeOut );
}
if( prvIsQueueFull( pxQueue ) )
{
/* Need to call xTaskCheckForTimeout again as time could
have passed since it was last called if this is not the
first time around this loop. */
if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
{
traceBLOCKING_ON_QUEUE_SEND( pxQueue );
vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
/* Unlocking the queue means queue events can effect the
event list. It is possible that interrupts occurring now
remove this task from the event list again - but as the
scheduler is suspended the task will go onto the pending
ready last instead of the actual ready list. */
prvUnlockQueue( pxQueue );
/* Resuming the scheduler will move tasks from the pending
ready list into the ready list - so it is feasible that this
task is already in a ready list before it yields - in which
case the yield will not cause a context switch unless there
is also a higher priority task in the pending ready list. */
if( !xTaskResumeAll() )
{
taskYIELD();
}
}
else
{
prvUnlockQueue( pxQueue );
( void ) xTaskResumeAll();
}
}
else
{
/* The queue was not full so we can just unlock the
scheduler and queue again before carrying on. */
prvUnlockQueue( pxQueue );
( void ) xTaskResumeAll();
}
}
/* Higher priority tasks and interrupts can execute during
this time and could possible refill the queue - even if we
unblocked because space became available. */
taskENTER_CRITICAL();
{
/* Is there room on the queue now? To be running we must be
the highest priority task wanting to access the queue. */
if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
{
traceQUEUE_SEND( pxQueue );
prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
xReturn = pdPASS;
/* If there was a task waiting for data to arrive on the
queue then unblock it now. */
if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
{
if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
{
/* The unblocked task has a priority higher than
our own so yield immediately. */
taskYIELD();
}
}
}
else
{
/* Setting xReturn to errQUEUE_FULL will force its timeout
to be re-evaluated. This is necessary in case interrupts
and higher priority tasks accessed the queue between this
task being unblocked and subsequently attempting to write
to the queue. */
xReturn = errQUEUE_FULL;
}
}
taskEXIT_CRITICAL();
if( xReturn == errQUEUE_FULL )
{
if( xTicksToWait > ( portTickType ) 0 )
{
if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
{
xReturn = queueERRONEOUS_UNBLOCK;
}
else
{
traceQUEUE_SEND_FAILED( pxQueue );
}
}
else
{
traceQUEUE_SEND_FAILED( pxQueue );
}
}
}
while( xReturn == queueERRONEOUS_UNBLOCK );
return xReturn;
}
/*-----------------------------------------------------------*/
#if configUSE_ALTERNATIVE_API == 1
signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
{
signed portBASE_TYPE xReturn = pdPASS;
xTimeOutType xTimeOut;
/* The source code that implements the alternative (Alt) API is
simpler because it makes more use of critical sections. This is
the approach taken by many other RTOSes, but FreeRTOS.org has the
preferred fully featured API too. The fully featured API has more
complex code that takes longer to execute, but makes less use of
critical sections. */
do
{
/* If xTicksToWait is zero then we are not going to block even
if there is no room in the queue to post. */
if( xTicksToWait > ( portTickType ) 0 )
{
portENTER_CRITICAL();
{
if( xReturn == pdPASS )
{
/* This is the first time through - capture the time
inside the critical section to ensure we attempt to
block at least once. */
vTaskSetTimeOutState( &xTimeOut );
}
if( prvIsQueueFull( pxQueue ) )
{
/* Need to call xTaskCheckForTimeout again as time could
have passed since it was last called if this is not the
first time around this loop. */
if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
{
traceBLOCKING_ON_QUEUE_SEND( pxQueue );
vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
/* This will exit the critical section, then re-enter when
the task next runs. */
taskYIELD();
}
}
}
portEXIT_CRITICAL();
}
/* Higher priority tasks and interrupts can execute during
this time and could possible refill the queue - even if we
unblocked because space became available. */
taskENTER_CRITICAL();
{
/* Is there room on the queue now? To be running we must be
the highest priority task wanting to access the queue. */
if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
{
traceQUEUE_SEND( pxQueue );
prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
xReturn = pdPASS;
/* If there was a task waiting for data to arrive on the
queue then unblock it now. */
if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
{
if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
{
/* The unblocked task has a priority higher than
our own so yield immediately. */
taskYIELD();
}
}
}
else
{
/* Setting xReturn to errQUEUE_FULL will force its timeout
to be re-evaluated. This is necessary in case interrupts
and higher priority tasks accessed the queue between this
task being unblocked and subsequently attempting to write
to the queue. */
xReturn = errQUEUE_FULL;
}
}
taskEXIT_CRITICAL();
if( xReturn == errQUEUE_FULL )
{
if( xTicksToWait > ( portTickType ) 0 )
{
if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
{
xReturn = queueERRONEOUS_UNBLOCK;
}
else
{
traceQUEUE_SEND_FAILED( pxQueue );
}
}
else
{
traceQUEUE_SEND_FAILED( pxQueue );
}
}
}
while( xReturn == queueERRONEOUS_UNBLOCK );
return xReturn;
}
#endif /* configUSE_ALTERNATIVE_API */
/*-----------------------------------------------------------*/
#if configUSE_ALTERNATIVE_API == 1
signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
{
signed portBASE_TYPE xReturn = pdTRUE;
xTimeOutType xTimeOut;
signed portCHAR *pcOriginalReadPosition;
/* The source code that implements the alternative (Alt) API is
simpler because it makes more use of critical sections. This is
the approach taken by many other RTOSes, but FreeRTOS.org has the
preferred fully featured API too. The fully featured API has more
complex code that takes longer to execute, but makes less use of
critical sections. */
do
{
/* If there are no messages in the queue we may have to block. */
if( xTicksToWait > ( portTickType ) 0 )
{
portENTER_CRITICAL();
{
if( xReturn == pdPASS )
{
/* This is the first time through - capture the time
inside the critical section to ensure we attempt to
block at least once. */
vTaskSetTimeOutState( &xTimeOut );
}
if( prvIsQueueEmpty( pxQueue ) )
{
/* Need to call xTaskCheckForTimeout again as time could
have passed since it was last called if this is not the
first time around this loop. */
if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
{
traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
#if ( configUSE_MUTEXES == 1 )
{
if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
{
vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
}
}
#endif
vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
taskYIELD();
}
}
}
portEXIT_CRITICAL();
}
taskENTER_CRITICAL();
{
if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
{
/* Remember our read position in case we are just peeking. */
pcOriginalReadPosition = pxQueue->pcReadFrom;
prvCopyDataFromQueue( pxQueue, pvBuffer );
if( xJustPeeking == pdFALSE )
{
traceQUEUE_RECEIVE( pxQueue );
/* We are actually removing data. */
--( pxQueue->uxMessagesWaiting );
#if ( configUSE_MUTEXES == 1 )
{
if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
{
/* Record the information required to implement
priority inheritance should it become necessary. */
pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
}
}
#endif
if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
{
if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
{
taskYIELD();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -