I defined a shared mutex for processes, in /dev/shmem
The mutex is for protecting the access to a hardware block. Each operation might last for 2 ms.
Wait for “operation completion” interrupt.
When interrupt comes, unlock the mutex;
(if the user wants to stop, unlock the mutex).
Lock the mutex,
Set up the next operation.
I wanted to only unlock the mutex if it was being locked, therefore, I add some check, an global_variable is_mutex_locked.
Don’t use global variable to indicate if the mutex is locked, or unlock it.
Wait for the operation complete (by reading a DMA status register);
Tell the dedicated thread to exit (by setting a global variable g_dead).
Wait for the dedicated thread exit;