Mutex, Semaphore, neither, other?

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

rbarreiros
Posts: 27
Joined: Sat Mar 28, 2015 2:32 am

Mutex, Semaphore, neither, other?

Postby rbarreiros » Fri May 04, 2018 11:08 pm

Hi,

I made a small network stack that's just meant to do ARP/ICMP and UDP, so its really small and fast (faster than uIP) and I have 1 thread handling the network traffic that calls callbacks based on which port the higher level api has registered. The network buffer is shared for data received and sent (like uIP) to save memory. So thread is something like this in pseudo code:

Code: Select all

While(1)
{
  Wait interrupt with data ready to read
  If read size > 0
    Process packet
  Endif
}


Now, while the packet is being processed, the buffer contents can be changed and used as the reply data if there is a reply, so far so good, but, sometimes, I have to send a sporadic packet outside of that thread packet processing, I have to figure out a way of just sending those packets when there is no packet being processes.

How would you guys tackle this issue ?

Best regards.

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

Re: Mutex, Semaphore, neither, other?

Postby Giovanni » Sat May 05, 2018 6:46 am

It is not very clear to me, do you have a single buffer?

Giovanni

rbarreiros
Posts: 27
Joined: Sat Mar 28, 2015 2:32 am

Re: Mutex, Semaphore, neither, other?

Postby rbarreiros » Sat May 05, 2018 9:50 am

Exactly, that can't be changed by anything outside the network thread as soon as its started to be processed.

But sometimes I have to use the same buffer to send sporadic packets outside the thread processing loop.

Mutex on the buffer?

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

Re: Mutex, Semaphore, neither, other?

Postby Giovanni » Sat May 05, 2018 9:58 am

Yes, mutex on buffer, assuming that the buffer is contended only by threads, not ISRs nor callbacks.

Giovanni

rbarreiros
Posts: 27
Joined: Sat Mar 28, 2015 2:32 am

Re: Mutex, Semaphore, neither, other?

Postby rbarreiros » Sat May 05, 2018 6:51 pm

But he is used in callbacks. The application registers a callback tied to a port, then, in the thread, if the packet matches the registered destination port, calls the callback to process the data (same buffer, pointer at the data index), is it still safe to use the mutex like this?

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

Re: Mutex, Semaphore, neither, other?

Postby Giovanni » Sat May 05, 2018 7:02 pm

You cannot use mutex from callbacks of course.

The only way to share something between an ISR and a thread is a critical zone, chSysLock()/chSysUnlock() but that is not always possible if you need to perform long operations,

An alternative would be your ISR delegating the access to a dedicated thread, then the mutex would be usable.

Giovanni


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 17 guests