2012-09-23 25 views
1

我偶然碰到排队的自旋锁,并想用C++实现。我搜索了一下这方面的信息,但无法获得正确的文档。排队的自旋锁

任何文档/实施技巧将不胜感激。

在此先感谢

我的代码如下疑问迈克尔·布朗

// 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: 
} 
} 

指出这是实现甚至正确吗?我很怀疑!

+2

http://www.codeproject.com/Tips/100195/Queued-spinlocks似乎是一个合适的实现 –

+0

感谢您的链接,但我已经发现它在我的谷歌搜索。我应该在我的问题中加上这个。问题是它似乎没有太多的文档与它相关联。对于像这样的复杂话题,一些直觉会很好。 – KodeWarrior

+1

啊是的,即时通讯不好意思,你的问题确实说明你想要的细节。你以后有什么细节? 排队的螺旋锁是旋转锁的链接列表,每个进入关键部分的线程都有一个锁。文章指出,当多个处理器只使用一个自旋锁时,这有助于缓存和缓存内存问题。 –

回答

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

是啊,我有点晚了党,但我这个职位就在几天前,它激励我写出更好的代码解释。