简体   繁体   English

Spin_lock 和互斥锁顺序

[英]Spin_lock and mutex lock order

I got test question (interview).我得到了测试问题(面试)。

You need to grab both spin_lock and mutex in order to do something.您需要同时获取 spin_lock 和 mutex 才能执行某些操作。 What is the correct order of acquiring?正确的获取顺序是什么? Why?为什么?

I have some thoughts about this but no strong opinion about answer.我对此有一些想法,但对答案没有强烈的看法。

The reasone why you should grub lock is protect "critical region" on SMP or "critical region" on single CPU from preemption relative corruption (race).您应该 grub 锁定的原因是保护 SMP 上的“关键区域”或单个 CPU 上的“关键区域”免受抢占相对损坏(竞争)。 It is very important you machine type SMP or single CPU.您的机器类型 SMP 或单 CPU 非常重要。 It is also important what code inside spin and mutex. Spin 和 mutex 内部的代码也很重要。 Is there kmalloc, vmalloc, mem_cache_alloc, alloc_bootmem or function with __user memory access oe even usleep.是否有 kmalloc、vmalloc、mem_cache_alloc、alloc_bootmem 或具有 __user 内存访问 oe 甚至 usleep 的函数。

spin_lock - it is the simplest lock in /include/asm/spinlock.h. spin_lock - 它是 /include/asm/spinlock.h 中最简单的锁。 Only one thread can be locked inside spin_lock in the same time. spin_lock 中只能同时锁定一个线程。 Any other thread that will try get spin_lock will be spin on the same place (instruction) until previous thread will free spin_lock.任何其他尝试获取 spin_lock 的线程都将在同一位置(指令)自旋,直到前一个线程释放 spin_lock。 Spined thread will not go to sleep. Spined 线程不会进入休眠状态。 So in the same time with spin_lock you can have two or more threads that will do something (one work and one spin).因此,在使用 spin_lock 的同时,您可以有两个或多个线程来做某事(一个工作和一个自旋)。 It is impossible on single CPU machine.在单 CPU 机器上是不可能的。 But very good work on SMP.但是在 SMP 上的工作非常好。 Code section inside spin_lock should be small and fast. spin_lock 内的代码部分应该小而快。 If you code should work on differnt machines try check CONFIG_SMP and CONFIG_PREEMPT.如果您的代码应该在不同的机器上工作,请尝试检查 CONFIG_SMP 和 CONFIG_PREEMPT。

mutex - on other hand work lick semaphore /inside/asm/semaphore.h but counter is one.互斥锁 - 另一方面,工作 lick semaphore /inside/asm/semaphore.h 但计数器是一个。 If counter is one so only one thread can go inside mutex lock region.如果计数器为 1,则只有一个线程可以进入互斥锁区域。 Any other thread that will try get lock will see counter is zero becouse one thrad inside.任何其他尝试获取锁的线程都会看到计数器为零,因为里面有一个 thrad。 And thread will go to wait queue.线程将进入等待队列。 It will be woken up when the mutex be released and counter equal one.当互斥量被释放且计数器等于 1 时,它将被唤醒。 Thread inside mutex lock can sleep.互斥锁内的线程可以休眠。 It can call memory allocation function and get userspace memory.它可以调用内存分配函数并获取用户空间内存。

(SMP)So imagine that you got spinlock and next mutex lock. (SMP) 所以想象一下你得到了自旋锁和下一个互斥锁。 So only one thread can get first spin and next mutex.所以只有一个线程可以获得第一次旋转和下一个互斥锁。 But potentially inside mutex lock code cant sleep and it is bad.但潜在的内部互斥锁代码无法入睡,这很糟糕。 Because mutex inside spin.因为互斥锁里面自旋。

(SMP)If you will get mutex lock and next spin lock. (SMP)如果您将获得互斥锁和下一个自旋锁。 The same situation only one thread can go inside lock region.同样的情况只有一个线程可以进入锁区。 But between mutex get lock and spinlock code can sleep and also between spin_unlock and mutex free lock it can sleep too.但是在 mutex get lock 和 spinlock 代码之间可以休眠,并且在 spin_unlock 和 mutex free lock 之间它也可以休眠。 Spin lock will get less unsleep region and it is good.自旋锁将获得更少的非睡眠区域,这很好。

TL;DR: Lock mutex first and then spin lock. TL;DR:先锁定互斥锁,然后自旋锁。


First you need to avoid such situations and be very careful to avoid deadlocks .首先,您需要避免这种情况并非常小心地避免死锁

Then, you should consider effects of locking.然后,您应该考虑锁定的影响。 Mutex may cause thread to block and sleep, while spin lock may cause thread to occupy processor in busy waiting loop.互斥可能导致线程阻塞和休眠,而自旋锁可能导致线程在忙等待循环中占用处理器。 So it is general recommendation to keep critical sections that own a spin lock short in time which leads to following rule of thumb: do not sleep (ie by locking mutex) while owning a spin lock or you will waste CPU time.因此,一般建议保持拥有自旋锁的临界区的时间较短,这导致以下经验法则:拥有自旋锁时不要休眠(即通过锁定互斥锁),否则会浪费 CPU 时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM