通过这个示例信号量实现(对于SMP系统),我理解多处理器原子检查需要测试和设置.但是,一旦我们添加原子检查不是禁用中断冗余?无论如何,禁用中断仅在一个处理器上提供原子性.除了信号量队列之外,还需要加以保护.
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 …Run Code Online (Sandbox Code Playgroud)