00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 #include <stdlib.h>
00082
00083
00084 #include "FreeRTOS.h"
00085 #include "task.h"
00086 #include "queue.h"
00087
00088
00089 #include "AltBlckQ.h"
00090
00091 #define blckqSTACK_SIZE configMINIMAL_STACK_SIZE
00092 #define blckqNUM_TASK_SETS ( 3 )
00093
00094
00095 typedef struct BLOCKING_QUEUE_PARAMETERS
00096 {
00097 xQueueHandle xQueue;
00098 portTickType xBlockTime;
00099 volatile portSHORT *psCheckVariable;
00100 } xBlockingQueueParameters;
00101
00102
00103 static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );
00104
00105
00106
00107 static portTASK_FUNCTION_PROTO( vBlockingQueueConsumer, pvParameters );
00108
00109
00110
00111
00112 static volatile portSHORT sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
00113
00114
00115
00116 static volatile portSHORT sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
00117
00118
00119
00120 void vStartAltBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )
00121 {
00122 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;
00123 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;
00124 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;
00125 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;
00126 const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;
00127 const portTickType xDontBlock = ( portTickType ) 0;
00128
00129
00130
00131
00132 pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00133
00134
00135
00136 pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
00137
00138
00139 pxQueueParameters1->xBlockTime = xBlockTime;
00140
00141
00142
00143 pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );
00144
00145
00146 pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00147
00148
00149 pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;
00150
00151
00152
00153 pxQueueParameters2->xBlockTime = xDontBlock;
00154
00155
00156
00157 pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );
00158
00159
00160
00161
00162 xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );
00163 xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );
00164
00165
00166
00167
00168
00169
00170 pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00171 pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
00172 pxQueueParameters3->xBlockTime = xDontBlock;
00173 pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );
00174
00175 pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00176 pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;
00177 pxQueueParameters4->xBlockTime = xBlockTime;
00178 pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );
00179
00180 xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );
00181 xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );
00182
00183
00184
00185
00186
00187 pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00188 pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
00189 pxQueueParameters5->xBlockTime = xBlockTime;
00190 pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );
00191
00192 pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00193 pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;
00194 pxQueueParameters6->xBlockTime = xBlockTime;
00195 pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );
00196
00197 xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );
00198 xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );
00199 }
00200
00201
00202 static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )
00203 {
00204 unsigned portSHORT usValue = 0;
00205 xBlockingQueueParameters *pxQueueParameters;
00206 portSHORT sErrorEverOccurred = pdFALSE;
00207
00208 #ifdef USE_STDIO
00209 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
00210
00211 const portCHAR * const pcTaskStartMsg = "Alt blocking queue producer task started.\r\n";
00212
00213
00214 vPrintDisplayMessage( &pcTaskStartMsg );
00215 #endif
00216
00217 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
00218
00219 for( ;; )
00220 {
00221 if( xQueueAltSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )
00222 {
00223 sErrorEverOccurred = pdTRUE;
00224 }
00225 else
00226 {
00227
00228
00229 if( sErrorEverOccurred == pdFALSE )
00230 {
00231 ( *pxQueueParameters->psCheckVariable )++;
00232 }
00233
00234
00235
00236 ++usValue;
00237 }
00238 }
00239 }
00240
00241
00242 static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )
00243 {
00244 unsigned portSHORT usData, usExpectedValue = 0;
00245 xBlockingQueueParameters *pxQueueParameters;
00246 portSHORT sErrorEverOccurred = pdFALSE;
00247
00248 #ifdef USE_STDIO
00249 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
00250
00251 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";
00252
00253
00254 vPrintDisplayMessage( &pcTaskStartMsg );
00255 #endif
00256
00257 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
00258
00259 for( ;; )
00260 {
00261 if( xQueueAltReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )
00262 {
00263 if( usData != usExpectedValue )
00264 {
00265
00266 usExpectedValue = usData;
00267
00268 sErrorEverOccurred = pdTRUE;
00269 }
00270 else
00271 {
00272
00273
00274 if( sErrorEverOccurred == pdFALSE )
00275 {
00276 ( *pxQueueParameters->psCheckVariable )++;
00277 }
00278
00279
00280
00281 ++usExpectedValue;
00282 }
00283 }
00284 }
00285 }
00286
00287
00288
00289 portBASE_TYPE xAreAltBlockingQueuesStillRunning( void )
00290 {
00291 static portSHORT sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
00292 static portSHORT sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
00293 portBASE_TYPE xReturn = pdPASS, xTasks;
00294
00295
00296
00297
00298
00299
00300
00301
00302 for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )
00303 {
00304 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ] )
00305 {
00306 xReturn = pdFALSE;
00307 }
00308 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];
00309
00310
00311 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ] )
00312 {
00313 xReturn = pdFALSE;
00314 }
00315 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];
00316 }
00317
00318 return xReturn;
00319 }
00320