Completion locks

Completion locks are an efficient way to achieve code synchronization if you need one or multiple threads of execution to wait for completion of some event, such as waiting for another process to reach a point or state. Completion locks may be preferred over a semaphore for a couple of reasons: multiple threads of execution can wait for a completion, and using complete_all(), they can all be released at once. This is way better than a semaphore waking up to multiple threads. Secondly, semaphores can lead to race conditions if a waiting thread deallocates the synchronization object; this problem doesn’t exist when using completion.

Completion can be used by including <linux/completion.h> and by creating a variable of type struct completion, which is an opaque structure for maintaining the state of completion. It uses a FIFO to queue the threads waiting for the completion event:

struct completion {
        unsigned int done;
        wait_queue_head_t wait;
};

Completion basically consists of initializing the completion structure, waiting through any of the variants of wait_for_completion() call, and finally signalling the completion through complete() or the complete_all() call. There are also functions to check the state of completions during its lifetime.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.211.87