does the atomic instruction involve the kernel

三世轮回 提交于 2021-02-11 07:04:45

问题


I'm reading this link to learn about futex of Linux. Here is something that I don't understand.

In order to acquire the lock, an atomic test-and-set instruction (such as cmpxchg()) can be used to test for 0 and set to 1. In this case, the locking thread acquires the lock without involving the kernel (and the kernel has no knowledge that this futex exists). When the next thread attempts to acquire the lock, the test for zero will fail and the kernel needs to be involved.

I don't quite understand why "acquires the lock without involving the kernel".

I'm always thinking that the atomic instruction, such as test-and-set, always involves the kernel.

So why does the first time of acquiring the lock won't involve the kernel? More specifically, the atomic instruction must or may involve the kernel?


回答1:


An atomic test and set instruction is just an ordinary instruction executed by user code as normal. It doesn't involve the kernel.

Futexes provide an efficient way to perform a lock and unlock operation without involving the kernel in the fast paths. However, if a process needs to be put to sleep (to wait to acquire the lock) or woken (because it couldn't acquire the lock but now can), then the kernel has to be involved to perform the scheduling operations.



来源:https://stackoverflow.com/questions/60161829/does-the-atomic-instruction-involve-the-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!