I know this isn't defined anywhere but the seems to be an ambiguity and discrepancy between versions of Unix and Linux over threads and semaphores.According to the Unix spec: per-process.
Do the "SEM_UNDO"s get applied when a thread terminates or when the "whole thing" terminates?
I think that in ipc/sem.c line 1062 the line should be made conditional on "u->semadj[i]" being non-zero.Fixed in 2.6. But there is another bug in that block: undos can increase the semaphore value above SEMVMX.
There is a potential problem here in that the code in ipc/sem.c doesn't allow the adjustment to yield a negative value but what if it starts at zero, thread A increments it, thread B decrements it back to zero (both with SEM_UNDO) and thread A exits first? Thread A's undo won't work and then thread B's undo will increment it again leaving it in an incorrect state which is different from thread B exiting first.Correct. undo operations should never try to decrease the semaphore value - an attempt to decrease below 0 is either silently ignored, or the semaphore value is set to 0.