Inserting a semaphore into ch_thread

Discussions and support about ChibiOS/RT, the free embedded RTOS.
szekelyisz
Posts: 10
Joined: Wed Jan 03, 2018 8:15 pm

Inserting a semaphore into ch_thread

Postby szekelyisz » Fri Jan 12, 2018 12:28 am

Hi,

full-duplex socket operation in LWIP 2.0 requires a dedicated semaphore per thread. I found the CH_CFG_THREAD_EXTRA_FIELDS macro which enables additional fields in the thread structure with CH_CFG_THREAD_INIT_HOOK and CH_CFG_THREAD_EXIT_HOOK to initialize and finalize them. So far so good.

But if I add a struct ch_semaphore as a new field, I get "has incomplete type" compilation errors from all around the OS code, which is understandable.

If I add a struct ch_semaphore*, I need to actually allocate the memory for the semaphore, which can be done with CH_CFG_THREAD_INIT_HOOK but I have no idea how to allocate memory there as chHeapAlloc() cannot be called from a lock zone in which _thread_init (that calls CH_CFG_THREAD_INIT_HOOK) resides. Allocating the memory with chCoreAllocI() seems like a bad idea as it can't be freed.

Any idea what the solution is here? Thanks!

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Inserting a semaphore into ch_thread

Postby Giovanni » Fri Jan 12, 2018 8:45 am

Hi,

You could use a memory pool feeding from the core allocator, the memory would be returned to the pool.

Giovanni

szekelyisz
Posts: 10
Joined: Wed Jan 03, 2018 8:15 pm

Re: Inserting a semaphore into ch_thread

Postby szekelyisz » Sat Jan 20, 2018 12:08 am

Thanks Giovanni, but I found my way around this. LWIP provides the LWIP_NETCONN_THREAD_SEM_ALLOC() and LWIP_NETCONN_THREAD_SEM_FREE() macros which are called if the thread semaphore is invalid (as defined by the sys_sem_valid() function in sys_arch.c) or not needed anymore, respectively. This enables any type of allocation.


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 6 guests