Skip to content

Commit f69b1db

Browse files
authored
Add Stream Batching Buffer (#916)
The difference between a stream buffer and a stream batching buffer is when a task performs read on a non-empty buffer: - The task reading from a non-empty stream buffer returns immediately regardless of the amount of data in the buffer. - The task reading from a non-empty steam batching buffer blocks until the amount of data in the buffer exceeds the trigger level or the block time expires.
1 parent 5a72344 commit f69b1db

File tree

7 files changed

+260
-44
lines changed

7 files changed

+260
-44
lines changed

include/FreeRTOS.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,15 +982,15 @@
982982
#endif
983983

984984
#ifndef traceSTREAM_BUFFER_CREATE_FAILED
985-
#define traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer )
985+
#define traceSTREAM_BUFFER_CREATE_FAILED( xStreamBufferType )
986986
#endif
987987

988988
#ifndef traceSTREAM_BUFFER_CREATE_STATIC_FAILED
989-
#define traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer )
989+
#define traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xStreamBufferType )
990990
#endif
991991

992992
#ifndef traceSTREAM_BUFFER_CREATE
993-
#define traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer )
993+
#define traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xStreamBufferType )
994994
#endif
995995

996996
#ifndef traceSTREAM_BUFFER_DELETE
@@ -2402,15 +2402,15 @@
24022402
#endif
24032403

24042404
#ifndef traceENTER_xStreamBufferGenericCreate
2405-
#define traceENTER_xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback )
2405+
#define traceENTER_xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, xStreamBufferType, pxSendCompletedCallback, pxReceiveCompletedCallback )
24062406
#endif
24072407

24082408
#ifndef traceRETURN_xStreamBufferGenericCreate
24092409
#define traceRETURN_xStreamBufferGenericCreate( pvAllocatedMemory )
24102410
#endif
24112411

24122412
#ifndef traceENTER_xStreamBufferGenericCreateStatic
2413-
#define traceENTER_xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback )
2413+
#define traceENTER_xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, xStreamBufferType, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback )
24142414
#endif
24152415

24162416
#ifndef traceRETURN_xStreamBufferGenericCreateStatic

include/message_buffer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ typedef StreamBufferHandle_t MessageBufferHandle_t;
158158
* \ingroup MessageBufferManagement
159159
*/
160160
#define xMessageBufferCreate( xBufferSizeBytes ) \
161-
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, pdTRUE, NULL, NULL )
161+
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, sbTYPE_MESSAGE_BUFFER, NULL, NULL )
162162

163163
#if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
164164
#define xMessageBufferCreateWithCallback( xBufferSizeBytes, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
165-
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, pdTRUE, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
165+
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, sbTYPE_MESSAGE_BUFFER, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
166166
#endif
167167

168168
/**
@@ -243,11 +243,11 @@ typedef StreamBufferHandle_t MessageBufferHandle_t;
243243
* \ingroup MessageBufferManagement
244244
*/
245245
#define xMessageBufferCreateStatic( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer ) \
246-
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, pdTRUE, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), NULL, NULL )
246+
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, sbTYPE_MESSAGE_BUFFER, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), NULL, NULL )
247247

248248
#if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
249249
#define xMessageBufferCreateStaticWithCallback( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
250-
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, pdTRUE, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
250+
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, sbTYPE_MESSAGE_BUFFER, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
251251
#endif
252252

253253
/**

include/mpu_prototypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,12 @@ size_t MPU_xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuff
357357
* with all the APIs. */
358358
StreamBufferHandle_t MPU_xStreamBufferGenericCreate( size_t xBufferSizeBytes,
359359
size_t xTriggerLevelBytes,
360-
BaseType_t xIsMessageBuffer,
360+
BaseType_t xStreamBufferType,
361361
StreamBufferCallbackFunction_t pxSendCompletedCallback,
362362
StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
363363
StreamBufferHandle_t MPU_xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
364364
size_t xTriggerLevelBytes,
365-
BaseType_t xIsMessageBuffer,
365+
BaseType_t xStreamBufferType,
366366
uint8_t * const pucStreamBufferStorageArea,
367367
StaticStreamBuffer_t * const pxStaticStreamBuffer,
368368
StreamBufferCallbackFunction_t pxSendCompletedCallback,

include/stream_buffer.h

Lines changed: 201 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@
6262
#endif
6363
/* *INDENT-ON* */
6464

65+
/**
66+
* Type of stream buffer. For internal use only.
67+
*/
68+
#define sbTYPE_STREAM_BUFFER ( ( BaseType_t ) 0 )
69+
#define sbTYPE_MESSAGE_BUFFER ( ( BaseType_t ) 1 )
70+
#define sbTYPE_STREAM_BATCHING_BUFFER ( ( BaseType_t ) 2 )
71+
6572
/**
6673
* Type by which stream buffers are referenced. For example, a call to
6774
* xStreamBufferCreate() returns an StreamBufferHandle_t variable that can
@@ -157,11 +164,11 @@ typedef void (* StreamBufferCallbackFunction_t)( StreamBufferHandle_t xStreamBuf
157164
*/
158165

159166
#define xStreamBufferCreate( xBufferSizeBytes, xTriggerLevelBytes ) \
160-
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, NULL, NULL )
167+
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BUFFER, NULL, NULL )
161168

162169
#if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
163170
#define xStreamBufferCreateWithCallback( xBufferSizeBytes, xTriggerLevelBytes, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
164-
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
171+
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BUFFER, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
165172
#endif
166173

167174
/**
@@ -257,11 +264,199 @@ typedef void (* StreamBufferCallbackFunction_t)( StreamBufferHandle_t xStreamBuf
257264
*/
258265

259266
#define xStreamBufferCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer ) \
260-
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), NULL, NULL )
267+
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BUFFER, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), NULL, NULL )
261268

262269
#if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
263270
#define xStreamBufferCreateStaticWithCallback( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
264-
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
271+
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BUFFER, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
272+
#endif
273+
274+
/**
275+
* stream_buffer.h
276+
*
277+
* @code{c}
278+
* StreamBufferHandle_t xStreamBatchingBufferCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes );
279+
* @endcode
280+
*
281+
* Creates a new stream batching buffer using dynamically allocated memory. See
282+
* xStreamBatchingBufferCreateStatic() for a version that uses statically
283+
* allocated memory (memory that is allocated at compile time).
284+
*
285+
* configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
286+
* FreeRTOSConfig.h for xStreamBatchingBufferCreate() to be available.
287+
* configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
288+
* xStreamBatchingBufferCreate() to be available.
289+
*
290+
* The difference between a stream buffer and a stream batching buffer is when
291+
* a task performs read on a non-empty buffer:
292+
* - The task reading from a non-empty stream buffer returns immediately
293+
* regardless of the amount of data in the buffer.
294+
* - The task reading from a non-empty steam batching buffer blocks until the
295+
* amount of data in the buffer exceeds the trigger level or the block time
296+
* expires.
297+
*
298+
* @param xBufferSizeBytes The total number of bytes the stream batching buffer
299+
* will be able to hold at any one time.
300+
*
301+
* @param xTriggerLevelBytes The number of bytes that must be in the stream
302+
* batching buffer to unblock a task calling xStreamBufferReceive before the
303+
* block time expires.
304+
*
305+
* @param pxSendCompletedCallback Callback invoked when number of bytes at least
306+
* equal to trigger level is sent to the stream batching buffer. If the
307+
* parameter is NULL, it will use the default implementation provided by
308+
* sbSEND_COMPLETED macro. To enable the callback, configUSE_SB_COMPLETED_CALLBACK
309+
* must be set to 1 in FreeRTOSConfig.h.
310+
*
311+
* @param pxReceiveCompletedCallback Callback invoked when more than zero bytes
312+
* are read from a stream batching buffer. If the parameter is NULL, it will use
313+
* the default implementation provided by sbRECEIVE_COMPLETED macro. To enable
314+
* the callback, configUSE_SB_COMPLETED_CALLBACK must be set to 1 in
315+
* FreeRTOSConfig.h.
316+
*
317+
* @return If NULL is returned, then the stream batching buffer cannot be created
318+
* because there is insufficient heap memory available for FreeRTOS to allocate
319+
* the stream batching buffer data structures and storage area. A non-NULL value
320+
* being returned indicates that the stream batching buffer has been created
321+
* successfully - the returned value should be stored as the handle to the
322+
* created stream batching buffer.
323+
*
324+
* Example use:
325+
* @code{c}
326+
*
327+
* void vAFunction( void )
328+
* {
329+
* StreamBufferHandle_t xStreamBatchingBuffer;
330+
* const size_t xStreamBufferSizeBytes = 100, xTriggerLevel = 10;
331+
*
332+
* // Create a stream batching buffer that can hold 100 bytes. The memory used
333+
* // to hold both the stream batching buffer structure and the data in the stream
334+
* // batching buffer is allocated dynamically.
335+
* xStreamBatchingBuffer = xStreamBatchingBufferCreate( xStreamBufferSizeBytes, xTriggerLevel );
336+
*
337+
* if( xStreamBatchingBuffer == NULL )
338+
* {
339+
* // There was not enough heap memory space available to create the
340+
* // stream batching buffer.
341+
* }
342+
* else
343+
* {
344+
* // The stream batching buffer was created successfully and can now be used.
345+
* }
346+
* }
347+
* @endcode
348+
* \defgroup xStreamBatchingBufferCreate xStreamBatchingBufferCreate
349+
* \ingroup StreamBatchingBufferManagement
350+
*/
351+
352+
#define xStreamBatchingBufferCreate( xBufferSizeBytes, xTriggerLevelBytes ) \
353+
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BATCHING_BUFFER, NULL, NULL )
354+
355+
#if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
356+
#define xStreamBatchingBufferCreateWithCallback( xBufferSizeBytes, xTriggerLevelBytes, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
357+
xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BATCHING_BUFFER, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
358+
#endif
359+
360+
/**
361+
* stream_buffer.h
362+
*
363+
* @code{c}
364+
* StreamBufferHandle_t xStreamBatchingBufferCreateStatic( size_t xBufferSizeBytes,
365+
* size_t xTriggerLevelBytes,
366+
* uint8_t *pucStreamBufferStorageArea,
367+
* StaticStreamBuffer_t *pxStaticStreamBuffer );
368+
* @endcode
369+
* Creates a new stream batching buffer using statically allocated memory. See
370+
* xStreamBatchingBufferCreate() for a version that uses dynamically allocated
371+
* memory.
372+
*
373+
* configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h for
374+
* xStreamBatchingBufferCreateStatic() to be available. configUSE_STREAM_BUFFERS
375+
* must be set to 1 in for FreeRTOSConfig.h for xStreamBatchingBufferCreateStatic()
376+
* to be available.
377+
*
378+
* The difference between a stream buffer and a stream batching buffer is when
379+
* a task performs read on a non-empty buffer:
380+
* - The task reading from a non-empty stream buffer returns immediately
381+
* regardless of the amount of data in the buffer.
382+
* - The task reading from a non-empty steam batching buffer blocks until the
383+
* amount of data in the buffer exceeds the trigger level or the block time
384+
* expires.
385+
*
386+
* @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
387+
* pucStreamBufferStorageArea parameter.
388+
*
389+
* @param xTriggerLevelBytes The number of bytes that must be in the stream
390+
* batching buffer to unblock a task calling xStreamBufferReceive before the
391+
* block time expires.
392+
*
393+
* @param pucStreamBufferStorageArea Must point to a uint8_t array that is at
394+
* least xBufferSizeBytes big. This is the array to which streams are
395+
* copied when they are written to the stream batching buffer.
396+
*
397+
* @param pxStaticStreamBuffer Must point to a variable of type
398+
* StaticStreamBuffer_t, which will be used to hold the stream batching buffer's
399+
* data structure.
400+
*
401+
* @param pxSendCompletedCallback Callback invoked when number of bytes at least
402+
* equal to trigger level is sent to the stream batching buffer. If the parameter
403+
* is NULL, it will use the default implementation provided by sbSEND_COMPLETED
404+
* macro. To enable the callback, configUSE_SB_COMPLETED_CALLBACK must be set to
405+
* 1 in FreeRTOSConfig.h.
406+
*
407+
* @param pxReceiveCompletedCallback Callback invoked when more than zero bytes
408+
* are read from a stream batching buffer. If the parameter is NULL, it will use
409+
* the default implementation provided by sbRECEIVE_COMPLETED macro. To enable
410+
* the callback, configUSE_SB_COMPLETED_CALLBACK must be set to 1 in
411+
* FreeRTOSConfig.h.
412+
*
413+
* @return If the stream batching buffer is created successfully then a handle
414+
* to the created stream batching buffer is returned. If either pucStreamBufferStorageArea
415+
* or pxStaticstreamBuffer are NULL then NULL is returned.
416+
*
417+
* Example use:
418+
* @code{c}
419+
*
420+
* // Used to dimension the array used to hold the streams. The available space
421+
* // will actually be one less than this, so 999.
422+
* #define STORAGE_SIZE_BYTES 1000
423+
*
424+
* // Defines the memory that will actually hold the streams within the stream
425+
* // batching buffer.
426+
* static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
427+
*
428+
* // The variable used to hold the stream batching buffer structure.
429+
* StaticStreamBuffer_t xStreamBufferStruct;
430+
*
431+
* void MyFunction( void )
432+
* {
433+
* StreamBufferHandle_t xStreamBatchingBuffer;
434+
* const size_t xTriggerLevel = 1;
435+
*
436+
* xStreamBatchingBuffer = xStreamBatchingBufferCreateStatic( sizeof( ucStorageBuffer ),
437+
* xTriggerLevel,
438+
* ucStorageBuffer,
439+
* &xStreamBufferStruct );
440+
*
441+
* // As neither the pucStreamBufferStorageArea or pxStaticStreamBuffer
442+
* // parameters were NULL, xStreamBatchingBuffer will not be NULL, and can be
443+
* // used to reference the created stream batching buffer in other stream
444+
* // buffer API calls.
445+
*
446+
* // Other code that uses the stream batching buffer can go here.
447+
* }
448+
*
449+
* @endcode
450+
* \defgroup xStreamBatchingBufferCreateStatic xStreamBatchingBufferCreateStatic
451+
* \ingroup StreamBatchingBufferManagement
452+
*/
453+
454+
#define xStreamBatchingBufferCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer ) \
455+
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BATCHING_BUFFER, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), NULL, NULL )
456+
457+
#if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
458+
#define xStreamBatchingBufferCreateStaticWithCallback( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
459+
xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), sbTYPE_STREAM_BATCHING_BUFFER, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
265460
#endif
266461

267462
/**
@@ -1053,14 +1248,14 @@ void vStreamBufferSetStreamBufferNotificationIndex( StreamBufferHandle_t xStream
10531248
/* Functions below here are not part of the public API. */
10541249
StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
10551250
size_t xTriggerLevelBytes,
1056-
BaseType_t xIsMessageBuffer,
1251+
BaseType_t xStreamBufferType,
10571252
StreamBufferCallbackFunction_t pxSendCompletedCallback,
10581253
StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
10591254

10601255
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
10611256
StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
10621257
size_t xTriggerLevelBytes,
1063-
BaseType_t xIsMessageBuffer,
1258+
BaseType_t xStreamBufferType,
10641259
uint8_t * const pucStreamBufferStorageArea,
10651260
StaticStreamBuffer_t * const pxStaticStreamBuffer,
10661261
StreamBufferCallbackFunction_t pxSendCompletedCallback,

0 commit comments

Comments
 (0)