I think enter/leave_critical_section() call should be replaced with semaphore/mutex/spinlock/atomic_ in most case, not only more standard but also more fast in SMP. Especially, multiple core MCU/DSP design extend from desktop to mobile and embedded now.
I would consider just removing the enter/leave_critical section and replacing it with nothing. I don't think that will cause any major issues.
So this case would only apply if two tasks are concurrently trying to set the syslog mask at the same. But no matter what you do, nothing can make this work in the concurrent case: One call to set the logmask will work and the other will fail to set the log mask (because the logmask will be overwritten by the other task).
There is not much difference in the behavior if the enter/leave_critical_section() present or not. WITH the the critical section, the oldmask will be exactly the old logmask before the new one was set. But that does not matter: In the concurrent access case, the logmask might immediately overwritten on a different thread.
The old logmask is usually not used. It is used only if you wanted restore the logmask to the previous value later. But the old logmask is not even reliable in the concureent case WITH the critical section: The old logmask might the correct, original logmask or it might the new logmask set by the other process. So it would be useless to restore the log mask in that case.
So, I think we should just avoid the unnecessary complexity. Just remove the critical section altogether. It is not needed; it does not guarantee correct operation. And I would add some comments explaing the race condition.
Basically, if you plan to set the logmask. You have to do it the the only safe thing is to go it on a single thread.
One way to make this work correctly would be to remove the logmask as a global variable and, instead, put the logic mask in the group structure or in the TCB structure. That would make logmask thread safe (in the second case). This would be the preferred solution. This solution would work. The other suggestions will not work and just implement more complex version of the broken code.
I would vote: - Remove the critical section and replace it with nothing, or - Move the logmask into the TCB Every other suggestion made here is technically wrong. Greg