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;
162 if (poll_fd->revents & G_IO_IN)
163 ret = read(poll_fd->fd, &dummy, 1);
166 state = pyglib_gil_state_ensure();
168 main_loop = pyg_get_current_main_loop();
170 if (PyErr_CheckSignals() == -1 && main_loop != NULL) {
171 PyErr_SetNone(PyExc_KeyboardInterrupt);
172 g_main_loop_quit(main_loop);
175 pyglib_gil_state_release(state);
181 pyg_signal_watch_dispatch(GSource *source,
182 GSourceFunc callback,
185 /* We should never be dispatched */
186 g_assert_not_reached();
190 static GSourceFuncs pyg_signal_watch_funcs =
192 pyg_signal_watch_prepare,
193 pyg_signal_watch_check,
194 pyg_signal_watch_dispatch
198 pyg_signal_watch_new(void)
200 GSource *source = g_source_new(&pyg_signal_watch_funcs,
201 sizeof(PySignalWatchSource));
203 #ifdef HAVE_PYSIGNAL_SETWAKEUPFD
204 PySignalWatchSource *real_source = (PySignalWatchSource *)source;
207 /* Unfortunately we need to create a new pipe here instead of
208 * reusing the pipe inside the GMainContext.
209 * Ideally an api should be added to GMainContext which allows us
210 * to reuse that pipe which would suit us perfectly fine.
211 * XXX More efficient than a pipe, we could use an eventfd on Linux
212 * kernels that support it.
214 gint already_piped = (pipe_fds[0] > 0);
215 if (!already_piped) {
216 if (pipe(pipe_fds) < 0)
217 g_error("Cannot create main loop pipe: %s\n",
220 /* Make the write end of the fd non blocking */
221 flag = fcntl(pipe_fds[1], F_GETFL, 0);
223 fcntl(pipe_fds[1], F_SETFL, flag);
226 real_source->fd.fd = pipe_fds[0];
227 real_source->fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
228 g_source_add_poll(source, &real_source->fd);
231 PySignal_SetWakeupFd(pipe_fds[1]);
236 PYGLIB_DEFINE_TYPE("glib.MainLoop", PyGMainLoop_Type, PyGMainLoop)
239 pyg_main_loop_init(PyGMainLoop *self, PyObject *args, PyObject *kwargs)
241 static char *kwlist[] = { "context", "is_running", NULL };
242 PyObject *py_context = Py_None;
244 GMainContext *context;
246 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
247 "|Ob:GMainLoop.__init__",
248 kwlist, &py_context, &is_running))
251 if (!PyObject_TypeCheck(py_context, &PyGMainContext_Type) &&
252 py_context != Py_None) {
253 PyErr_SetString(PyExc_TypeError,
254 "context must be a glib.MainContext or None");
258 if (py_context != Py_None) {
259 context = ((PyGMainContext*)py_context)->context;
264 self->loop = g_main_loop_new(context, is_running);
266 self->signal_source = pyg_signal_watch_new();
267 g_source_attach(self->signal_source, context);
268 g_source_unref(self->signal_source);
274 pyg_main_loop_dealloc(PyGMainLoop *self)
276 if (self->signal_source != NULL) {
277 g_source_destroy(self->signal_source);
278 self->signal_source = NULL;
281 if (self->loop != NULL) {
282 g_main_loop_unref(self->loop);
290 pyg_main_loop_richcompare(PyObject *self, PyObject *other, int op)
292 if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGMainLoop_Type)
293 return _pyglib_generic_ptr_richcompare(((PyGMainLoop*)self)->loop,
294 ((PyGMainLoop*)other)->loop,
297 Py_INCREF(Py_NotImplemented);
298 return Py_NotImplemented;
303 _wrap_g_main_loop_get_context (PyGMainLoop *loop)
305 return pyg_main_context_new(g_main_loop_get_context(loop->loop));
309 _wrap_g_main_loop_is_running (PyGMainLoop *self)
311 return PyBool_FromLong(g_main_loop_is_running(self->loop));
315 _wrap_g_main_loop_quit (PyGMainLoop *self)
317 g_main_loop_quit(self->loop);
324 _wrap_g_main_loop_run (PyGMainLoop *self)
326 GMainLoop *prev_loop;
328 prev_loop = pyg_save_current_main_loop(self->loop);
330 pyglib_begin_allow_threads;
331 g_main_loop_run(self->loop);
332 pyglib_end_allow_threads;
334 pyg_restore_current_main_loop(prev_loop);
336 if (PyErr_Occurred())
343 static PyMethodDef _PyGMainLoop_methods[] = {
344 { "get_context", (PyCFunction)_wrap_g_main_loop_get_context, METH_NOARGS },
345 { "is_running", (PyCFunction)_wrap_g_main_loop_is_running, METH_NOARGS },
346 { "quit", (PyCFunction)_wrap_g_main_loop_quit, METH_NOARGS },
347 { "run", (PyCFunction)_wrap_g_main_loop_run, METH_NOARGS },
352 pyglib_mainloop_register_types(PyObject *d)
354 PyGMainLoop_Type.tp_dealloc = (destructor)pyg_main_loop_dealloc;
355 PyGMainLoop_Type.tp_richcompare = pyg_main_loop_richcompare;
356 PyGMainLoop_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
357 PyGMainLoop_Type.tp_methods = _PyGMainLoop_methods;
358 PyGMainLoop_Type.tp_init = (initproc)pyg_main_loop_init;
359 PYGLIB_REGISTER_TYPE(d, PyGMainLoop_Type, "MainLoop");