Semaphore: why is the interrupt required with the test suite disabled?

Turning to these implementations of the semaphore with samples (for SMP systems), I understand that the test suite is necessary for multiprocessor atomic checks. However, as soon as we add that atomic checks are not disabled interrupts redundant? In any case, disable interrupts, only atomicity of an atom on one processor. The addition to the semaphore queue also needs to be protected.

class semaphore { private int t; private int count; private queue q; public semaphore(int init) { t = 0; count = init; q = new queue(); } public void P() { Disable interrupts; while (TAS(t) != 0) { /* just spin */ }; if (count > 0) { count--; t = 0; Enable interrupts; return; } Add process to q; t = 0; Enable interrupts; Redispatch; } public V() { Disable interrupts; while (TAS(t) != 0) { /* just spin */ }; if (q == empty) { count++; } else { Remove first process from q; Wake it up; } t = 0; Enable interrupts; } 

}

+6
source share
1 answer

Although it is true that interrupting interrupts on one processor is not enough to provide access to atomic memory in a multiprocessor system (since, as you say, threads on other processors can still access shared resources), we interrupt interrupts for part of the multi-processor implementation of the semaphore, because we don’t want us to be planned while we do the test and install.

If the thread containing the test and the set is delayed, no other threads can do anything with the semaphore (because its account is protected by this test and installed) the thread used during sleep mode (this is not good). To ensure that this does not happen, we will disable interrupts on our processor when using the test and installation.

+7
source

Source: https://habr.com/ru/post/979898/


All Articles