xQueueAddToSet/xQueueRemoveFromSet is it safe?

beginend1 wrote on Friday, January 24, 2014:

Is it safe to use xQueueAddToSet/xQueueRemoveFromSet? The check in those functions are made outside critical section, so there could be race condition. See note in code.

	if( ( ( xQUEUE * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
	{
		/* Cannot add a queue/semaphore to more than one queue set. */
		xReturn = pdFAIL;
	}
	/* #### critical setion from here ? #### */
	else if( ( ( xQUEUE * ) xQueueOrSemaphore )->uxMessagesWaiting != ( unsigned portBASE_TYPE ) 0 )
	{
		/* Cannot add a queue/semaphore to a queue set if there are already
		items in the queue/semaphore. */
		xReturn = pdFAIL;
	}
	else
	{
		taskENTER_CRITICAL();
		{
			( ( xQUEUE * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
		}
		taskEXIT_CRITICAL();
		xReturn = pdPASS;
	}

	return xReturn;

rtel wrote on Friday, January 24, 2014:

I’m not sure why the decision was taken not to put a critical section around that line, as there is a critical section in the ‘else’ statement. No matter what the reason, as this is a very fast function that is generally only called during initialisation anyway, it would seem prudent to move the existing critical section to instead go around the entire if/else if/else statements. It has been done but not checked in yet.

Regards.