lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] 'Avoiding sys_thread_new' and 'RTOS Integration'


From: Freddie Chopin
Subject: Re: [lwip-users] 'Avoiding sys_thread_new' and 'RTOS Integration'
Date: Mon, 10 Oct 2016 10:04:45 +0200

On pon, 2016-10-10 at 08:58 +0200, Dirk Ziegelmeier wrote:
> You can't. All you can do in such cases is count how many
> mboxes/semaphores/threads etc. you need and allocate their storage
> statically. A call to sys_mbox_new() just returns "the next one" from
> the static allocated ones. This is what we do at work in constrained
> devices.

?

Even in lwIP 1.4.1 the prototype for these functions was:
err_t sys_sem_new(sys_sem_t *sem, u8_t count);
err_t sys_mbox_new(sys_mbox_t *mbox, int size);

These functions clearly don't return anything else other than error and
the buffer for semaphore/mailbox has to be provided by user.

These can work the way you describe but only if you define sys_sem_t
and sys_mbox_t to be pointers, which is not the best approach. If you
define them as storage with the same size and alignment as the "real"
semaphore/mailbox, then these functions can just construct the object
into the storage, with no memory allocation (except the storage for
mailbox contents).

For example this is what I have in lwIP port to my RTOS.

in sys_arch.h:

---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----

...

/// struct with same size and alignment as distortos::Semaphore
struct distortosSemaphore
{
        /// implementation details
        const void* const implementation[4];
};

/// lwIP semaphore
typedef struct distortosSemaphore sys_sem_t;

...

---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----

Then in some source file:

---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----

...

void sys_sem_free(sys_sem_t* const semaphore)
{
        reinterpret_cast<distortos::Semaphore*>(semaphore)->~Semaphore();
}

err_t sys_sem_new(sys_sem_t* const semaphore, const u8_t count)
{
        new (semaphore) distortos::Semaphore {count};
        return ERR_OK;
}

...

---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----

There is no dynamic allocation here and this works perfectly fine...

Regards,
FCh





reply via email to

[Prev in Thread] Current Thread [Next in Thread]