On Thu, 22 Dec 2005, michael meeks wrote:

---------- Forwarded message ----------
Date: Tue, 20 Dec 2005 17:21:07 +0000
From: michael meeks <[EMAIL PROTECTED]>
To: Federico Mena Quintero <[EMAIL PROTECTED]>, Tim Janik <[EMAIL PROTECTED]>
Cc: Gtk Hackers <gtk-devel-list@gnome.org>
Subject: missing GMainContext methods ...

So,

        I sent this a while back - but didn't see it on the list - perhaps
broken subscription information (?).

        I append a patch implementing these two to some level.

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 ].

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 ();
+
+#ifdef G_THREADS_ENABLED
+  LOCK_CONTEXT (context);
+  is_owner = context->owner == G_THREAD_SELF;
+  UNLOCK_CONTEXT (context);
+#else
+  is_owner = TRUE;
+#endif
+
+  return is_owner;
+}

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.


        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.

this looks good to me as is:

+void
+g_main_context_acquire_with_wakeup (GMainContext *context)
+{
+  gboolean got_ownership;
+
+  if (!context)
+    context = g_main_context_default ();
+
+  got_ownership = g_main_context_acquire (context);
+  while (!got_ownership)
+    {
+      LOCK_CONTEXT (context);
+      g_main_context_wakeup_unlocked (context);
+      got_ownership = g_main_context_wait (context,
+                                          context->cond,
+                                          g_static_mutex_get_mutex 
(&context->mutex));
+      UNLOCK_CONTEXT (context);
+    }
+}


        The basic reasoning here is that - ~all existing event-driven gtk+
code, typically happens from the default GMainContext, and/or at least
that provides a reasonable back-compatible way to create a backwards
compatible 'apartment' (by holding the GMainContext lock) to execute old
code in in-line.

I attach a simple patch; the g_main_context_is_owner impl. is of course
trivial - it'd be great to be able to commit that by itself. The
'acquire_with_wakeup' is also relatively trivial, cf. above.

        There is no bugzilla number, is that a problem ?

i don't think so. unless owen vetos in, i'll apply them tomorrow with
the change outlined above.


        Thanks,

                Michael.

--
[EMAIL PROTECTED]  <><, Pseudo Engineer, itinerant idiot

---
ciaoTJ
_______________________________________________
gtk-devel-list mailing list
gtk-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-devel-list

Reply via email to