Skip to content

Commit 21d0639

Browse files
authored
pthread_cond: Use Task Notifications (#34)
Update the pthread_cond_t implementation to utilize Task Notifications. In the existing implementation, if multiple threads of different priority are blocked on the same condition, a higher priority thread, in most cases, will unblock multiple times due to taking the underlying semaphore multiple times instead of just once. Switching to Task notifications guarantees that all tasks are equally notified and unblocked. pthread_cond_signal has also been updated to conform to the POSIX specification in that it will unblock the highest priority task waiting on the condition. Resolves #8
1 parent e6b133f commit 21d0639

File tree

3 files changed

+162
-101
lines changed

3 files changed

+162
-101
lines changed

FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX_internal.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,22 @@
8080
typedef struct pthread_cond_internal
8181
{
8282
BaseType_t xIsInitialized; /**< Set to pdTRUE if this condition variable is initialized, pdFALSE otherwise. */
83-
StaticSemaphore_t xCondWaitSemaphore; /**< Threads block on this semaphore in pthread_cond_wait. */
84-
unsigned iWaitingThreads; /**< The number of threads currently waiting on this condition variable. */
83+
TaskHandle_t* xTasksWaiting; /**< Array of tasks awaiting the condition */
84+
int tasksLenth; /**< Length of the xTasksWaiting Array */
8585
} pthread_cond_internal_t;
8686

8787
/**
8888
* @brief Compile-time initializer of pthread_cond_internal_t.
8989
*/
9090

91-
#define FREERTOS_POSIX_COND_INITIALIZER \
92-
( ( ( pthread_cond_internal_t ) \
93-
{ \
94-
.xIsInitialized = pdFALSE, \
95-
.xCondWaitSemaphore = { { 0 } }, \
96-
.iWaitingThreads = 0 \
97-
} \
98-
) \
91+
#define FREERTOS_POSIX_COND_INITIALIZER \
92+
( ( ( pthread_cond_internal_t ) \
93+
{ \
94+
.xIsInitialized = pdFALSE, \
95+
.xTasksWaiting = NULL, \
96+
.tasksLenth = posixconfigPTHREAD_COND_MAX_WAITERS \
97+
} \
98+
) \
9999
)
100100

101101
#endif /* if posixconfigENABLE_PTHREAD_COND_T == 1 */

FreeRTOS-Plus-POSIX/include/portable/FreeRTOS_POSIX_portable_default.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262
#endif
6363
/**@} */
6464

65+
/**
66+
* @name Defaults for POSIX conditions implementation.
67+
*/
68+
/**@{ */
69+
#ifndef posixconfigPTHREAD_COND_MAX_WAITERS
70+
#define posixconfigPTHREAD_COND_MAX_WAITERS 4 /**< Maximum number of tasks that can wait on a cond at one time */
71+
#endif
72+
6573
/**
6674
* @name POSIX implementation-dependent constants usually defined in limits.h.
6775
*

0 commit comments

Comments
 (0)