I'm wondering about the thread safety of the linux kernel linked list. Suppose one thread adds items to a list while another thread reads the list. Of course while modifying the list I need to take a lock/mutex. But does checking if the list is empty using list_empty already require a lock?
Looking at the source of list_empty:
static inline int list_empty(const struct list_head *head)
{
return READ_ONCE(head->next) == head;
}
we see that it uses READ_ONCE. AFAIK this prevents certain compiler optimizations and is also atomic for properly aligned/sized variables. Because there is no memory barrier I get no ordering with other memory accesses whatsoever, but the list will eventually become not empty after some other thread added an item. So I believe a lock is not strictly necessary for list_empty.
I'm asking because I want to use list_empty as a condition for wait_event_interruptible.