Say I have a reader and a producer that share a buffer. I have a wait queue for the reader to put itself in if there is no contents in the buffer when it reads. I also have an atomic variable for keeping track of the number of readers.
The producer wakes readers when contents are added to the buffer.
Is it cleaner to decrement the reader counter in the producer, after it does the waking, or the reader, after it wakes?
Example code
void producer(){
add_to_buffer();
if(atomic_read(num_waiting_readers) > 0){
wake_event(consumer_queue);
// Cleaner here?
atomic_decrement(num_waiting_readers);
}
}
void consumer(){
if(buffer_isempty()){
atomic_increment(num_waiting_readers);
wait_event(consumer_queue, 0);
// Or here, after wake?
// atomic_decrement(num_waiting_readers);
}
read_buffer();
}
Edit: In my specific case, there is only one reader, but the producer might be called multiple times. The libraries are Linux kernel 4.9 (c language), although in my actual code I use the interruptible
versions of wait_event
and wake_event