I'm feeling a bit overwhelmed when using multiple threads in embedded programming since each and every shared resource ends up with a getter/setter protected by a mutex.
I would really like to understand if a getter of the following sort
static float static_raw;
float get_raw() {
os_mutex_get(mutex, OS_WAIT_FOREVER);
float local_raw = static_raw;
os_mutex_put(mutex);
return local_raw ;
}
makes sense or if float
assignement can be considered atomic e.g. for ARM (differently from e.g 64bit variables) making this superfluous.
I can understand something like this:
raw = raw > VALUE ? raw + compensation() : raw;
where the value is handled multiple times, but what about when reading or returning it?
Can you make my mind clear?
EDIT 1: Regarding the second question below. let's assume we have an "heavy" function in terms of time execution let's call it
void foo(int a, int b, int c)
where a,b,c are potentially values from shared resources. When the foo function is called should it be enveloped by a mutex, locking it for plenty of time even if it just needs a copy of the value? e.g.
os_mutex_get(mutex, OS_WAIT_FOREVER);
foo(a,b,c);
os_mutex_put(mutex);
does it make any sense to do
os_mutex_get(mutex, OS_WAIT_FOREVER);
int la = a;
int lb = b;
int lc = c;
os_mutex_put(mutex);
foo(la,lb,lc);
locking only the copy of the variable instead of the full execution?
EDIT2: Given there could exist getter and setter for "a", "b" and "c". Is it problematic in terms of performance/or anything else in doing something like this?
int static_a;
int get_a(int* la){
os_mutex_get(mutex, OS_WAIT_FOREVER);
*la = static_a;
os_mutex_put(mutex);
}
or
int static_b;
int get_b(){
os_mutex_get(mutex, OS_WAIT_FOREVER);
int lb = static_b;
os_mutex_put(mutex);
return lb;
}
using them as
void main(){
int la = 0;
get_a(&la);
foo(la,get_b());
}
I'm asking this because im locking and relocking on the same mutex sequentially for potential no reason.