BaseFlash::state - use critical region to change

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
Post Reply
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

BaseFlash::state - use critical region to change

Post by faisal »

The BaseFlash:state should be able to be read without having to use flash(Acquire/Release)Exclusive in the case of multiple threads using the same BaseFlash. Normally, this isn't a problem - but erase operations can take a long time during which the thread managing the erase may want to get exclusive access to the BaseFlash device (by locking it with the mutex).

Since the BaseFlash::state variable is not modified in a critical region using osalSysLock/Unlock - the only way to safely check the state of the BaseFlash is to acquire the lock - which could end up with the thread getting stalled for a long duration (the duration of an erase). If critical region hygiene is maintained by the BaseFlash drivers using osalSysLock/Unlock to interact with the BaseFlash::state - then other threads can simply read the BaseFlash::state in a critical region and be guaranteed of no race condition.

It comes down to this - querying the state of the BaseFlash (or any module?) should be race-free and quick. There is a tradeoff. For example, you may only be able to see the state of the BaseFlash:state variable this way, and not actually query the device (such as what snor_query_erase would do). But that's better than waiting potentially 10s of seconds (a lifetime in microcontroller time) for feedback on what the state of the BaseFlash is.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: BaseFlash::state - use critical region to change

Post by Giovanni »

Hi,

Why acquire a lock just to read "state"? single read/write operations are atomic, no lock required.

Is there some read-modify-write to consider somewhere?

Giovanni
Post Reply