排队旋转锁
Queued Spinlock
我偶然发现了排队的旋转锁,并希望在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:
}
}
这种实现是否正确?我怀疑是这样!
这里相关代码的作者。首先让我声明代码是正确的。我只是在这里写了更详细的代码解释: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
是的,我参加聚会有点晚了,但我几天前才发表这篇文章,它激发了我写更好的代码解释。
相关文章:
- 与互斥锁相比,旋转锁可以保证上下文切换
- 绘制旋转的三角形
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 发布旋转矩阵(openGL/glm)
- 顺时针迭代旋转 3 位数字
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- 子轴围绕父轴而不是他自己的轴旋转
- 将使用太多的纹理插值器 - 带旋转的着色器
- SFML 向下移动时如何围绕屏幕中心旋转?
- 如何在OpenGL中正确旋转和缩放对象?
- 使用 C++在 OpenGL 中移动自动旋转的 3D 多边形
- 按时间自动旋转形状,同时在 OpenGL 中使用C++移动它们
- 排队功能
- 如何使用增强::几何计算多边形的旋转固体体积?
- 使用 GDI+ 旋转位图,然后转换为 HDC
- 排队旋转锁