Hello ...
is there a way to perform atomic increment or decrement of uint8_t variable ?
Is necessary to use mutex ?
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hello ...
is there a way to perform atomic increment or decrement of uint8_t variable ?
Is necessary to use mutex ?
In addition to mutex provided by OS, you can just disable/enable interrupts (set/clear global interrupt mask).
If only one task will write the variable and other tasks will only read the variable then you don't need to write it atomically. So, sometimes, you can use two variables to prevent mutex. Only task-A will write to variable-A and only task-B will write to variable-B. Both tasks A and B will read variables A and B.
Hi Mauro,
TI-RTOS is a preemptive scheduler: https://dev.ti.com/tirex/explore/node?node=AEwahlHnEy6Wj4XMYzQTrQ__fc2e6sr__LATEST
Best regards,
Sarah
There is nothing to do with "preemptive". If task-A has higher priority than task-B then you can probably remove the atomic protection code (mutex or disable/enable interrupts) in task-A. But it is dangerous to do it this way since the you may change the priority of task A/B in the future. So, it is always safer to use mutex in all tasks which will modify this shared variable.
The CPU of CC3235 is Cortex-M4 which supports LDREX/STREX instructions to do atomic operation. TI-RTOS should utilize these instructions to implement mutex. So, the overhead should be very small to use mutex. But you can also use LDREX/STREX instructions to atomically modify the variable by yourself. You can use intrinsic __ldrexb() and __strexb() in C code.
I have two task:
one put some data in a FIFO and counts how many data are in FIFO
for(;;) { if(numData < AVAILABLE_SPACE) { /* Write Data */ numData ++; } }
the other pop data
for(;;) { if(numData > 0) { /* Read Data */ numData --; } }
how can I use LDREX/STREX ??
I believe you can google a lot of LDREX/STREX examples.
Replace
numData ++;
by
while (1) { uint8_t count = __ldrexb(&numData); count++; if (__strexb(count, &numData) == 0) { asm(" DMB"); break; } }
Replace "numData--" by above code with "count--".
I didn't test the code but it should work. The concept is simple. You should goole & study associated documents to understand how it work.