[sheepdog] quesiton about function worker_routine() in lib/work.c

Liu Yuan namei.unix at gmail.com
Thu Sep 4 04:07:21 CEST 2014


On Wed, Sep 03, 2014 at 05:37:19PM +0800, Bingpeng Zhu wrote:
> Hi, all
>    I am reading sheepdog's source code. I have a question of the code in function worker_routine() in  lib/work.c. 
> It calls sd_mutex_lock(&wi->startup_lock)  in the beginning , and calls  sd_mutex_unlock(&wi->startup_lock) ‍
> immediately after the lock. It seems that the code want to block the new threads until all the new threads are created.
> I don't understand the purpose for doing this.  Can anybody answer my question?
> 

We have tow concepts in thread pool mechanism, workers and main thread, so
normally the task would be split into two parts, one can be executed in paralle
in different thread context, and the other can be executed in the main thread
sequentially.

worker pool

thread 1    ---- run someone's job1 in worker thread
thread 2    ++++++++++++++++++++ run some other's job2
thread 3    **** idle **************
            ....
         job 1 worker finishes and queue to run something in main thread 
	          |
	          V
main thread =====---============++++=========
                                |
				v
		             job2's main part

In this way, if job1 and job2 has something that need to run sequentially, then
we can put those sequential action in main thread.

work.fn will run in worker pool in parallel with other jobs
work.done will run in main thread sequentially

Thanks
Yuan



More information about the sheepdog mailing list