The GNSS task updates the latest GPS location into a shared global variable, while the HTTP task runs independently every one minute and reads the latest value to send to the server.
I believe there is a risk of data corruption due to a race condition. The issue arises because the GNSS task might be updating the location data at the exact same time the HTTP task is reading it. Since GPS data consists of multiple fields such as latitude and longitude.
Because of this, the HTTP task may read partially updated data for example, a new latitude combined with an old longitude which results in inconsistent or corrupted data being sent.
To prevent this, I believe proper synchronization is required when accessing the shared variable. The simplest approach I think is to use a mutex to ensure mutual exclusion.
In this approach, The GNSS task takes the mutex before updating the location data. The HTTP task takes the mutex before reading the data
This ensures that only one task can access the shared variable at a time, preventing concurrent read/write operations and avoiding data corruption.
Do you agree with this, or would you suggest a better way to handle this scenario?
Your “risk of corruption” is precisely why I mentioned the need of access protection. A mutex is one option, my preference would be a critical section around a copy operation. I prefer this as the cost of manipulating the mutex is at least on the order of the copy, and will use critical sections within it. Thus directly using a critical section makes sense. For longer operations, you do want to use a mutex.
For a single core system, this will be basically a disable interrupts. Multi-core systems running in symmetric mode will often use a spin lock combined with disabling interrupts.
@rvt based on your comment, you either need to use the use the resources provided by FreeRTOS if you want to be able to depend on its certifications, or you need to do the work yourself to understand the full system to do your own proof of correctness.
I would say that mixing in pieces not part of the provided system into a FreeRTOS system is just REDUCING the clarity of what is happening.
I suppose the question is why did you even try to implement your own version of a “critical section” when the OS you are using provides something that does exactly what you wanted to do in the first place?
And I presume you mean RAII (Resource Acquisition is Initialization) not RIAA (a music licensing company). You could, if you want that sort of wrapper, do that around the actual FreeRTOS operation in similar amounts of code (although your code is NOT a “RAII” class.
I do have a “Lock” class that uses mutexes to make a RAII region for locking a mutex. I haven’t made a class for the simpler critical section, as my rules for using them doesn’t lead to a need for it, as critical section at user code level should be small and short so the direct calls are easily matched.
I had a misunderstanding earlier that every task should be treated as a periodic task. The reason for this was that I often saw people, before designing a system, first decide the deadlines for each task. So I assumed that every task must be periodic in order to meet its deadline.
But now, I feel that this is not the right way to think about RTOS system design. Instead, we should first understand the nature of each task whether it is periodic, event-driven, or interrupt-driven and then design the system accordingly.
From discussion with you, I have learned that choosing the correct type of task is more important than assuming everything is periodic.
Most task will be “periodic”, but the period might not be strictly time based (it might be every time a door is opened or a button pressed). Most task work in response to something that causes them to have something to do, and the whole concept of “Real Time” is that these operations tend to have time limits for the response to occur.
I find very few tasks actually are working on a strictly “time” basis, although other designers might have more measurement system that have a time base to make measurements.
In one sense, “Time” and “Interrupts” are just special names for certain types of “Events”.
And ultimately, every event starts with an interrupt, even if it is just the tick interrupt telling us it it is time to look if something has happened.
I am making some general rules to better understand system design. such as we should start by identifying whether a task is periodic, event-driven, or interrupt-driven, and based on that, we should decide how it needs to be scheduled.
we should check whether the task is triggered by an ISR, so we can choose the appropriate communication mechanism, such as queues or semaphore.
We should also analyze whether tasks share any resources or data. If they do, we should decide the correct synchronization technique such as a mutex, semaphore,
Do you want to add any additional rules that you typically follow while designing systems?
I would say you have started at the wrong step. You don’t even HAVE “tasks” until you have looked at the system requirements.
I start with the system requirements of what the system is to DO. In this case the (partial) list is to get the GPS location and periodically report it to a server via HTTP.
When can then list out the various components the system:
A GPS receiver, likely with a serial interface
This means we need hardware/software to perform serial communications
We need some code to setup the GPS and process its output. This is likely a module we want to be able to use for other applications, so we want to think of it as a reusable block.
We need a Ethernet Connection of some sort to the HTTP server
We need a Ethernet Stack that can process messages from applications to the hardware.
We need a set of code to tranlate the generic GPS information into the format that allows it to be posted to the servers API
We can then extract out a list of software pieces that seem to be needed:
Serial Driver, we can likely use a fairly generic driver which is good for reuse.
A GPS control module connected to that serial port. This likely can be a fairly generic module usable in many system, perhaps with a bit of custom code for the particular GPS module.
Some code to translate the data from that GPS module to API requrests.
An Ethernet Stack to transfer those messages into Ethernet messages.
We can then think about how to implement these pieces.
Serial Driver: I define a generic API that supports most of the common features of any communications port, with a specific implementation for that particular hardware. In fact, my generic serial port API also has a more generic “streaming” API for any link that can just stream data (like perhaps to a file system).
GPS Module: Again a generic definition for generic GPS with a specific type of Data Format, which is connected to the Communication port that the GPS is on. There will likely also be a bit of device specific additions for the specific GPS. As a reusable block, it will define a notification interface to alert other modules of new data, and an interface to get the “current” data.
The code that takes this data, once a minute, and posts it to the external server.
A Network Stack connected to the network hardware.
Note, to this point I haven’t talked about what is a “task”, what is an “ISR”, or what is something else. This is the point that we can make those decisions.
Serial Port: Since the serial port driver doesn’t know about the use of the data, there isn’t much processing to do. Most serial ports can be interrupt based, so this is likely an ISR based driver. Note, except for performance risks, there is nothing preventing the driver from being. implemented as a Idle level task that polls the serial port (hopefully with a large FIFO) and reads the data and puts it into the Queue/StreamBuffer, but that method does require the system to normally be idle.
GPS Module. Since this module is responding to data coming in “asyncronously” (not in responce to a immediately preceeding request) this is best implemented with a base task. Some work needs to go into how it will notify other modules that want to be notified. One option is an EventGroup. Another option would be to have a list of call-backs (call-backs do require the call-backs to be strictly limited in how much work the call back can do).
Posting to Server: This needs a bit of thinking for design requirements. If we want it to be able to confirm recept of the message, this likely needs to be a task of its own.
Ethernet Stack: You seemed to have missed this, likely because you think of it as being “supplied”, but it is part of the system. Since Ethernet stacks need to respond to asyncronous events (packets coming in, and complete packets going out) and the work on incomming packets is more than you really want to do in an ISR, this likely has a task (or more) of its own.
In performing this step, we automatically make the decision of “type” that you talk about. And now we should look deeper at processing scheduling. As long as the serial driver is given a receive buffer big enough for a GPS status message, we actually have very little “hard” requirements on the processing, and you RPi is much more processor than you need. I would say the biggest stress on system requirements is the network stack and how much memory it will want to use.
Note, most of your details of implementation are decisions within a given module implementation. For instance, the getting of the current GPS status is likely done in a function that is part of the GPS module, that puts that status into a buffer provided, so the syncronization decision is a PRIVATE decision in that module, not part of its API.