spinlock | Different implementations of spinlock | Machine Learning library
kandi X-RAY | spinlock Summary
kandi X-RAY | spinlock Summary
Various spinlock implementations from this article Spinlocks and Read-Write Locks by Lockless Inc. I made some modification to make each implementation self contained and provide a benchmark script. The code relies on GCC's built-in functions for atomic memory access.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of spinlock
spinlock Key Features
spinlock Examples and Code Snippets
Community Discussions
Trending Discussions on spinlock
QUESTION
As we know, xv6 doesn't let a spinlock be acquired twice (even by a process itself).
I am trying to add this feature which lets a process to acquire a lock more than once.
In order to reach this, I am adding an attribute called lock_holder_pid
to the struct spinlock
which is supposed to hold the pid of the process which has acquired this lock.
The only file I have changed is spinlock.c
Here is my new acquire()
function:
ANSWER
Answered 2021-Jun-10 at 09:15This is simply because you are trying to have access to a field of a null struct (myproc()->pid
).
As you may know, myproc()
returns a process running on the current processor. If you look at main.c
, you may notice that the bootstrap processor starts running there. Therefore, if we can find a function which calls the acquire()
function before setting up the first process, the problem will be solved.
If you take a close look at the kinit1
function, you can realize that the acquire
function is called in it. Consequently, we found a function that uses the acquire
function, even before initializing the ptable
struct. Therefore, when you try to access the myproc()
value, it is not initialized yet.
QUESTION
I'm writing a UEFI OS loader, and I use the system table provided by efi_main
in the panic handler to print a string on the console. Currently, I'm using a global static variable and a helper function to access it like this:
ANSWER
Answered 2021-May-29 at 10:53I dont think so. If its possible, any parameter would be a global as well. Making it more complex.
Global variables are ok for this. Create your own global panic object and give it to a new panic handler from the real one.
QUESTION
I created custom SpinLock class
I want to use this class in condition variable, but have an error
...ANSWER
Answered 2021-Jun-06 at 17:46std::condition_variable
only supports std::unique_lock
. Use std::condition_variable_any
instead.
The
condition_variable_any
class is a generalization ofstd::condition_variable
. Whereasstd::condition_variable
works only onstd::unique_lock
,condition_variable_any
can operate on any lock that meets theBasicLockable
requirements.
QUESTION
I am analyzing a performance issue on a Java servlet running on AdoptOpenJDK 11. The response time of the servlet slows down as the number of concurrent requests increases.
To isolate the cause of the issue, I had collected performance information with two tools while changing the number of concurrent requests:
- Windows Performance Monitor (to inspect CPU usage of the server)
- JDK Flight Recorder and JDK Mission Control (to inspect synchronization issue in JavaVM)
According to Mission Control, the servlet has a synchronization issue. It causes so many synchronization events when two or more requests come concurrently. The synchronization events are fired in a class used to lookup a table. The lookup table is implemented with ConcurrentHashMap
. So the synchronization is not intentional and looks unnecessary.
On the other hand, according to the Performance Monitor, the number of concurrent requests and the CPU usage % are almost linearly increase together. This is an unexpected result for me.
I had expected that the CPU usage will become constant because the requests will be processed one by one due to the synchronization. As a result of more research, I have found the Java VM had introduced the Adaptive Spinning on Java 6, and I had set a hypothesis that this (spinlock for synchronization in Java VM) is the reason why the CPU usage increased linearly.
QuestionThe class that causes the synchronization issue is used in so many places in our application. To reasoning and explanation for the change to the class (remove the synchronized
block from the class), I have to confirm the hypothesis with the result of a performance test. To confirm the hypothesis, I want to disable the spinlock for synchronization in Java VM.
I have found the JRockit and OpenJ9 had the command-line option to change the behavior of the adaptive spinning. But I could not find the equivalent for OpenJDK. There were no options related to spinlock in the result of java -XX:+PrintFlagsFinal -version
.
Is there any way to disable the spinlock for synchronization in OpenJDK Java VM?
...ANSWER
Answered 2021-May-05 at 19:40The question is actually an XY problem. I'll answer it with the caveat that the experiment might be useless or even misleading.
The hypothesis that disabled spinning will decrease CPU usage is not necessary correct. Without spinning, a contended lock ends up in calling into OS kernel to park/unpark threads. A system call with a context switch is expensive, so, depending on the number of threads and the length of the critical section, this may actually increase CPU usage.
The options to disable spinning on intrinsic locks in OpenJDK 11 are:
QUESTION
I have been reading the makefile of xv6 project. I want to put all the user programs into a folder to keep the project tidy. I can not find where it compiles the user programs. I know that when i run: make fs.img; it will compile the user programs, but i can not find any commands to do so. I also want all the kernel code to go into a directory called "kernel".
Is there a feature in make that allows automatic compilation or is there just something I'm not seeing. And could anyone suggest any make docs to help me understand this makefile.
My Makefile:
...ANSWER
Answered 2021-Mar-25 at 12:30All make systems (as required by POSIX) have a number of built-in rules including rules that know how to compile object files from C files.
For information on GNU make's built-in rules you can review the manual.
QUESTION
I suppose that std::atomic
sometimes can replace usages of std::mutex
. But is it always safe to use atomic instead of mutex? Example code:
ANSWER
Answered 2021-Feb-14 at 13:47I'll ignore your code for the sake of the answer, the answer generally is yes.
A lock does the following things :
- allows only one thread to acquire it at any given time
- when the lock is acquired, a read barrier is placed
- right before the lock is released, a write barrier is placed
The combination of the 3 points above makes the critical section thread safe. only one thread can touch the shared memory, all changes are observed by the locking thread because of the read barrier, and all the changes are to be visible to other locking threads, because of the write barrier.
Can you use atomics to achieve it? Yes, And real life locks (provided for example, by Win32/Posix) ARE implemented by either using atomics and lock free programming, either by using locks that use atomics and lock free programing.
Now, realistically speaking, should you use a self-written lock instead of the standard locks? Absolutely not.
Many concurrency tutorials preserve the notion that spin-locks are "more efficient" than regular locks. I can't stress enough how foolish it is. A user-mode spinlock IS NEVER more efficient than a lock that the OS provides. The reason is simple, that OS locks are wired to the OS scheduler. So if a lock tries to lock a lock and fails - the OS knows to freeze this thread and not reschedule it to run until the lock has been released.
With user-mode spinlocks, this doesn't happen. The OS can't know that the relevant thread tries to acquire to the lock in a tight loop. Yielding is just a patch and not a solution - we want to spin for a short time, then go to sleep until the lock is released. With user mode spin locks, we might waste the entire thread quantum trying to lock the spinlock and yielding.
I will say, for the sake of honesty, that recent C++ standards do give us the ability to sleep on an atomic waiting for it to change its value. So we can, in a very lame way, implement our own "real" locks that try to spin for a while and then sleep until the lock is released. However, implementing a correct and efficient lock when you're not a concurrency expert is pretty much impossible.
My own philosophical opinion that in 2021, developers should rarely deal with those very low-level concurrency topics. Leave those things to the kernel guys. Use some high level concurrency library and focus on the product you want to develop rather than micro-optimizing your code. This is concurrency, where correctness >>> efficiency.
QUESTION
If I create a kernel thread with kthread_run
then kthread_stop
it immediately, the kernel thread might be stopped without running. I checked the source code of kthread_run
and kthread_stop
in Linux-5.4.73
ANSWER
Answered 2021-Feb-01 at 16:13(This answer corresponds to Linux kernel version 5.4.)
The newly created kernel thread task executes the function kthread
in "kernel/kthread.c". If all is well kthread
calls the thread function referred to by the kthread_run
's (or kthread_create
's) threadfn
parameter. However, the final test before calling the threadfn
function pointer is to check the kernel thread's KTHREAD_SHOULD_STOP
bit. If the KTHREAD_SHOULD_STOP
bit is set, the threadfn
function pointer will not be called and the new kernel thread task will call do_exit
with the exit code -EINTR
. The relevant piece of code at the end of function kthread
is as follows:
QUESTION
All over StackOverflow and the net I see folks to distinguish mutexes and spinlocks as like mutex is a mutual exclusion lock providing acquire()
and release()
functions and if the lock is taken, then acquire()
will allow a process to be preempted.
Nevertheless, A. Silberschatz in his Operating System Concepts says in the section 6.5:
... The simplest of these tools is the mutex lock. (In fact, the term mutex is short for mutual exclusion.) We use the mutex lock to protect critical sections and thus prevent race conditions. That is, a process must acquire the lock before entering a critical section; it releases the lock when it exits the critical section. The acquire() function acquires the lock, and the release() function releases the lock.
and then he describes a spinlock, though adding a bit later
The type of mutex lock we have been describing is also called a spinlock because the process “spins” while waiting for the lock to become available.
so as spinlock is just a type of mutex lock as opposed to sleepable locks allowing a process to be preempted. That is, spinlocks and sleepable locks are all mutexes: locks by means of acquire()
and release()
functions.
I see totally logical to define mutex locks in the way Silberschatz did (though a bit implicitly).
What approach would you agree with?
...ANSWER
Answered 2020-Nov-18 at 19:27The type of mutex lock we have been describing is also called a spinlock because the process “spins” while waiting for the lock to become available.
Maybe you're misreading the book (that is, "The type of mutex lock we have been describing" might not refer to the exact passage you think it does), or the book is outdated. Modern terminology is quite clear in what a mutex is, but spinlocks get a bit muddy.
A mutex is a concurrency primitive that allows one agent at a time access to its resource, while the others have to wait in the meantime until it the exclusive access is released. How they wait is not specified and irrelevant, their process might go to sleep, get written to disk, spin in a loop, or perhaps you are using cooperative concurrency (also known as "asynchronous programming") and passing control to the event loop as your 'waiting operation'.
A spinlock does not have a clear definition. It might be used to refer to:
- A synonym for mutex (this is in my opinion wrong, but it happens).
- A specific mutex implementation that always waits in a busy loop.
- Any sort of busy-waiting loop waiting for a resource. A semaphore, for example, might also get implemented using a 'spinlock'.
I would consider any use of the word to refer to a (part of a) specific implementation of a concurrency primitive that waits in a busy loop to be correct, if a more general term is not appropriate. That is, use mutex (or whatever primitive you desire) unless you specifically want to talk about a busy-waiting concurrency primitive.
QUESTION
I have a Windows 10 Pro virtual machine using KVM. It is set up through virt-manager using the latest version of libvirt and QEMU. All of this is running on Manjaro Linux 20. Manjaro defaults to using PulseAudio for its audio output (at least for Manjaro 20). I was wondering if there is a way to use PulseAudio with libvirt/QEMU/KVM to have an audio pass-through from the guest OS to the host. Meaning that anything that is playing in the Guest OS will need to be heard through the default output device in Manjaro Linux. First, I tried setting environment variables to set it up, but that didn't work, and it also is deprecated past QEMU 4.1 (I am using 5.1.) Then, I tried using this tutorial, which ended up somehow bricking Manjaro, and I had to reinstall. I am currently using the default Spice server through virt-manager. I have heard that the Spice server introduces lots of overhead, and I can't afford that. I need every CPU cycle, and if PulseAudio is already running, I may as well use it. My libvirt XML config WITHOUT the Spice server is as follows:
...ANSWER
Answered 2020-Oct-28 at 19:08This is more of a comment, really...
I don't use libvirt
myself, but as far as I can tell there aren't any audio devices in your config.
Here are the qemu
command line options I use for guest (Win10 and others) audio output to PulseAudio output on -machine q35
:
QUESTION
Given the following code, I would expect the actions to be executed sequentially
...ANSWER
Answered 2020-Oct-13 at 10:16.ContinueWith(_ => DoActionAsync(i))
Returns a Task
(or specifically a Task
) that completes when DoActionAsync
returns. DoActionAsync will return at the first await, thus completing the outer Task
and allowing the next operation to begin.
So it will run each "create" serially, but "Finished" will run asynchronously.
A simple solution would be to schedule all the tasks using a LimitedconcurrencyTaskScheduler instead. Or just create a queue of the operations and have a thread processing the queue in order.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install spinlock
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page