Diwakar Sharma
Diwakar Sharma

Reputation: 425

Kernel programming : wait queue uses spinlock internally

I am working on implementing blocking calls in char device drivers. I use wait_queue_head_t element with wait_queue_interruptible and wake_up_interruptible calls. What I understand and read about blocking behaviour is that when a call is blocked it sleeps( not busy wait) and doesn't consume resources. But when I checked struct wait_queue_head in wait.h , it uses a spin-lock. spin-locks have behaviour of not sleeping (busy waiting)..! So this confused me. Any clarifications to help on this please? Am I missing something?

Upvotes: 0

Views: 1331

Answers (2)

Roland
Roland

Reputation: 6583

To expand on the other answer: the spinlock inside the struct wait_queue_head is used to protect the internal list member in case multiple threads are accessing it simultaneously. However, the spinlock is never held when going to sleep -- it is only held during the small, non-preemptable, non-sleeping critical sections that manipulate the wait queue internals.

Upvotes: 1

toyoubala
toyoubala

Reputation: 71

The purpose of spinlock you find in

struct __wait_queue_head {
spinlock_t lock;
struct list_head task_list;

};

is to protect the member task_list from concurrent access. If you step in wait_queue_interruptible() you could find a call to schedule().

Upvotes: 2

Related Questions