3 ms·
If they got an improvement of 116% perhaps a spinlock isn't the right solution? It sounds more like the subsystem protected should be autonomous and respond to
by Flow 13y ago
If they got an improvement of 116% perhaps a spinlock isn't the right solution? It sounds more like the subsystem protected should be autonomous and respond to messages in a message queue instead.
- jamesaguilar 13y agoWhether a system should be guarded by a spinlock or a more heavyweight construct is orthogonal to making the spinlock faster.
- exDM69 13y ago> If they got an improvement of 116% perhaps a spinlock isn't the right solution? It sounds more like the subsystem protected should be autonomous and respond to messages in a message queue instead. You need some kind of a locking mechanism to be able to build a message queue. And you need some kind of spinlock to be able to implement a proper locking mechanism. Lock-free queue mechanisms can be constructed using atomic operations but they exhibit the same problems as spinlocks (moving cachelines from cpu to cpu) and there needs to be special handling for cases when the queue is empty or full. In the case of kernel programming, there is a lot of code that must be able to run in an interrupt handler context and other contexts where you can't rely on a scheduler for waiting or use any high level constructs. A spinlock is one of the most fundamental building blocks for synchronization in a multi-core system. It is difficult if not impossible to build a kernel without some kind of spinlocks. Message queues are a good high level construct and the kernel is full of different kinds of message queues, but they can't function without spinlocks. Or do you have a suggestion how would you get a packet from the Ethernet adapter interrupt to the IP stack using a message queue without locking? (edit: this might be a bad example)
- kev009 13y agoFor your last para, I think it is actually pretty common to use a lock free ring buffer. An atomic instruction takes the place of locking for swapping out data pointers. In fact I think modern adapters write directly into a buffer (DMA) and atomically update the data pointer in the ring. Not an expert on these topics, feel free to correct. But lockless programming is pretty much critical section programming, just offloading the critical part to the consistency model.
- exDM69 13y ago> For your last para, I think it is actually pretty common to use a lock free ring buffer. An atomic instruction takes the place of locking for swapping out data pointers. I think you're right, that particular use case is perhaps not the best example. Ethernet -> IP is a single producer situation where the Ethernet driver is free to drop a packet if the input buffer is full. So that can be implemented without locking.