I have been trying with multiple tutorials to implement a simple spin lock in pure c (i.e. with no OS specific stuff).
This is the last i have come up with
myFile.c
static atomic_flag mg_lockAllBuffers = ATOMIC_FLAG_INIT;
/**
* @brief Busy blocks infinite until the buffer is unlocked
* @param pHandle - Pointer to the buffer
*/
static inline void waitAndLockConcurrentAccess();
/**
* @brief unlocks the buffer
* @param pHandle - Pointer to the buffer
*/
static inline void unlockConcurrentAccess();
static inline void waitAndLockConcurrentAccess()
{
// Atomic load
int dummy = 0;
while (atomic_flag_test_and_set(&mg_lockAllBuffers))
{
// busy wait
dummy++;
}
}
static inline void unlockConcurrentAccess()
{
atomic_flag_clear(&mg_lockAllBuffers);
}
However GCCs thread sanitiser is complaining about a data race within waitAndLockConcurrentAccess(). But I do not know why.
Read of size 8 at 0x7f3e05b45ed0 by thread T2:
Previous write of size 8 at 0x7f3e05b45ed0 by thread T1:
Could you help we out here?
thx guys