1 /*
2 * GStreamer
3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4 * Copyright (C) 2015 Thibault Saunier <tsaunier@gnome.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #include "gstgtkutils.h"
23
24 struct invoke_context
25 {
26 GThreadFunc func;
27 gpointer data;
28 GMutex lock;
29 GCond cond;
30 gboolean fired;
31
32 gpointer res;
33 };
34
35 static gboolean
gst_gtk_invoke_func(struct invoke_context * info)36 gst_gtk_invoke_func (struct invoke_context *info)
37 {
38 g_mutex_lock (&info->lock);
39 info->res = info->func (info->data);
40 info->fired = TRUE;
41 g_cond_signal (&info->cond);
42 g_mutex_unlock (&info->lock);
43
44 return G_SOURCE_REMOVE;
45 }
46
47 gpointer
gst_gtk_invoke_on_main(GThreadFunc func,gpointer data)48 gst_gtk_invoke_on_main (GThreadFunc func, gpointer data)
49 {
50 GMainContext *main_context = g_main_context_default ();
51 struct invoke_context info;
52
53 g_mutex_init (&info.lock);
54 g_cond_init (&info.cond);
55 info.fired = FALSE;
56 info.func = func;
57 info.data = data;
58
59 g_main_context_invoke (main_context, (GSourceFunc) gst_gtk_invoke_func,
60 &info);
61
62 g_mutex_lock (&info.lock);
63 while (!info.fired)
64 g_cond_wait (&info.cond, &info.lock);
65 g_mutex_unlock (&info.lock);
66
67 g_mutex_clear (&info.lock);
68 g_cond_clear (&info.cond);
69
70 return info.res;
71 }
72