Re: Changes to the GLib main loop [ GMainLoop ]



> > The context->owner mechanism presents some problems with GMainLoop.
> > Consider what happens when:
> > 
> >  - Thread A calls g_main_context_iteration()
> >  - Before that returns, thread B calls g_main_run() on the same thread
> >  - Thread A returns and starts a long computation
> 
> No problem, Thread A calls g_main_context_iteration and set owner to A. thread
> B finds A owning the contexts waits, A is ready and signals B to be ready, B
> finds loop->context->owner == NULL and then simply takes the control of the
> main loop. Neat, eh?

OK, looks like I misread that portion of the patch - it's pretty
chopped up. I agree that this should work properly. (Except for
excessive wakeups and contention.)

[...]
  
> > Also, using g_main_loop_run() to wait for, say, a modal dialog,
> > in a second thread has become _much_ less efficient since you
> > are waking up the second thread via g_cond_broadcast() on
> > _every_ iteration. In face, I'd consider this to be an
> > unacceptable performance hit. The reason why each GMainLoop
> > currently has a separate condition variable is to avoid this.
> 
> Ok, but that could be optimized away by also telling g_main_context_iterate,
> whether it was called from g_main_loop_run or not only signalling other
> threads, when it was not run from g_main_loop_run. For calls not done from
> g_main_loop_run it would still need to be called, because of the scenario
> outlined above (the thread A and B thingy)

This doesn't work because the thread needs to be woken up to check for
loop->is_running, even before the main loop stops running its loop.

(Remember, what I want to be able to do here is use main loop to wait
for a modal dialog being closed, whether or not the dialog is being
shown from the main thread.)

Basically, we need to wait on the condition:

 loop->is_running || loop->context->owner == NULL

Trying to do this without having a conditional variable for _this_
condition is going to be really inefficient. I think we indeed
need to have a separate (demand created) condition variable for each 
GMainLoop.

Regards,
                                        Owen




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]