Yes and no. The atomicity is local, for as far as possible. There is a single operation that does require kernel help, that is blocking without allowing a moment of rest.ineo wrote: @Candy: In fact you're speaking of a kind of "local" atomicity.
For a perfect semaphore (since a semaphore is only for security, it's useless unless perfect) you need to be sure that it works perfectly every possible way. If there is a way to bypass the semaphore, it will be used some time.However I don't see the point here, maybe I am missing something. Since CLI/STI are not really required for semaphore (you could use spinning loops to avoid it, and that's how it works on multiprocessor systems), You could use semaphore at any level you like without any kernel code.
For user code, it's very inefficient to switch to the kernel and use a kernel-bound semaphore function. Using a userlevel semaphore function is a lot more efficient. The CLI/STI idea was only to make sure the cpu doesn't interrupt you in your semaphore modifying operation, so that every other thread would spin for a while. It's not required in any way, only a performance enhancement.
You CAN NOT block a thread using a kernel call if you're trying to take a semaphore in between. This is not perfect.
When an interrupt would occur during the short period of time that you have decided that you can not down the semaphore, and before you have called the kernel-block function, you'd be in state of limbo. If another thread was started at that point, and raised the semaphore, signalling all waiting threads (not you!), then letting you at it again, you block yourself and wait indefinitely for the semaphore that is there, but that you do not see.
I don't see why you would have to use a system-managed semaphore for a shared resource. In the case where you trust both sides (IE, you made them or something), you can use the userlevel function. In case of no trust, you cannot trust that the other will leave any objects intact, so you have to protect it all with a kernel interface anyway. In the paradoxal situation that you would have a shared object where you do trust the object not to be modified, but cannot risk the semaphore to be wrong, you have a point with a kernel-level semaphore. The only place I see this is shared memory between user programs, with a kernel semaphore that counts the usage.However You will need some "system managed" semaphores for shared resources. But there again it may be implemented using a user task (aka server in my microkernel).
Could you be more precise ? Or explain, because I feel like I am missing something important.
But in that case, the entire map call is kernel bound, so you're in kernel anyway.
AFAIK we have two different types of semaphores, the kernel-level and the user-level. Those at kernel level can always be trusted to be right, are for kernel use only and protect kernel and user level things. Those at user level can always be trusted to be right, are for user use only, have kernel support for the atomic block and protect user level things.