排队的Spinlock

时间:2012-09-23 19:26:05

标签: c++ synchronization linux-kernel mutex spinlock

我碰巧偶然发现了Queued Spinlock并希望用C ++实现。我搜索了一下有关此信息,但无法获得适当的文档。

非常感谢任何文档/实施技巧。

提前致谢

我在Michael Brown指出的代码中有以下疑问

// represents processor in wait queue of the spinlock
struct qsl_entry
{

// next processor in the queue that is waiting to enter section
qsl_entry* next;

// indicates whether the access to section has been granted to processor
int state;

};

// queued spinlock
struct qsl
{

// the first processor in the queue that is waiting to enter section
qsl_entry* head;

};

// requests access to critical section guarded by the spinlock,
// if the section is already taken it puts processor to wait
// and insert it into queue
// lck - queued lock that used to guard section
// ent - entry that represent processor in queue of the spinlock
void lock_qsl(qsl* lck, qsl_entry* ent)
{
__asm
{
    mov eax, ent;
    mov ebx, lck;

    // prepare queue entry
    mov [eax], 0;
    mov edx, eax;
    mov [eax]qsl_entry.state, 1;

    // store it as the last entry of the queue -- Is this what is line is doing ?
    // ebx contains address of lck & [ ebx ] refers to address pointed by lck & 
    // it is over written to ent. eax now contains the memory the lck was pointing to.
    lock xchg [ebx],eax;

    // if the section available grant access to processor?
    test eax, eax;
    jz enter_section;
        // link new entry with the rest of queue -- really ? are we nt overwritting
        // the next pointer here ?
        mov [eax],edx

        // wait for processor's turn
        wait1:
            pause;
            cmp [edx]qsl_entry.state, 1;
            je wait1;

    enter_section:
 }
}

这种实施是否正确?我对此表示怀疑!

1 个答案:

答案 0 :(得分:3)

此处代码的作者。首先让我说明代码是正确的。我刚刚在这里写了更详细的代码说明:http://kataklinger.com/index.php/queued-spinlocks/

此外,我还有另一个实现,它稍微简单一点,但不如此实现(尽管如此)。 我会看看我能在某个地方找到它。我找到了它。以下是包含两种实现的讨论链接:http://forum.osdev.org/viewtopic.php?f=15&t=15389

最后一篇文章还提供了更深入讨论排队螺旋锁的链接:http://www.cs.rice.edu/~johnmc/papers/tocs91.pdf

是的,我参加派对的时间有点迟了,但几天前我就是这篇文章了,这让我想起了更好的代码解释。