Why recursive locks are needed in kernel




















Because the recursive mutex has a sense of ownership, the thread that grabs the mutex must be the same thread that releases the mutex.

In the case of non-recursive mutexes, there is no sense of ownership and any thread can usually release the mutex no matter which thread originally took the mutex. In many cases, this type of "mutex" is really more of a semaphore action, where you are not necessarily using the mutex as an exclusion device but use it as synchronization or signaling device between two or more threads. Another property that comes with a sense of ownership in a mutex is the ability to support priority inheritance.

Because the kernel can track the thread owning the mutex and also the identity of all the blocker s , in a priority threaded system it becomes possible to escalate the priority of the thread that currently owns the mutex to the priority of the highest priority thread that is currently blocking on the mutex.

The first person to catch the door-handle of the booth, is the one who is allowed to use the phone. He has to keep holding on to the handle of the door as long as he uses the phone, otherwise someone else will catch hold of the handle, throw him out and talk to his wife : There's no queue system as such.

When the person finishes his call, comes out of the booth and leaves the door handle, the next person to get hold of the door handle will be allowed to use the phone. Any thread which has to execute some lines of code which should not be modified by other threads at the same time using the phone to talk to his wife , has to first acquire a lock on a mutex clutching the door handle of the booth. Only then will a thread be able to run those lines of code making the phone call.

Once the thread has executed that code, it should release the lock on the mutex so that another thread can acquire a lock on the mutex other people being able to access the phone booth. There are concepts of recursive mutexes etc, but this example was only meant to show you the basic concept.

Hope the example gives you a clear picture of the concept. Instead of explicitly using lock and unlock , you can use brackets as shown here, if you are using a scoped lock for the advantage it provides. Scoped locks have a slight performance overhead though. A lock allows only one thread to enter the part that's locked and the lock is not shared with any other processes.

A semaphore does the same as a mutex but allows x number of threads to enter, this can be used for example to limit the number of cpu, io or ram intensive tasks running at the same time. This scenario can happen if child completes all of it's processing before foo manages to acquire the mutex.

Since foo never checks done while holding the mutex, it won't notice that child has finished its work. Most of the time, if you think you need a recursive mutex then your design is wrong, so it definitely should not be the default. For a class with a single mutex protecting the data members, then the mutex should be locked in all the public member functions, and all the private member functions should assume the mutex is already locked.

This paper is an effort to analyze the 6 mutex types and 3 scoped lock types N proposes to suggest a simplification. ArangoDB currently uses mutexes, spinlocks and RW-locks for that. There is another type of mutex, called 'recursive mutex', which allows the thread that locked it, to lock it several more times, without getting blocked but other threads that try to lock the mutex now will get blocked.

Mutex is kind of a key to use any resources in the system. The thread that has locked a mutex becomes its current owner and remains the owner until the same thread has unlocked it. Note the two steps shall complete in one atomic operation. A mutex short for MUTual EXclusion is a flag or lock used to allow only one thread to access a section of code at a time. If the mutex was in a locked state, the call will cause the invoking thread to "block" stop execution until the mutex becomes unlocked.

When timeout is given, it specifies a point in time where the waiting should be aborted. It takes 3 arguments, a starting long long number, the number of primes to find, and the number of threads to use. When the lock is set, no other thread can access the locked region of code. Creation - initially, a mutex is always unlocked. Only the thread that currently holding the lock can unlock a Locking and unlocking a mutex by hand is dangerous business though: forget to unlock it and the program is compromised.

Mutex is like a C lock, but Mutex can work across multiple processes. If the mutex was already locked, the calling thread gets blocked until the mutex becomes available.

Once a mutex is poisoned, all other threads are unable to access the data by default as it is likely tainted some invariant is not being upheld. Either they succeed or they don't. A mutex that supports timeout. Mutex also referred as Mutual Exclusion, which states that the lockable object can only be owned by one thread at a time and only the owner thread can release the lock on a mutex. Unlocking: release mutex.

Both t1 and t2 need to call mutex lock. If Recursive mutex allows the same thread to recursively lock a resource - up to an unspecified limit. Essentially, futex provides a way to ask the kernel to block a thread until Treat Mutex as a lock, there are two functions. What I want is that if some process is putting something on shared memory segment, other process should not be able to access that segment.

Mutex variables are one of the primary means of implementing thread synchronization and for protecting shared data when multiple writes occur. The MCS lock proposed by Mellor-Crummey and Scott is a simple spinlock with the desirable properties of being fair and with each cpu trying to acquire the lock spinning on a local variable.

Sleep time. So before reading or writing the file, i want to mutex shared lock. Creating a mutex: import locks var mutex: Lock initLock mutex.

So for example say the arguments are 9 2 1, then the output will be. IMHO, most arguments against recursive locks which are what I use To name one, the "callback" problem, which is elaborated on exhaustively and without any multithreading related point of view, for example in the book Component software - beyond Object oriented programming. As soon as you have some inversion of control e.

Independent of whether there are mutexes and threading involved or not. Now, with code like the above you get all error cases, which would usually be named in the context of recursive locks - only without any of them. An event handler can unregister itself once it has been called, which would lead to a bug in a naively written fireChangedEvent.

Or it could call other member functions of EvilFoo which cause all sorts of problems. The root cause is re-entrance.

Worst of all, this could not even be very obvious as it could be over a whole chain of events firing events and eventually we are back at our EvilFoo non- local. So, re-entrance is the root problem, not the recursive lock. Now, if you felt more on the safe side using a non-recursive lock, how would such a bug manifest itself?

In a deadlock whenever unexpected re-entrance occurs. And with a recursive lock? The same way, it would manifest itself in code without any locks. So the evil part of EvilFoo are the events and how they are implemented, not so much a recursive lock. Another aspect often coming into the discussion is the definition of what a lock is supposed to do in the first place:.

The way I do my concurrent programming, I have a mental model of the latter protect a resource. This is the main reason why I am good with recursive locks. If some member function needs locking of a resource, it locks. If it calls another member function while doing what it does and that function also needs locking - it locks. And I don't need an "alternate approach", because the ref-counting of the recursive lock is quite the same as if each function wrote something like:.

And once events or similar constructs visitors?! Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 13 years, 1 month ago. Active 1 year, 1 month ago. Viewed k times. Improve this question. Mecki Mecki k 31 31 gold badges silver badges bronze badges.

Add a comment. Active Oldest Votes. However , there are other considerations at play here too. If you refer to classic VxWorks RTOS kernel, they define three mechanisms: mutex - supports recursion, and optionally priority inheritance. This mechanism is commonly used to protect critical sections of data in a coherent manner. This mechanism can be used to protect critical sections, but is also particularly useful for coherent signalling or synchronization between threads.

Improve this answer. Tall Jeff Tall Jeff 9, 7 7 gold badges 42 42 silver badges 61 61 bronze badges. A mutex whether recursive or non-recursive has a notion of ownership. JayD It's very confusing when people argue about things like these.. Pacerier The relevant standard. This answer is e. Other systems and standards might behave very different.



0コメント

  • 1000 / 1000