1
\$\begingroup\$

I use ATSAM4LC2A ARM microcontroller that drives 3xUSART, GPIOs, DMA. timers with the use of interrupts and all the remaining logic of my application.. The project is functional but with out a RTOS yet. This is my first attempt and I want to port my code logic with the help of FreeRTOS but I am afraid to use because theadsafety is all new to me.

Is there a software checker that evaluates my code as thread-safe? Or is there a set of written rules that I can use as a ckecklist for every single function I use?

The only rules I can think of are:

  • No use of global variables inside functions.
  • Use mutex if global variables are used..

Also is it better to check for thead-safe or reentrant for my embedded application?

\$\endgroup\$
  • \$\begingroup\$ I recall our custom RTOS used renetrant code but with a 8khz IRQ for digital DS1 {dual FXD 1.544Mbps} our RTOS concentrator/Mux for multi-IO streams used prioritized messages ranked by severity of task and push/pop the stack on every IRQ. This was circa '79 using Motorola CPU chip. The only requirement was that no single task could exceed the IRQ interval otherwise a real-time failure could occur., from cumulative stack overflow \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 Nov 6 '16 at 17:04
  • \$\begingroup\$ See What is meant by "thread-safe" code? or the Wikipedia article on thread safety. \$\endgroup\$ – kkrambo Nov 7 '16 at 14:38
  • 1
    \$\begingroup\$ If your code is working without out an RTOS, why do you want to incorporate an RTOS? \$\endgroup\$ – kkrambo Nov 7 '16 at 14:40
  • 1
    \$\begingroup\$ I wait for a GSM and a GPS module to initialize. I want stufff to happen in parallel in order to stay low power. There are some good reasons. Also for the expierience for future projects. \$\endgroup\$ – Tedi Nov 8 '16 at 21:46
1
\$\begingroup\$

your best bet here is to follow the idom "move fast and break things". Unless you project is a nuclear reactor core I suggest you try and see what works and what doesn't, if you have mentors around ask them for help if you get stuck and are unable to figure it out.

In general an RTOS will provide ways of sharing data between tasks using mailboxes and locks. The over thing is in a flat memory model without an MMU certain library functions will have re-entrant versions that should be used. For example strtok uses storage to store the current location in the string, if two threads are using strtok then confusion may arise between successive calls. In this cas strtok_r should be used.

This paper on C/C++ Thread Safety Analysis is a good read.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.