2003-05-02 21:18:55 -03:00
|
|
|
#include "Python.h"
|
|
|
|
#include <CoreFoundation/CFRunLoop.h>
|
|
|
|
|
|
|
|
/* These macros are defined in Python 2.3 but not 2.2 */
|
|
|
|
#ifndef PyMODINIT_FUNC
|
|
|
|
#define PyMODINIT_FUNC void
|
|
|
|
#endif
|
|
|
|
#ifndef PyDoc_STRVAR
|
|
|
|
#define PyDoc_STRVAR(Var,Str) static char Var[] = Str
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#undef AUTOGIL_DEBUG
|
|
|
|
|
|
|
|
static PyObject *AutoGILError;
|
|
|
|
|
|
|
|
|
|
|
|
static void autoGILCallback(CFRunLoopObserverRef observer,
|
2010-05-09 12:15:40 -03:00
|
|
|
CFRunLoopActivity activity,
|
|
|
|
void *info) {
|
|
|
|
PyThreadState **p_tstate = (PyThreadState **)info;
|
2003-05-02 21:18:55 -03:00
|
|
|
|
2010-05-09 12:15:40 -03:00
|
|
|
switch (activity) {
|
|
|
|
case kCFRunLoopBeforeWaiting:
|
|
|
|
/* going to sleep, release GIL */
|
2003-05-02 21:18:55 -03:00
|
|
|
#ifdef AUTOGIL_DEBUG
|
2010-05-09 12:15:40 -03:00
|
|
|
fprintf(stderr, "going to sleep, release GIL\n");
|
2003-05-02 21:18:55 -03:00
|
|
|
#endif
|
2010-05-09 12:15:40 -03:00
|
|
|
*p_tstate = PyEval_SaveThread();
|
|
|
|
break;
|
|
|
|
case kCFRunLoopAfterWaiting:
|
|
|
|
/* waking up, acquire GIL */
|
2003-05-02 21:18:55 -03:00
|
|
|
#ifdef AUTOGIL_DEBUG
|
2010-05-09 12:15:40 -03:00
|
|
|
fprintf(stderr, "waking up, acquire GIL\n");
|
2003-05-02 21:18:55 -03:00
|
|
|
#endif
|
2010-05-09 12:15:40 -03:00
|
|
|
PyEval_RestoreThread(*p_tstate);
|
|
|
|
*p_tstate = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2003-05-02 21:18:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void infoRelease(const void *info) {
|
2010-05-09 12:15:40 -03:00
|
|
|
/* XXX This should get called when the run loop is deallocated,
|
|
|
|
but this doesn't seem to happen. So for now: leak. */
|
|
|
|
PyMem_Free((void *)info);
|
2003-05-02 21:18:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
autoGIL_installAutoGIL(PyObject *self)
|
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
PyObject *tstate_dict = PyThreadState_GetDict();
|
|
|
|
PyObject *v;
|
|
|
|
CFRunLoopRef rl;
|
|
|
|
PyThreadState **p_tstate; /* for use in the info field */
|
|
|
|
CFRunLoopObserverContext context = {0, NULL, NULL, NULL, NULL};
|
|
|
|
CFRunLoopObserverRef observer;
|
|
|
|
|
|
|
|
if (tstate_dict == NULL)
|
|
|
|
return NULL;
|
|
|
|
v = PyDict_GetItemString(tstate_dict, "autoGIL.InstalledAutoGIL");
|
|
|
|
if (v != NULL) {
|
|
|
|
/* we've already installed a callback for this thread */
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl = CFRunLoopGetCurrent();
|
|
|
|
if (rl == NULL) {
|
|
|
|
PyErr_SetString(AutoGILError,
|
|
|
|
"can't get run loop for current thread");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_tstate = PyMem_Malloc(sizeof(PyThreadState *));
|
|
|
|
if (p_tstate == NULL) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
"not enough memory to allocate "
|
|
|
|
"tstate pointer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*p_tstate = NULL;
|
|
|
|
context.info = (void *)p_tstate;
|
|
|
|
context.release = infoRelease;
|
|
|
|
|
|
|
|
observer = CFRunLoopObserverCreate(
|
|
|
|
NULL,
|
|
|
|
kCFRunLoopBeforeWaiting | kCFRunLoopAfterWaiting,
|
|
|
|
1, 0, autoGILCallback, &context);
|
|
|
|
if (observer == NULL) {
|
|
|
|
PyErr_SetString(AutoGILError,
|
|
|
|
"can't create event loop observer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
CFRunLoopAddObserver(rl, observer, kCFRunLoopDefaultMode);
|
|
|
|
/* XXX how to check for errors? */
|
|
|
|
|
|
|
|
/* register that we have installed a callback for this thread */
|
|
|
|
if (PyDict_SetItemString(tstate_dict, "autoGIL.InstalledAutoGIL",
|
|
|
|
Py_None) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2003-05-02 21:18:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(autoGIL_installAutoGIL_doc,
|
|
|
|
"installAutoGIL() -> None\n\
|
|
|
|
Install an observer callback in the event loop (CFRunLoop) for the\n\
|
2003-05-02 21:38:26 -03:00
|
|
|
current thread, that will lock and unlock the Global Interpreter Lock\n\
|
2003-05-02 21:18:55 -03:00
|
|
|
(GIL) at appropriate times, allowing other Python threads to run while\n\
|
2003-05-02 21:38:26 -03:00
|
|
|
the event loop is idle."
|
2003-05-02 21:18:55 -03:00
|
|
|
);
|
|
|
|
|
|
|
|
static PyMethodDef autoGIL_methods[] = {
|
2010-05-09 12:15:40 -03:00
|
|
|
{
|
|
|
|
"installAutoGIL",
|
|
|
|
(PyCFunction)autoGIL_installAutoGIL,
|
|
|
|
METH_NOARGS,
|
|
|
|
autoGIL_installAutoGIL_doc
|
|
|
|
},
|
|
|
|
{ 0, 0, 0, 0 } /* sentinel */
|
2003-05-02 21:18:55 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(autoGIL_docs,
|
|
|
|
"The autoGIL module provides a function (installAutoGIL) that\n\
|
|
|
|
automatically locks and unlocks Python's Global Interpreter Lock\n\
|
|
|
|
when running an event loop."
|
|
|
|
);
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
initautoGIL(void)
|
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
PyObject *mod;
|
|
|
|
|
|
|
|
if (PyErr_WarnPy3k("In 3.x, the autoGIL module is removed.", 1) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mod = Py_InitModule4("autoGIL", autoGIL_methods, autoGIL_docs,
|
|
|
|
NULL, PYTHON_API_VERSION);
|
|
|
|
if (mod == NULL)
|
|
|
|
return;
|
|
|
|
AutoGILError = PyErr_NewException("autoGIL.AutoGILError",
|
|
|
|
PyExc_Exception, NULL);
|
|
|
|
if (AutoGILError == NULL)
|
|
|
|
return;
|
|
|
|
Py_INCREF(AutoGILError);
|
|
|
|
if (PyModule_AddObject(mod, "AutoGILError",
|
|
|
|
AutoGILError) < 0)
|
|
|
|
return;
|
2003-05-02 21:18:55 -03:00
|
|
|
}
|