# Can FreeRTOS-POSIX be used with SMP

**URL:** <https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158>\
**Category:** Kernel\
**Created:** [December 31, 2024, 9:45am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158 "2024-12-31T09:45:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saiiijchan](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/saiiijchan/32/3287_2.png) [@Saiiijchan](https://forums.freertos.org/u/Saiiijchan)\
**Post date:** [December 31, 2024, 9:45am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/1 "2024-12-31T09:45:08Z")

</div>

Hi

[FreeRTOS POSIX V1.1.0](https://github.com/FreeRTOS/Lab-Project-FreeRTOS-POSIX) was launched four years ago, but SMP was launched this year with v11. Can FreeRTOS + POSIX be used for FreeRTOS SMP?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [January 2, 2025, 5:49am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/2 "2025-01-02T05:49:10Z")

</div>

FreeRTOS-POSIX is a labs project which implements a small subset of POSIX APIs. It does not expose the [SMP specific functionality](https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/13-Symmetric-multiprocessing-introduction) currently. Other than that it should theoretically work but we never tested it with SMP. Why do you want to use FreeRTOS-POSIX and not native FreeRTOS APIs directly?

---

<div class="post-metadata">

**Author:** ![Saiiijchan](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/saiiijchan/32/3287_2.png) [@Saiiijchan](https://forums.freertos.org/u/Saiiijchan)\
**Post date:** [February 13, 2025, 11:09am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/3 "2025-02-13T11:09:58Z")

</div>

Sorry for the late reply. Our client is exploring POSIX compatibility, and we’ve been modifying POSIX APIs to adapt to the V11.1 SMP environment. However, we’ve encountered an issue with `errno` handling in SMP scenarios.

The problem:  
We’re trying to replace the global errno variable with a per-core array like `freertos_errno[configNUMBER_OF_CORES]`, but there’s a race condition during assignment. When determining the core ID for errno updates, a context switch might occur between reading the core ID and writing to the array. This could lead to the errno value being written to the wrong core’s slot if the thread migrates between cores during the switch.

We’re considering atomic operations to ensure core ID consistency during assignment, but we’re wondering are there alternative approaches to handle it?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [February 13, 2025, 4:35pm UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/4 "2025-02-13T16:35:19Z")

</div>

Making errno core specific doesn’t actually do anything about the problem, as if the task could change during the setting, it could change just after the setting and the task wouldn’t see it had been set.

Normally, errno is made per thread/task, not per-core.

---

<div class="post-metadata">

**Author:** ![Saiiijchan](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/saiiijchan/32/3287_2.png) [@Saiiijchan](https://forums.freertos.org/u/Saiiijchan)\
**Post date:** [February 14, 2025, 5:09am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/5 "2025-02-14T05:09:45Z")

</div>

> [@richard-damon](#):
>
> Normally, errno is made per thread/task, not per-core.

Yes, that is the point. While the current implement saves/restores errno through the `iTaskError` field in the TCB during context switches [vTaskSwitchContext()](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/1a1ae36f9aa4e3c474e55d6404238898003dfc47/tasks.c#L5155).

```auto
 #if ( configUSE_POSIX_ERRNO == 1 )
+#if ( configNUMBER_OF_CORES> 1 )
+ volatile int FreeRTOS_errnos[configNUMBER_OF_CORES] = { 0 };
+ #define FreeRTOS_errno FreeRTOS_errnos[portGET_CORE_ID()]
+#else
        int FreeRTOS_errno = 0;
+#endif /* configNUMBER_OF_CORES> 1 */
 #endif

```

Use array to avoid FreeRTOS\_errno modified by multi-core, but this approach creates a hazardous race condition in SMP environments:

1. **Pre-Switch Phase** :

- Task\_A on Core\_0 writes to `freertos_errno[Core_0]`
- Context switch triggered before TCB update

1. **Post-Switch Phase** :

- Task\_A migrates to Core\_1
- Scheduler restores stale `iTaskError` from TCB (now mapped to `freertos_errno[Core_1]`)

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [February 14, 2025, 6:01am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/6 "2025-02-14T06:01:00Z")

</div>

Should we just have one copy in the TCB and always read/write that?

---

<div class="post-metadata">

**Author:** ![Saiiijchan](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/saiiijchan/32/3287_2.png) [@Saiiijchan](https://forums.freertos.org/u/Saiiijchan)\
**Post date:** [February 14, 2025, 7:40am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/7 "2025-02-14T07:40:17Z")

</div>

That’s a good suggestion, but since the `TCB_t` structure is defined internally in the .c file (not exposed in headers), POSIX layers and applications can’t directly access `pxTCB->iTaskErrno` for errno operations.  
How about store `errno` in the [pthread structure](https://github.com/FreeRTOS/Lab-Project-FreeRTOS-POSIX/blob/83398452660e8bfa65eaa2048495ed30cb793f21/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c#L67C3-L67C21) itself?

```auto
typedef struct pthread_internal
{
    pthread_attr_internal_t xAttr; /**< Thread attributes. */
    void * ( *pvStartRoutine )( void * ); /**< Application thread function. */
    void * xTaskArg; /**< Arguments for application thread function. */
    TaskHandle_t xTaskHandle; /**< FreeRTOS task handle. */
    StaticSemaphore_t xJoinBarrier; /**< Synchronizes the two callers of pthread_join. */
    StaticSemaphore_t xJoinMutex; /**< Ensures that only one other thread may join this thread. */
    void * xReturn; /**< Return value of pvStartRoutine. */
+ int32_t errno; /**< Error number. */
} pthread_internal_t;

```

Allow direct access via `pthread_self()->errno` without exposing implementation details.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [February 14, 2025, 9:04am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/8 "2025-02-14T09:04:49Z")

</div>

> [@Saiiijchan](#):
>
> Allow direct access via `pthread_self()->errno` without exposing implementation details.

This suggestion sounds good to me!

---

<div class="post-metadata">

**Author:** ![Saiiijchan](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/saiiijchan/32/3287_2.png) [@Saiiijchan](https://forums.freertos.org/u/Saiiijchan)\
**Post date:** [February 14, 2025, 9:54am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/9 "2025-02-14T09:54:24Z")

</div>

Sorry，`pthread_internal_t` is also defined in the .c file 😰. If adding new APIs in FreeRTOS\_POSIX\_pthread.c and pthread.h to read errors, it seems inconsistent with the pthread specification.

How about adding an `xTaskGetERRNO` API in tasks.c to retrieve `tcb->iTaskError`

```auto
#if ( configUSE_POSIX_ERRNO == 1 )
    extern int *xTaskGetERRNO( void );
    #define errno *xTaskGetERRNO()
#endif

```

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [February 16, 2025, 5:52am UTC](https://forums.freertos.org/t/can-freertos-posix-be-used-with-smp/22158/10 "2025-02-16T05:52:54Z")

</div>

> [@Saiiijchan](#):
>
> If adding new APIs in FreeRTOS\_POSIX\_pthread.c and pthread.h to read errors, it seems inconsistent with the pthread specification.

We won’t be deviating from the spec but just add some APIs, right? Since errno is related to POSIX, it makes more sense to keep it in the FreeRTOS\_POSIX\_pthread code.
