Calling complete() on automatic variable of type struct completion defined in "waiting" thread

355 Views Asked by At

I try to understand use of completion in a piece of code.

Basically, one kernel thread creates automatic variable struct completion which is, I assume, allocated on the thread's stack. Then it pushes pointer of the completion struct to another thread (using fifo) and waits for completion.

struct completion done;

init_completion(&done);
push_to_fifo(&done);
wait_for_completion(&done);

The second thread fetches request from fifo, processes it and completes task.

Will the done variable be accessible from the second thread which calls complete(done)?

1

There are 1 best solutions below

1
On BEST ANSWER

The first thread is waiting for the second to finish, so the struct completion on its stack will be stable until after wait_for_completion returns.

The stack space where that structure resides is just regular memory, the same as heap-allocated memory. The only difference is that once this function returns, and its caller invokes a different function, the same memory gets re-used for the stack frame / local variables of that next function.

So, if the other thread were to access the structure after that point, that would be a problem, but the point is that is supposed to be finished by then and once it signals "done", it shouldn't touch that memory again.