Share a resource between threads and VT?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Share a resource between threads and VT?

Post by Korken »

Hi,

I'm implementing a message passing (over serial) system which talks to a circular buffer to transmit data as (example):

Code: Select all

void SendMessage(....)
{
  /* Claim the circular buffer for writing */
  chMtxLock(cb_mutex);
  {
    /* Write to Circular buffer for transmission over serial */
  }
  /* Release the circular buffer */
  chMtxUnlock(cb_mutex);
}

However some messages are periodic, and each periodic message is implemented as a Virtual Timer with the correct frequency.
The problem I'm facing and am unable to come to a good solution to is: How do I make the VTs wait until he mutex is unlocked until they try to access the circular buffer?
VT's ISRs in between it is no problem since each ISR is locked from the other ISRs, however if a thread has locked the buffer it must not try to access it.

What is the best way to do this using ChibiOS?
I was thinking of disabling the VTs before locking the circular buffer, and re-enabling them after the thread is finished, but perhaps there is a more elegant way to do it which I have yet to find?

Thanks in advance!
jscott
Posts: 129
Joined: Tue Jul 03, 2012 3:50 pm

Re: Share a resource between threads and VT?

Post by jscott »

Hi Korken,

If I understand your concept correctly, I think I would implement your message passing using a thread not the VT callback.

-- In the thread, you would wait on a semaphore.

-- Then in the VT callback you would signal that semaphore.

-- Then the thread can do whatever it needs, and you can adjust the thread priority in relation to other threads as needed.

This way you can avoid the issues of waiting in an interrupted state and potentially causing missed interrupts
and excessive jitter.

-John Scott
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: Share a resource between threads and VT?

Post by Korken »

Ah yes, that would be a much more elegant way! Thank you! :D

I was thinking of making it a bit more generic, so perhaps the semaphore will be replaced by a Mailbox to be able to pass (different) messages from different VTs.
I will give it a try!
Post Reply