]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/include/semphr.h
- Rework the StaticAllocation.c common demo file to reflect the changes to the static...
[freertos] / FreeRTOS / Source / include / semphr.h
index 48d384095544203cd9fc29f2240978fa32255d0c..eff0bfd306259e7905b55cf6b8db28c87d79416b 100644 (file)
@@ -132,14 +132,16 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary\r
  * \ingroup Semaphores\r
  */\r
-#define vSemaphoreCreateBinary( xSemaphore )                                                                                                                                                                                                           \\r
-       {                                                                                                                                                                                                                                                                                               \\r
-               ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, NULL, queueQUEUE_TYPE_BINARY_SEMAPHORE );        \\r
-               if( ( xSemaphore ) != NULL )                                                                                                                                                                                                                            \\r
-               {                                                                                                                                                                                                                                                                                       \\r
-                       ( void ) xSemaphoreGive( ( xSemaphore ) );                                                                                                                                                                                              \\r
-               }                                                                                                                                                                                                                                                                                       \\r
-       }\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+       #define vSemaphoreCreateBinary( xSemaphore )                                                                                                                                                                                    \\r
+               {                                                                                                                                                                                                                                                                       \\r
+                       ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE );    \\r
+                       if( ( xSemaphore ) != NULL )                                                                                                                                                                                                    \\r
+                       {                                                                                                                                                                                                                                                               \\r
+                               ( void ) xSemaphoreGive( ( xSemaphore ) );                                                                                                                                                                      \\r
+                       }                                                                                                                                                                                                                                                               \\r
+               }\r
+#endif\r
 \r
 /**\r
  * semphr. h\r
@@ -158,9 +160,8 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * automatically dynamically allocated inside the xSemaphoreCreateBinary()\r
  * function.  (see http://www.freertos.org/a00111.html).  If a binary semaphore\r
  * is created using xSemaphoreCreateBinaryStatic() then the application writer\r
- * can instead optionally provide the memory that will get used by the binary\r
- * semaphore.  xSemaphoreCreateBinaryStatic() therefore allows a binary\r
- * semaphore to be created without using any dynamic memory allocation.\r
+ * must provide the memory.  xSemaphoreCreateBinaryStatic() therefore allows a\r
+ * binary semaphore to be created without using any dynamic memory allocation.\r
  *\r
  * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this\r
  * xSemaphoreCreateBinary() function.  Note that binary semaphores created using\r
@@ -199,7 +200,9 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup xSemaphoreCreateBinary xSemaphoreCreateBinary\r
  * \ingroup Semaphores\r
  */\r
-#define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, NULL, queueQUEUE_TYPE_BINARY_SEMAPHORE )\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+       #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )\r
+#endif\r
 \r
 /**\r
  * semphr. h\r
@@ -218,9 +221,8 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * automatically dynamically allocated inside the xSemaphoreCreateBinary()\r
  * function.  (see http://www.freertos.org/a00111.html).  If a binary semaphore\r
  * is created using xSemaphoreCreateBinaryStatic() then the application writer\r
- * can instead optionally provide the memory that will get used by the binary\r
- * semaphore.  xSemaphoreCreateBinaryStatic() therefore allows a binary\r
- * semaphore to be created without using any dynamic memory allocation.\r
+ * must provide the memory.  xSemaphoreCreateBinaryStatic() therefore allows a\r
+ * binary semaphore to be created without using any dynamic memory allocation.\r
  *\r
  * This type of semaphore can be used for pure synchronisation between tasks or\r
  * between an interrupt and a task.  The semaphore need not be given back once\r
@@ -229,21 +231,12 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * semaphore does not use a priority inheritance mechanism.  For an alternative\r
  * that does use priority inheritance see xSemaphoreCreateMutex().\r
  *\r
- * @param pxSemaphoreBuffer If pxSemaphoreBuffer is NULL then the memory\r
- * required to hold the semaphore's data structures will be allocated\r
- * dynamically, just as when a semaphore is created using\r
- * xSemaphoreCreateBinary().  If pxSemaphoreBuffer is not NULL then it must\r
- * point to a variable of type StaticSemaphore_t, which will then be used to\r
- * hold the semaphore's data structure, removing the need for the memory to be\r
- * allocated dynamically.\r
- *\r
- * @return If pxSemaphoreBuffer is not NULL then the function will not attempt\r
- * any dynamic memory allocation, and a handle to the created semaphore will\r
- * always be returned.  If pxSemaphoreBuffer is NULL then the function will\r
- * attempt to dynamically allocate the memory required to hold the semaphore's\r
- * data structures.  In this case, if the allocation succeeds then a handle to\r
- * the created semaphore will be returned, and if the allocation fails NULL will\r
- * be returned.\r
+ * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,\r
+ * which will then be used to hold the semaphore's data structure, removing the\r
+ * need for the memory to be allocated dynamically.\r
+ *\r
+ * @return If the semaphore is created then a handle to the created semaphore is\r
+ * returned.  If pxSemaphoreBuffer is NULL then NULL is returned.\r
  *\r
  * Example usage:\r
  <pre>\r
@@ -267,7 +260,7 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \ingroup Semaphores\r
  */\r
 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
-       #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )\r
+       #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )\r
 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
 \r
 /**\r
@@ -714,10 +707,9 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * using xSemaphoreCreateMutex() then the required memory is automatically\r
  * dynamically allocated inside the xSemaphoreCreateMutex() function.  (see\r
  * http://www.freertos.org/a00111.html).  If a mutex is created using\r
- * xSemaphoreCreateMutexStatic() then the application writer can instead\r
- * optionally provide the memory that will get used by the mutex.\r
- * xSemaphoreCreateMutexStatic() therefore allows a mutex to be created without\r
- * using any dynamic memory allocation.\r
+ * xSemaphoreCreateMutexStatic() then the application writer must provided the\r
+ * memory.  xSemaphoreCreateMutexStatic() therefore allows a mutex to be created\r
+ * without using any dynamic memory allocation.\r
  *\r
  * Mutexes created using this function can be accessed using the xSemaphoreTake()\r
  * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and\r
@@ -758,7 +750,9 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup xSemaphoreCreateMutex xSemaphoreCreateMutex\r
  * \ingroup Semaphores\r
  */\r
-#define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX, NULL )\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+       #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )\r
+#endif\r
 \r
 /**\r
  * semphr. h\r
@@ -772,10 +766,9 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * using xSemaphoreCreateMutex() then the required memory is automatically\r
  * dynamically allocated inside the xSemaphoreCreateMutex() function.  (see\r
  * http://www.freertos.org/a00111.html).  If a mutex is created using\r
- * xSemaphoreCreateMutexStatic() then the application writer can instead\r
- * optionally provide the memory that will get used by the mutex.\r
- * xSemaphoreCreateMutexStatic() therefore allows a mutex to be created without\r
- * using any dynamic memory allocation.\r
+ * xSemaphoreCreateMutexStatic() then the application writer must provided the\r
+ * memory.  xSemaphoreCreateMutexStatic() therefore allows a mutex to be created\r
+ * without using any dynamic memory allocation.\r
  *\r
  * Mutexes created using this function can be accessed using the xSemaphoreTake()\r
  * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and\r
@@ -792,16 +785,12 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * semaphore and another always 'takes' the semaphore) and from within interrupt\r
  * service routines.\r
  *\r
- * @param pxMutexBuffer If pxMutexBuffer is NULL then the memory required to\r
- * hold the mutex's data structures will be allocated dynamically, just as when\r
- * a mutex is created using xSemaphoreCreateMutex().  If pxMutexBuffer is not\r
- * NULL then it must point to a variable of type StaticSemaphore_t, which will\r
- * then be used to hold the mutex's data structure, removing the need for\r
+ * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,\r
+ * which will be used to hold the mutex's data structure, removing the need for\r
  * the memory to be allocated dynamically.\r
  *\r
  * @return If the mutex was successfully created then a handle to the created\r
- * mutex is returned.  If pxMutexBuffer was NULL, and there was not enough\r
- * heap to allocate the mutex data structures, then NULL is returned.\r
+ * mutex is returned.  If pxMutexBuffer was NULL then NULL is returned.\r
  *\r
  * Example usage:\r
  <pre>\r
@@ -823,7 +812,7 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \ingroup Semaphores\r
  */\r
  #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
-       #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutex( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )\r
+       #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )\r
 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
 \r
 \r
@@ -840,8 +829,8 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * automatically dynamically allocated inside the\r
  * xSemaphoreCreateRecursiveMutex() function.  (see\r
  * http://www.freertos.org/a00111.html).  If a recursive mutex is created using\r
- * xSemaphoreCreateRecursiveMutexStatic() then the application writer can\r
- * instead optionally provide the memory that will get used by the mutex.\r
+ * xSemaphoreCreateRecursiveMutexStatic() then the application writer must\r
+ * provide the memory that will get used by the mutex.\r
  * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to\r
  * be created without using any dynamic memory allocation.\r
  *\r
@@ -890,7 +879,9 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup xSemaphoreCreateRecursiveMutex xSemaphoreCreateRecursiveMutex\r
  * \ingroup Semaphores\r
  */\r
-#define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX, NULL )\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+       #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )\r
+#endif\r
 \r
 /**\r
  * semphr. h\r
@@ -905,8 +896,8 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * automatically dynamically allocated inside the\r
  * xSemaphoreCreateRecursiveMutex() function.  (see\r
  * http://www.freertos.org/a00111.html).  If a recursive mutex is created using\r
- * xSemaphoreCreateRecursiveMutexStatic() then the application writer can\r
- * instead optionally provide the memory that will get used by the mutex.\r
+ * xSemaphoreCreateRecursiveMutexStatic() then the application writer must\r
+ * provide the memory that will get used by the mutex.\r
  * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to\r
  * be created without using any dynamic memory allocation.\r
  *\r
@@ -932,17 +923,12 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * semaphore and another always 'takes' the semaphore) and from within interrupt\r
  * service routines.\r
  *\r
- * @param pxMutexBuffer If pxMutexBuffer is NULL then the memory required to\r
- * hold the recursive mutex's data structures will be allocated dynamically,\r
- * just as when a recursive mutex is created using\r
- * xSemaphoreCreateRecursiveMutex().  If pxMutexBuffer is not NULL then it must\r
- * point to a variable of type StaticSemaphore_t, which will then be used to\r
- * hold the recursive mutex's data structure, removing the need for the memory\r
- * to be allocated dynamically.\r
+ * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,\r
+ * which will then be used to hold the recursive mutex's data structure,\r
+ * removing the need for the memory to be allocated dynamically.\r
  *\r
  * @return If the recursive mutex was successfully created then a handle to the\r
- * created recursive mutex is returned.  If pxMutexBuffer was NULL, and there\r
- * was not enough heap to allocate the mutex data structures, then NULL is\r
+ * created recursive mutex is returned.  If pxMutexBuffer was NULL then NULL is\r
  * returned.\r
  *\r
  * Example usage:\r
@@ -967,7 +953,7 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \ingroup Semaphores\r
  */\r
 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
-       #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )\r
+       #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )\r
 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
 \r
 /**\r
@@ -977,6 +963,10 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * Creates a new counting semaphore instance, and returns a handle by which the\r
  * new counting semaphore can be referenced.\r
  *\r
+ * In many usage scenarios it is faster and more memory efficient to use a\r
+ * direct to task notification in place of a counting semaphore!\r
+ * http://www.freertos.org/RTOS-task-notifications.html\r
+ *\r
  * Internally, within the FreeRTOS implementation, counting semaphores use a\r
  * block of memory, in which the counting semaphore structure is stored.  If a\r
  * counting semaphore is created using xSemaphoreCreateCounting() then the\r
@@ -1042,7 +1032,9 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting\r
  * \ingroup Semaphores\r
  */\r
-#define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ), ( NULL ) )\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+       #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )\r
+#endif\r
 \r
 /**\r
  * semphr. h\r
@@ -1051,16 +1043,19 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * Creates a new counting semaphore instance, and returns a handle by which the\r
  * new counting semaphore can be referenced.\r
  *\r
+ * In many usage scenarios it is faster and more memory efficient to use a\r
+ * direct to task notification in place of a counting semaphore!\r
+ * http://www.freertos.org/RTOS-task-notifications.html\r
+ *\r
  * Internally, within the FreeRTOS implementation, counting semaphores use a\r
  * block of memory, in which the counting semaphore structure is stored.  If a\r
  * counting semaphore is created using xSemaphoreCreateCounting() then the\r
  * required memory is automatically dynamically allocated inside the\r
  * xSemaphoreCreateCounting() function.  (see\r
  * http://www.freertos.org/a00111.html).  If a counting semaphore is created\r
- * using xSemaphoreCreateCountingStatic() then the application writer can\r
- * instead optionally provide the memory that will get used by the counting\r
- * semaphore.  xSemaphoreCreateCountingStatic() therefore allows a counting\r
- * semaphore to be created without using any dynamic memory allocation.\r
+ * using xSemaphoreCreateCountingStatic() then the application writer must\r
+ * provide the memory.  xSemaphoreCreateCountingStatic() therefore allows a\r
+ * counting semaphore to be created without using any dynamic memory allocation.\r
  *\r
  * Counting semaphores are typically used for two things:\r
  *\r
@@ -1090,18 +1085,13 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * @param uxInitialCount The count value assigned to the semaphore when it is\r
  *        created.\r
  *\r
- * @param pxSemaphoreBuffer If pxSemaphoreBuffer is NULL then the memory\r
- * required to hold the semaphore's data structures will be allocated\r
- * dynamically, just as when a counting semaphore is created using\r
- * xSemaphoreCreateCounting().  If pxSemaphoreBuffer is not NULL then it must\r
- * point to a variable of type StaticSemaphore_t, which will then be used to\r
- * hold the semaphore's data structure, removing the need for the memory\r
- * to be allocated dynamically.\r
+ * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,\r
+ * which will then be used to hold the semaphore's data structure, removing the\r
+ * need for the memory to be allocated dynamically.\r
  *\r
  * @return If the counting semaphore was successfully created then a handle to\r
- * the created counting semaphore is returned.  If pxSemaphoreBuffer was NULL,\r
- * and there was not enough heap to allocate the counting semaphore data\r
- * structures, then NULL is returned.\r
+ * the created counting semaphore is returned.  If pxSemaphoreBuffer was NULL\r
+ * then NULL is returned.\r
  *\r
  * Example usage:\r
  <pre>\r
@@ -1128,7 +1118,7 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \ingroup Semaphores\r
  */\r
 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
-       #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )\r
+       #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )\r
 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
 \r
 /**\r
@@ -1162,11 +1152,11 @@ typedef QueueHandle_t SemaphoreHandle_t;
 \r
 /**\r
  * semphr.h\r
- * <pre>TaskHandle_t xSemaphoreGetCount( SemaphoreHandle_t xMutex );</pre>\r
+ * <pre>UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xMutex );</pre>\r
  *\r
- * If the semaphore is a counting semaphore then xSemaphoreGetCount() returns\r
+ * If the semaphore is a counting semaphore then uxSemaphoreGetCount() returns\r
  * its current count value.  If the semaphore is a binary semaphore then\r
- * xSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the\r
+ * uxSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the\r
  * semaphore is not available.\r
  *\r
  */\r