Re: missing GMainContext methods ... (fwd)



On Thu, 2005-12-22 at 18:20 +0100, Tim Janik wrote:

> >
> > On Thu, 2005-11-17 at 16:57 +0000, michael meeks wrote:
> >> 	So - I've been trying to use the GMainContext to fix a rather tricky
> >> issue in using unsafe single-threaded code accessed via ORBit2 from
> >> multiple OO.o threads in a safe & reliable way. This is somewhat
> >> involved, for various reasons, but made particularly unpleasant due to 2
> >> missing methods:
> >> 
> >> 	a) gboolean g_main_context_is_owner (GMainContext *context);
> >> 		+ this would tell you if the current thread owns the
> >> 		  g_main_context. NB. this is subtly different from
> >> 		  something like:
> >> 			if (g_main_context_acquire ()) {
> >> 				g_main_context_release();
> >> 				... I own it - horay ...
> >> 			}
> >> 		+ since that actually transiently takes ownership of
> >> 		  the thread but doesn't tell you if you (now) contine
> >> 		  to own it [ due to some (much) higher stack frame
> >> 		  having taken that lock ].

+ * If the current thread is the owner of @context returns
+ * TRUE else FALSE. This is semantically rather different
+ * from acquiring ownership.

Umm, "no duh". :-) It would be much more useful if the docs gave
some idea of why you would want this. Obviously giving a full idea
of that would probably involve describing some horribly twisted
and convoluted thing inside OpenOffice, but at least a few sentences
that give an idea of the "problem domain" - if someone is trying
to find out how to fix some problem, is function potentially useful?

> i think this should go in, with one modification:
> 
> +gboolean
> +g_main_context_is_owner (GMainContext *context)
> +{
> +  gboolean is_owner;
> +
> +  if (!context)
> +    context = g_main_context_default ();

[...]

> if (!context), the function should simply return FALSE, instead of
> forcefully create a context. following the basic principle of eliminating
> side-effects on getters/queries.

NULL should consistently mean "the default context" - you could add
a private peek_default() or something.

> >> 	b) void g_main_context_acquire_with_wakeup (GMainContext *context);
> >> 		+ this cunning method - would be the analogue of
> >> 		  g_main_context_wait () - except instead of sitting
> >> 		  around hoping that the other thread doing the poll
> >> 		  will wake-up, it does a (safe) g_main_context_wakeup
> >> 		  with the relevant locks held;
> >> 		+ currently it appears impossible/acutely-ugly to
> >> 		  get ownership of the GMainContext from another thread
> >> 		  if the main thread is in it's poll.

*Normally* waking up a GMainContext from a poll does not get it to
release ownership - where normally is g_main_loop_run(). So, if this
gets added, there needs to be a huge caveat in the documentation.

I must say, that I really don't quite understand the goal here - my
guess would have to be that your main loop function is something like::

 had_context = get_have_context();
 if (had_context)
    release_context();
 
 while (true) {
    acquire_context();
    wait_for_input_and_dispatch_gtk_events();
    release_context();
    handle_openoffice_stuff();
 }

 if (had_context)
    acquire_context();

So you can just run it from multiple threads, and if the openoffice
stuff blocks the other thread can pick up the slack. This worries
me in various ways:

 - If g_main_context_dispatch() blocks, then it doesn't work because
   g_main_context_dispatch() has to be called with the context
   owned. So, if some openoffice code is called from any sort 
   of GTK+/ORBit/etc. callback you could have problems.

 - I have some doubts that releasing ownership inside  
   g_main_context_dispatch() will work properly. The code certainly
   wasn't designed for that and the issues could be very subtle.

I'm not completely opposed to adding these functions, if documented
with the appropriate comments and caveats. But they seem highly
specialized to doing particular sorts of scary things.

Regards,
						Owen

Attachment: signature.asc
Description: This is a digitally signed message part



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