有没有改善这种自旋锁功能的性能有任何简单的方法? [英] Is there any simple way to improve performance of this spinlock function?

查看:140
本文介绍了有没有改善这种自旋锁功能的性能有任何简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的code自旋锁,但我根据维基百科的结果极其缓慢的性能实现了自旋锁。

I'm trying to implement a spinlock in my code but the spinlock that I implemented based on Wikipedia results in extremely slow performance.

int lockValue = 0;

void lock() {
    __asm__("loop: \n\t"
            "movl $1, %eax \n\t"
            "xchg %eax, lockValue \n\t"
            "test %eax, %eax \n\t"
            "jnz loop");
}

有没有改进这个问题的任何方式,使其更快?

Is there any way of improving this to make it faster?

感谢。

推荐答案

如何像这样(我的理解,这是KeAcquireSpinLock实现)。我在与安培; T汇编薄弱不幸

How about something like this (I understand this is the KeAcquireSpinLock implementation). My at&t assembly is weak unfortunately.

spin_lock:
    rep; nop
    test lockValue, 1
    jnz spin_lock
    lock bts lockValue
    jc spin_lock

这篇关于有没有改善这种自旋锁功能的性能有任何简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆