summaryrefslogtreecommitdiffstats
path: root/glib/pyglib.c
diff options
context:
space:
mode:
authorJohan Dahlin <johan@gnome.org>2008-07-21 17:27:22 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-07-21 17:27:22 +0000
commitbf49647a03923975eb24ce634dae8254553db8de (patch)
treefac72b5b605789d7fa566332bd6a986b148a1ca5 /glib/pyglib.c
parent39cf2d9ca4fc9b50984ec172282d33b1843fceee (diff)
downloadpygobject-bf49647a03923975eb24ce634dae8254553db8de.tar.gz
pygobject-bf49647a03923975eb24ce634dae8254553db8de.tar.xz
pygobject-bf49647a03923975eb24ce634dae8254553db8de.zip
Move over Source, IOChannel, Idle, Timeout and PollFD to glib from
2008-07-21 Johan Dahlin <johan@gnome.org> * glib/Makefile.am: * glib/glibmodule.c (pyg_idle_add), (pyg_timeout_add), (pyg_timeout_add_seconds), (pyg_io_add_watch), (pyglib_register_api), (pyglib_register_error), (pyglib_register_version_tuples), (init_glib): * glib/pygiochannel.c (py_io_channel_next), (py_io_channel_shutdown), (py_io_channel_set_encoding), (py_io_channel_read_chars), (py_io_channel_write_chars), (py_io_channel_write_lines), (py_io_channel_flush), (py_io_channel_set_flags), (py_io_channel_read_line), (py_io_channel_read_lines), (py_io_channel_seek), (py_io_channel_init), (pyglib_iochannel_register_types): * glib/pygiochannel.h: * glib/pyglib-private.h: * glib/pyglib.c (pyglib_threads_enabled), (pyglib_gil_state_ensure), (pyglib_gil_state_release), (pyglib_enable_threads), (pyglib_block_threads), (pyglib_unblock_threads), (pyglib_set_thread_block_funcs), (pyglib_handler_marshal), (pyglib_destroy_notify): * glib/pyglib.h: * glib/pygsource.c (pyg_source_set_callback), (pyglib_source_register_types): * glib/pygsource.h: * glib/pygspawn.c (pyglib_spawn_register_types): * glib/pygspawn.h: * gobject/Makefile.am: * gobject/__init__.py: * gobject/gobjectmodule.c (pyg_set_thread_block_funcs), (init_gobject): * gobject/pygiochannel.c: * gobject/pygobject-private.h: * gobject/pygsource.c: Move over Source, IOChannel, Idle, Timeout and PollFD to glib from gobject. Clean up and add a bit of new api for glib. svn path=/trunk/; revision=846
Diffstat (limited to 'glib/pyglib.c')
-rw-r--r--glib/pyglib.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/glib/pyglib.c b/glib/pyglib.c
index 3f7ebbd..a6d3c26 100644
--- a/glib/pyglib.c
+++ b/glib/pyglib.c
@@ -80,12 +80,16 @@ pyglib_init_internal(PyObject *api)
gboolean
pyglib_threads_enabled(void)
{
+ g_return_val_if_fail (_PyGLib_API != NULL, FALSE);
+
return _PyGLib_API->threads_enabled;
}
PyGILState_STATE
pyglib_gil_state_ensure(void)
{
+ g_return_val_if_fail (_PyGLib_API != NULL, PyGILState_LOCKED);
+
if (!_PyGLib_API->threads_enabled)
return PyGILState_LOCKED;
@@ -95,6 +99,8 @@ pyglib_gil_state_ensure(void)
void
pyglib_gil_state_release(PyGILState_STATE state)
{
+ g_return_if_fail (_PyGLib_API != NULL);
+
if (!_PyGLib_API->threads_enabled)
return;
@@ -116,6 +122,8 @@ pyglib_enable_threads(void)
gboolean
pyglib_enable_threads(void)
{
+ g_return_val_if_fail (_PyGLib_API != NULL, FALSE);
+
if (_PyGLib_API->threads_enabled)
return TRUE;
@@ -142,6 +150,47 @@ pyglib_gil_state_release_py23 (int flag)
PyGILState_Release(flag);
}
+/**
+ * pyglib_block_threads:
+ *
+ */
+void
+pyglib_block_threads(void)
+{
+ g_return_if_fail (_PyGLib_API != NULL);
+
+ if (_PyGLib_API->block_threads != NULL)
+ (* _PyGLib_API->block_threads)();
+}
+
+/**
+ * pyglib_unblock_threads:
+ *
+ */
+void
+pyglib_unblock_threads(void)
+{
+ g_return_if_fail (_PyGLib_API != NULL);
+ if (_PyGLib_API->unblock_threads != NULL)
+ (* _PyGLib_API->unblock_threads)();
+}
+
+/**
+ * pyglib_set_thread_block_funcs:
+ *
+ * hooks to register handlers for getting GDK threads to cooperate
+ * with python threading
+ */
+void
+pyglib_set_thread_block_funcs (PyGLibThreadBlockFunc block_threads_func,
+ PyGLibThreadBlockFunc unblock_threads_func)
+{
+ g_return_if_fail (_PyGLib_API != NULL);
+
+ _PyGLib_API->block_threads = block_threads_func;
+ _PyGLib_API->unblock_threads = unblock_threads_func;
+}
+
/**
* pyglib_error_check:
@@ -289,3 +338,50 @@ pyglib_main_context_new(GMainContext *context)
self->context = g_main_context_ref(context);
return (PyObject *)self;
}
+
+gboolean
+pyglib_handler_marshal(gpointer user_data)
+{
+ PyObject *tuple, *ret;
+ gboolean res;
+ PyGILState_STATE state;
+
+ g_return_val_if_fail(user_data != NULL, FALSE);
+
+ state = pyglib_gil_state_ensure();
+
+ tuple = (PyObject *)user_data;
+ ret = PyObject_CallObject(PyTuple_GetItem(tuple, 0),
+ PyTuple_GetItem(tuple, 1));
+ if (!ret) {
+ PyErr_Print();
+ res = FALSE;
+ } else {
+ res = PyObject_IsTrue(ret);
+ Py_DECREF(ret);
+ }
+
+ pyglib_gil_state_release(state);
+
+ return res;
+}
+
+/**
+ * pyglib_destroy_notify:
+ * @user_data: a PyObject pointer.
+ *
+ * A function that can be used as a GDestroyNotify callback that will
+ * call Py_DECREF on the data.
+ */
+void
+pyglib_destroy_notify(gpointer user_data)
+{
+ PyObject *obj = (PyObject *)user_data;
+ PyGILState_STATE state;
+
+ g_return_if_fail (_PyGLib_API != NULL);
+
+ state = pyglib_gil_state_ensure();
+ Py_DECREF(obj);
+ pyglib_gil_state_release(state);
+}