Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.arch.embedded > #29310
| From | pozz <pozzugno@gmail.com> |
|---|---|
| Newsgroups | comp.arch.embedded |
| Subject | Re: Cortex-M: share an int between two tasks |
| Date | 2020-03-19 11:26 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <r4vhdd$d8d$1@dont-email.me> (permalink) |
| References | (2 earlier) <r4sobf$gk9$1@dont-email.me> <r4srd8$2i9$1@dont-email.me> <r4suan$keu$1@dont-email.me> <r4v447$4ms$1@dont-email.me> <r4vb1h$8r8$1@dont-email.me> |
Il 19/03/2020 09:38, David Brown ha scritto:
> On 19/03/2020 07:40, pozz wrote:
>> Il 18/03/2020 11:48, David Brown ha scritto:
>
>>> If you are using "suspend" and "resume" directly, you are almost
>>> certainly doing it wrong - no matter which threads are doing what.
>>>
>>> Suspension of a task should be for a reason. If you are waiting for
>>> some time, use vTaskDelay (or similar functions). If you are waiting
>>> for data from another thread, wait for that - receiving from a queue,
>>> waiting on a semaphore, a condition variable, etc.
>>>
>>> Resumption of a task should be for a reason - send it a message in a
>>> queue, set the semaphore or condition variable, etc.
>>>
>>> "suspend" and "resume" are low-level "beat it with a hammer" approaches.
>>
>> I didn't know that suspend/resume mechanism shouldn't be used in
>> normal cases like mine.
>>
>> I simply wanted a management taskB to stop and restart sensor sampling
>> done by taskA. taskA uses vTaskDelay() for sampling at precise timings.
>
> You want vTaskDelayUntil, not vTaskDelay.
Oh yes.
>
>>
>> Suspend/resume appeared to me the most natural way to implement this
>> stop/restart feature.
>>
>
> It would be possible, but it does not look good to me. I'd be looking
> at an atomic flag (volatile bool, pre-C11) to indicate task A should be
> shutting down after the current sample, then wait on a semaphore. But
> I'd need a clearer picture of the whole system and interaction between
> threads.
This is what I thought, but I'm not able to implement it without any
race conditions.
// TaskA
static volatile bool stopped;
static volatile unsigned int delay_ms;
stopped = false;
while(1) {
// get sample and process
if (stopped) { (1)
vTaskDelay(portMAX_DELAY); (2)
} else {
vTaskDelay(pdMS_TO_TICKS(delay_ms));
}
}
void set_delay(unsigned int new_delay) {
// This is TaskB
if (new_delay == 0) {
stopped = true;
} else {
stopped = false;
delay_ms = new_delay;
vTaskDelayAbort(taskA);
}
}
This code has some race conditions if both tasks can be interrupted in
any point by the other.
For example, if stopped is true and taskB enters immediately after (1)
and before (2) with set_delay(3), I will have taskA blocked forever!!
Could you suggest a solution with a simple volatile bool flag?
>> Thank you for your suggestions. I think I will use a queue and events
>> pushed from taskB and read from taskA.
>
> That can usually work, but might not be the most efficient. I don't
> know if efficiency is important here. "Direct to task notifications",
> "Event groups", and "Message buffers" are topics you might want to read
> about on the FreeRTOS pages. In particular, communication that only
> goes from one known task to another known task can be a lot more
> efficient than general queues.
>
>>
>> After many years using cooperative schedulers, it appears to me
>> incredible how difficult, error prone and resource consuming is to
>> implement even simple mechanisms like this one with a preemptive
>> scheduler.
>>
>
> Multi-threading /is/ more difficult, and you need to understand a lot
> more. It is very, very easy to make something that is completely wrong,
> and hangs at unexpected times, and is difficult to debug. On the other
> hand, it is also easier to write the parts of the system in separate
> blocks, living their own lives without having layers of state machines
> or always thinking about breaking work up into little bits. The keep
> point is to be very clear about the communication between threads and
> any shared resources - keep this all to a minimum. As much as possible,
> make your threads self-contained - a C file contains only one thread,
> and all functions it calls. Any common functions in common files should
> be re-entrant.
>
> An RTOS has a lot of benefits, but it is not a free lunch.
I'm starting understanding this.
>> There are many subtle things that could happen in a preemptive
>> scenario and you must open your eyes very well in each part of code to
>> avoid race conditions that will be manifested only to the user and not
>> during testing.
>>
>> Is there a way to test for the presence of dangerous race conditions?
>> Or is this demanded to design only?
>>
> You can never use testing to show the lack of bugs - only the presence
> of bugs. Race conditions often only trigger in rare timing
> coincidences, so you can't rely on tests. You have to design the code
> so that race conditions don't occur.
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