# FreeRTOs with "error.h" ...

**URL:** <https://forums.freertos.org/t/freertos-with-error-h/6864>\
**Category:** Kernel\
**Created:** [November 29, 2017, 8:51am UTC](https://forums.freertos.org/t/freertos-with-error-h/6864 "2017-11-29T08:51:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 29, 2017, 8:51am UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/1 "2017-11-29T08:51:26Z")

</div>

**[debugasm](https://sourceforge.net/u/debugasm/)** wrote on [Wednesday, November 29, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#21d5):

Hi,

I use FreeRTOs v9.0.0 with Lwip v1.4.1 and socket thread uses “error.h” with error variable to get fault connection, timeout, ecc.

The question:

(1) The “error” variable is FreeRTOs Thread safe or reentrant ?

(2) How check if “error” is visible only for thread and not overwritten by other threads ?

Thanks very much.

debugasm

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 29, 2017, 6:06pm UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/2 "2017-11-29T18:06:09Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, November 29, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#21d5/1187):

Sorry, don’t know, that is part of lwiP not FreeRTOS.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 30, 2017, 8:18am UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/3 "2017-11-30T08:18:55Z")

</div>

**[debugasm](https://sourceforge.net/u/debugasm/)** wrote on [Thursday, November 30, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#0e38):

Hi,

> Sorry, don’t know, that is part of lwiP not FreeRTOS.

You are right, but FreeRTOs is also used with “libc” that provides “errno”, perhaps one possibility is to modify the “get\_errno” and “set\_errno” routines to write the error in a task variable using “configNUM\_THREAD\_LOCAL\_STORAGE\_POINTERS”.

I can try to implement it.

Thanks.

debugasm

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [November 30, 2017, 9:05am UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/4 "2017-11-30T09:05:14Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Thursday, November 30, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#2189):

That’s indeed the way to go.

When defining `configNUM_THREAD_LOCAL_STORAGE_POINTERS`, a number of objects can be stored in the task’s context.

This object has the size of a pointer, but it does not have to be a pointer. An integer may be casted and stored as a pointer.

The FreeRTOS+FAT library already uses 3 of these storage pointers, one of the to stored the last `errno` seen. Another pointer stores the CWD (current working directory).

Here are the two access functions:

```auto
/* The errno is stored in a thread local buffer. */
static portINLINE void stdioSET_ERRNO( int iErrno )
{
	vTaskSetThreadLocalStoragePointer( NULL, ffconfigCWD_THREAD_LOCAL_INDEX, ( void * ) ( iErrno ) );
}

static portINLINE int stdioGET_ERRNO( void )
{
void *pvResult;

	pvResult = pvTaskGetThreadLocalStoragePointer( ( TaskHandle_t )NULL, ffconfigCWD_THREAD_LOCAL_INDEX );
	return ( int ) pvResult;
}

```

I know that in some environments `errno` has an instance per thread in some magic way, but I don’t think that it will work together with FreeRTOS.

Regards,

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 30, 2017, 1:52pm UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/5 "2017-11-30T13:52:39Z")

</div>

**[debugasm](https://sourceforge.net/u/debugasm/)** wrote on [Thursday, November 30, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#df64):

Hi,

> I know that in some environments errno has an instance per thread in some magic way, but I  
> don’t think that it will work together with FreeRTOS.

The Lwip have a routine to set “errno”, I just need to modify this function to use FreeRTOs. It should not be difficult.

Thanks very much.

debugasm

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [November 30, 2017, 2:41pm UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/6 "2017-11-30T14:41:12Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Thursday, November 30, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#102e):

Not everyone appreciates C++, but you could make a nice little class for `errno`.

```auto
class CErrno {
public:
    operator int()
    {
    void *pvResult;

        pvResult = pvTaskGetThreadLocalStoragePointer( ( TaskHandle_t )NULL, ffconfigCWD_THREAD_LOCAL_INDEX );
        return ( int ) pvResult;
    }
    int operator =(int aValue)
    {
        vTaskSetThreadLocalStoragePointer( NULL, ffconfigCWD_THREAD_LOCAL_INDEX, ( void * ) ( aValue ) );
        return aValue;
    }
};

CErrno myErrno;

```

Now the same instance of `myErrno` can be used by any task:

```auto
    myErrno = 0;
    /* call function */
    if( myErrno == 2 )
    {
    }

```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 2, 2017, 4:40pm UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/7 "2017-12-02T16:40:27Z")

</div>

**[thomask](https://sourceforge.net/u/thomask/)** wrote on [Saturday, December 02, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#4acb):

Hein Tibosch wrote:

> I know that in some environments errno has an instance per thread in some magic way,  
> but I don’t think that it will work together with FreeRTOS.

debugasm wrote:

> The Lwip have a routine to set “errno”, I just need to modify this function to use FreeRTOs.

If you are using newlib, you only have to set

configUSE\_NEWLIB\_REENTRANT to 1

in your FreeRTOSConfig.h

In newlib,“errno” and other global variables like “stdout” are defined as macros and will actually point to a task local reent structure. On single core systems, there’s just a pointer called “\_impure\_ptr” that is updated by FreeRTOS on task switch (see task.c).

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 3, 2017, 8:24am UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/8 "2017-12-03T08:24:19Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, December 03, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#ca02):

> If you are using newlib, you only have to set  
> `configUSE_NEWLIB_REENTRANT` to 1  
> in your `FreeRTOSConfig.h`

Oops, yes, I had seen it but as I rarely use newlib, I forgot about it.

Thanks for this explanation.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 4, 2017, 9:12am UTC](https://forums.freertos.org/t/freertos-with-error-h/6864/9 "2017-12-04T09:12:53Z")

</div>

**[debugasm](https://sourceforge.net/u/debugasm/)** wrote on [Monday, December 04, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f6db85b2/#4acb/be58):

I use Xilinx SDK that use “libc”. I don’t have found a way to compile “newlibc” and use it in this environment. So I was building a new library to be used with FreeRTOs usable even in debugging unlike other libraries.

Why does FreeRTOS team not develop an internal library like “Micrium μc / os-xxx”, which integrates the main libraries and then free themselves from external bookcases ?
