Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.arch.embedded > #29298
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.arch.embedded |
| Subject | Re: Cortex-M: share an int between two tasks |
| Date | 2020-03-18 10:06 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <r4sobf$gk9$1@dont-email.me> (permalink) |
| References | <r4g5sc$8jr$1@dont-email.me> <r4slk8$1cb$1@dont-email.me> |
On 18/03/2020 09:20, pozz wrote:
> Il 13/03/2020 15:38, pozz ha scritto:
>> I'm working on a Cortex-M4 MCU and using FreeRTOS.
>>
>> One task:
>>
>> uint32_t wait_period;
>> while(1) {
>> // make some things
>> vTaskDelay(pdMS_TO_TICKS(wait_period));
>> }
>>
>> The following function can be called from another task:
>>
>> void set_waiting_period(uint32_t new_period) {
>> wait_period = new_period;
>> }
>>
>>
>> In this case, is it needed to protect the access of the shared
>> variable wait_period with a mutex? I don't think, we are dealing with
>> an integer variable that should be accessed (reading and writing)
>> atomically.
>
> Ok the example was extremely simple. If I have something a little more
> complex:
>
> ---
> volatile uint32_t wait_period;
>
> while(1) {
> // make some things
> if (wait_period > 0) {
> vTaskDelay(pdMS_TO_TICKS(wait_period));
> } else {
> vTaskSuspend(myTask);
> }
> }
>
> void set_waiting_period(uint32_t new_period) {
> wait_period = new_period;
> if (wait_period > 0) {
> vTaskResume(myTask);
> }
> }
> ---
>
> I think this doesn't work in all the cases. For example, if
> wait_period=0 and set_waiting_period(3) is executed from another higher
> priority task immediately after
>
> if(wait_period > 0) // the if in the endless loop
>
> wait_period is changed to 3, but vTaskSuspended() is executed in the
> else branch, so leaving the task suspended with a not-null wait_period.
>
> Do I need a mutex in this case to protect wait_period access in both
> points?
>
> Pardon for the silly question, but I'm very new to multitasking and RTOS.
When you have more complex examples, you start needing synchronisation
of some sort (it can be a disabled interrupt block, a lock like a mutex,
or message passing, or something similar). Sometimes it can be handled
with lock-free methods.
Generally, if you have one reader and one writer, volatile will be
enough - but you have to think about exactly how you want to arrange the
accesses.
I find one of the best ways to get good software design is to think
about it like hardware. You don't have one hardware signal controlled
by various different signals scattered around the design - if you have
an LED that can be controlled by different signals, you put these
signals into a multiplexer or logic gate (or wired or / wired and). And
you collect them all together in one place in the design to do that.
The same applies to software. Don't change a variable from a dozen
different places around the program. Don't access the same resource
from different places. Put in queues, multiplexers, etc., so that you
have control.
In a case like this, the idea that you have your "myTask" being
suspended from one thread and woken up by a different thread is your
flaw. Fix the design - the suspension and resumption of "myTask" should
happen from /one/ place in /one/ thread - and your problems of
synchronisation go away.
Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-13 15:38 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-13 17:49 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-18 09:09 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 09:17 +0100
Re: Cortex-M: share an int between two tasks Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2020-03-18 17:37 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 19:23 +0100
Re: Cortex-M: share an int between two tasks Les Cargill <lcargill99@comcast.com> - 2020-03-22 13:42 -0500
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-22 21:45 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-22 20:50 -0400
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-23 09:09 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-23 08:00 -0400
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-23 14:21 +0100
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-03-25 09:55 +1100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-30 16:07 +0200
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-03-31 09:02 +1100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-31 10:57 +0200
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-04-01 12:05 +1100
Re: Cortex-M: share an int between two tasks Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2020-03-30 17:49 +0300
Re: Cortex-M: share an int between two tasks kegs@provalid.com (Kent Dickey) - 2020-05-19 00:34 -0500
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-05-19 17:40 +1000
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-05-19 12:00 +0200
Re: Cortex-M: share an int between two tasks Tom Gardner <spamjunk@blueyonder.co.uk> - 2020-05-19 13:48 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-18 09:20 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 10:06 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-18 10:59 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 11:48 +0100
Re: Cortex-M: share an int between two tasks upsidedown@downunder.com - 2020-03-18 20:37 +0200
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 21:24 +0100
Re: Cortex-M: share an int between two tasks Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2020-03-19 14:05 +0200
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 13:16 +0100
Re: Cortex-M: share an int between two tasks Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2020-03-19 14:27 +0200
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 14:13 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 08:03 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 09:10 +0100
Re: Cortex-M: share an int between two tasks upsidedown@downunder.com - 2020-03-19 12:51 +0200
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 07:40 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 09:38 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 11:26 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 12:12 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 15:31 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-23 09:22 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-23 08:10 -0400
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-23 13:47 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-23 14:31 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-23 13:50 -0400
Re: Cortex-M: share an int between two tasks Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-03-19 12:27 +0200
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 11:45 +0100
Re: Cortex-M: share an int between two tasks upsidedown@downunder.com - 2020-03-19 13:27 +0200
Re: Cortex-M: share an int between two tasks antispam@math.uni.wroc.pl - 2020-03-18 16:24 +0000
csiph-web