1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
4 * Copyright (C) 2004 Johan Dahlin
6 * pygmainloop.c: GMainLoop wrapper
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
33 #include "pygmainloop.h"
34 #include "pygmaincontext.h"
36 #include "pyglib-private.h"
38 static int pipe_fds[2];
43 } PySignalWatchSource;
45 #ifdef DISABLE_THREADING
46 static GMainLoop *pyg_current_main_loop = NULL;;
48 static inline GMainLoop *
49 pyg_save_current_main_loop (GMainLoop *main_loop)
51 GMainLoop *retval = pyg_current_main_loop;
53 g_return_val_if_fail(main_loop != NULL, NULL);
55 pyg_current_main_loop = g_main_loop_ref(main_loop);
61 pyg_restore_current_main_loop (GMainLoop *main_loop)
63 if (pyg_current_main_loop != NULL)
64 g_main_loop_unref(pyg_current_main_loop);
65 pyg_current_main_loop = main_loop;
68 static inline GMainLoop *
69 pyg_get_current_main_loop (void)
71 return pyg_current_main_loop;
73 #else /* !defined(#ifndef DISABLE_THREADING) */
75 static int pyg_current_main_loop_key = -1;
77 static inline GMainLoop *
78 pyg_save_current_main_loop (GMainLoop *main_loop)
82 g_return_val_if_fail(main_loop != NULL, NULL);
84 if (pyg_current_main_loop_key == -1)
85 pyg_current_main_loop_key = PyThread_create_key();
87 retval = PyThread_get_key_value(pyg_current_main_loop_key);
88 PyThread_delete_key_value(pyg_current_main_loop_key);
89 PyThread_set_key_value(pyg_current_main_loop_key,
90 g_main_loop_ref(main_loop));
96 pyg_restore_current_main_loop (GMainLoop *main_loop)
100 g_return_if_fail (pyg_current_main_loop_key != -1);
102 prev = PyThread_get_key_value(pyg_current_main_loop_key);
104 g_main_loop_unref(prev);
105 PyThread_delete_key_value(pyg_current_main_loop_key);
106 if (main_loop != NULL)
107 PyThread_set_key_value(pyg_current_main_loop_key, main_loop);
110 static inline GMainLoop *
111 pyg_get_current_main_loop (void)
113 if (pyg_current_main_loop_key == -1)
115 return PyThread_get_key_value(pyg_current_main_loop_key);
117 #endif /* DISABLE_THREADING */
120 pyg_signal_watch_prepare(GSource *source,
123 /* Python only invokes signal handlers from the main thread,
124 * so if a thread other than the main thread receives the signal
125 * from the kernel, PyErr_CheckSignals() from that thread will
129 #ifdef HAVE_PYSIGNAL_SETWAKEUPFD
131 #else /* !HAVE_PYSIGNAL_SETWAKEUPFD */
132 /* On Windows g_poll() won't be interrupted by a signal
133 * (AFAIK), so we need the timeout there too, even if there's
136 #ifndef PLATFORM_WIN32
137 if (!pyglib_threads_enabled())
139 #endif /* PLATFORM_WIN32 */
141 /* If we're using 2.5 or an earlier version of python we
142 * will default to a timeout every second, be aware,
143 * this will cause unnecessary wakeups, see
144 * http://bugzilla.gnome.org/show_bug.cgi?id=481569
148 #endif /* HAVE_PYSIGNAL_SETWAKEUPFD */
152 pyg_signal_watch_check(GSource *source)
154 PyGILState_STATE state;
155 GMainLoop *main_loop;
157 #ifdef HAVE_PYSIGNAL_SETWAKEUPFD
158 PySignalWatchSource *real_source = (PySignalWatchSource *)source;
159 GPollFD *poll_fd = &real_source->fd;
161 if (poll_fd->revents & G_IO_IN)
162 (void) read(poll_fd->fd, &dummy, 1);
165 state = pyglib_gil_state_ensure();
167 main_loop = pyg_get_current_main_loop();
169 if (PyErr_CheckSignals() == -1 && main_loop != NULL) {
170 PyErr_SetNone(PyExc_KeyboardInterrupt);
171 g_main_loop_quit(main_loop);
174 pyglib_gil_state_release(state);
180 pyg_signal_watch_dispatch(GSource *source,
181 GSourceFunc callback,
184 /* We should never be dispatched */
185 g_assert_not_reached();
189 static GSourceFuncs pyg_signal_watch_funcs =
191 pyg_signal_watch_prepare,
192 pyg_signal_watch_check,
193 pyg_signal_watch_dispatch
197 pyg_signal_watch_new(void)
199 GSource *source = g_source_new(&pyg_signal_watch_funcs,
200 sizeof(PySignalWatchSource));
202 #ifdef HAVE_PYSIGNAL_SETWAKEUPFD
203 PySignalWatchSource *real_source = (PySignalWatchSource *)source;
206 /* Unfortunately we need to create a new pipe here instead of
207 * reusing the pipe inside the GMainContext.
208 * Ideally an api should be added to GMainContext which allows us
209 * to reuse that pipe which would suit us perfectly fine.
210 * XXX More efficient than a pipe, we could use an eventfd on Linux
211 * kernels that support it.
213 gint already_piped = (pipe_fds[0] > 0);
214 if (!already_piped) {
215 if (pipe(pipe_fds) < 0)
216 g_error("Cannot create main loop pipe: %s\n",
219 /* Make both ends of the fd non blocking */
220 flag = fcntl(pipe_fds[0], F_GETFL, 0);
222 fcntl(pipe_fds[0], F_SETFL, flag);
223 flag = fcntl(pipe_fds[1], F_GETFL, 0);
225 fcntl(pipe_fds[1], F_SETFL, flag);
228 real_source->fd.fd = pipe_fds[0];
229 real_source->fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
230 g_source_add_poll(source, &real_source->fd);
233 PySignal_SetWakeupFd(pipe_fds[1]);
238 PYGLIB_DEFINE_TYPE("gi._glib.MainLoop", PyGMainLoop_Type, PyGMainLoop)
241 pyg_main_loop_init(PyGMainLoop *self, PyObject *args, PyObject *kwargs)
243 static char *kwlist[] = { "context", "is_running", NULL };
244 PyObject *py_context = Py_None;
246 GMainContext *context;
248 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
249 "|Ob:GMainLoop.__init__",
250 kwlist, &py_context, &is_running))
253 if (!PyObject_TypeCheck(py_context, &PyGMainContext_Type) &&
254 py_context != Py_None) {
255 PyErr_SetString(PyExc_TypeError,
256 "context must be a gi._glib.MainContext or None");
260 if (py_context != Py_None) {
261 context = ((PyGMainContext*)py_context)->context;
266 self->loop = g_main_loop_new(context, is_running);
268 self->signal_source = pyg_signal_watch_new();
269 g_source_attach(self->signal_source, context);
270 g_source_unref(self->signal_source);
276 pyg_main_loop_dealloc(PyGMainLoop *self)
278 if (self->signal_source != NULL) {
279 g_source_destroy(self->signal_source);
280 self->signal_source = NULL;
283 if (self->loop != NULL) {
284 g_main_loop_unref(self->loop);
292 pyg_main_loop_richcompare(PyObject *self, PyObject *other, int op)
294 if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGMainLoop_Type)
295 return _pyglib_generic_ptr_richcompare(((PyGMainLoop*)self)->loop,
296 ((PyGMainLoop*)other)->loop,
299 Py_INCREF(Py_NotImplemented);
300 return Py_NotImplemented;
305 _wrap_g_main_loop_get_context (PyGMainLoop *loop)
307 return pyg_main_context_new(g_main_loop_get_context(loop->loop));
311 _wrap_g_main_loop_is_running (PyGMainLoop *self)
313 return PyBool_FromLong(g_main_loop_is_running(self->loop));
317 _wrap_g_main_loop_quit (PyGMainLoop *self, PyObject *args, PyObject *kwargs)
319 g_main_loop_quit(self->loop);
326 _wrap_g_main_loop_run (PyGMainLoop *self)
328 GMainLoop *prev_loop;
330 prev_loop = pyg_save_current_main_loop(self->loop);
332 pyglib_begin_allow_threads;
333 g_main_loop_run(self->loop);
334 pyglib_end_allow_threads;
336 pyg_restore_current_main_loop(prev_loop);
338 if (PyErr_Occurred())
345 static PyMethodDef _PyGMainLoop_methods[] = {
346 { "get_context", (PyCFunction)_wrap_g_main_loop_get_context, METH_NOARGS },
347 { "is_running", (PyCFunction)_wrap_g_main_loop_is_running, METH_NOARGS },
348 { "quit", (PyCFunction)_wrap_g_main_loop_quit, METH_VARARGS|METH_KEYWORDS },
349 { "run", (PyCFunction)_wrap_g_main_loop_run, METH_NOARGS },
354 pyglib_mainloop_register_types(PyObject *d)
356 PyGMainLoop_Type.tp_dealloc = (destructor)pyg_main_loop_dealloc;
357 PyGMainLoop_Type.tp_richcompare = pyg_main_loop_richcompare;
358 PyGMainLoop_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
359 PyGMainLoop_Type.tp_methods = _PyGMainLoop_methods;
360 PyGMainLoop_Type.tp_init = (initproc)pyg_main_loop_init;
361 PYGLIB_REGISTER_TYPE(d, PyGMainLoop_Type, "MainLoop");