【问题标题】:Queued Spinlock排队自旋锁
【发布时间】:2012-09-23 19:26:05
【问题描述】:

我偶然发现了 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:
 }
}

这个实现是否正确?我怀疑!

【问题讨论】:

  • codeproject.com/Tips/100195/Queued-spinlocks 似乎是一个合适的实现
  • 啊,是的,对不起,您的问题确实说明了您想要的详细信息。你追求什么细节?排队的自旋锁是自旋锁的链表,每个进入临界区的线程都有一个。文章指出,当在多个处理器上仅使用一个自旋锁时,这有助于解决内存和缓存问题
  • 从概念上讲,这类似于信号量。除了计数器被拆分为布尔值,并放置在链表(fifo)中。如果您想要更多可能有帮助的理论。否则,上面链接的实现是你最好的选择
  • 嘿嘿。我觉得这方面的细节最好放在维基百科上。因此,将尝试输入并返回此处。 p.s.代码是正确的。
  • 只要数据在双字边界上对齐(编译器会这样做),那么cmp 操作就是原子的。

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


【解决方案1】:

此处有问题的代码的作者。首先让我声明代码正确。我只是在这里写了更详细的代码解释: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

是的,我参加聚会有点晚了,但我几天前才发过这篇文章,它启发了我写出更好的代码解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2015-08-21
    • 1970-01-01
    • 2013-12-28
    • 2016-08-12
    • 1970-01-01
    • 2012-06-28
    相关资源
    最近更新 更多