Inserting a semaphore into ch_thread

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
szekelyisz
Posts: 11
Joined: Wed Jan 03, 2018 8:15 pm
Has thanked: 1 time

Inserting a semaphore into ch_thread

Post by szekelyisz »

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: 14890
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Inserting a semaphore into ch_thread

Post by Giovanni »

Hi,

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

Giovanni
szekelyisz
Posts: 11
Joined: Wed Jan 03, 2018 8:15 pm
Has thanked: 1 time

Re: Inserting a semaphore into ch_thread

Post by szekelyisz »

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.
Post Reply