| 1 | n/a | #include "Python.h" |
|---|
| 2 | n/a | #include <CoreFoundation/CFRunLoop.h> |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | /* These macros are defined in Python 2.3 but not 2.2 */ |
|---|
| 5 | n/a | #ifndef PyMODINIT_FUNC |
|---|
| 6 | n/a | #define PyMODINIT_FUNC void |
|---|
| 7 | n/a | #endif |
|---|
| 8 | n/a | #ifndef PyDoc_STRVAR |
|---|
| 9 | n/a | #define PyDoc_STRVAR(Var,Str) static char Var[] = Str |
|---|
| 10 | n/a | #endif |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | #undef AUTOGIL_DEBUG |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | static PyObject *AutoGILError; |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | static void autoGILCallback(CFRunLoopObserverRef observer, |
|---|
| 19 | n/a | CFRunLoopActivity activity, |
|---|
| 20 | n/a | void *info) { |
|---|
| 21 | n/a | PyThreadState **p_tstate = (PyThreadState **)info; |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | switch (activity) { |
|---|
| 24 | n/a | case kCFRunLoopBeforeWaiting: |
|---|
| 25 | n/a | /* going to sleep, release GIL */ |
|---|
| 26 | n/a | #ifdef AUTOGIL_DEBUG |
|---|
| 27 | n/a | fprintf(stderr, "going to sleep, release GIL\n"); |
|---|
| 28 | n/a | #endif |
|---|
| 29 | n/a | *p_tstate = PyEval_SaveThread(); |
|---|
| 30 | n/a | break; |
|---|
| 31 | n/a | case kCFRunLoopAfterWaiting: |
|---|
| 32 | n/a | /* waking up, acquire GIL */ |
|---|
| 33 | n/a | #ifdef AUTOGIL_DEBUG |
|---|
| 34 | n/a | fprintf(stderr, "waking up, acquire GIL\n"); |
|---|
| 35 | n/a | #endif |
|---|
| 36 | n/a | PyEval_RestoreThread(*p_tstate); |
|---|
| 37 | n/a | *p_tstate = NULL; |
|---|
| 38 | n/a | break; |
|---|
| 39 | n/a | default: |
|---|
| 40 | n/a | break; |
|---|
| 41 | n/a | } |
|---|
| 42 | n/a | } |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | static void infoRelease(const void *info) { |
|---|
| 45 | n/a | /* XXX This should get called when the run loop is deallocated, |
|---|
| 46 | n/a | but this doesn't seem to happen. So for now: leak. */ |
|---|
| 47 | n/a | PyMem_Free((void *)info); |
|---|
| 48 | n/a | } |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | static PyObject * |
|---|
| 51 | n/a | autoGIL_installAutoGIL(PyObject *self) |
|---|
| 52 | n/a | { |
|---|
| 53 | n/a | PyObject *tstate_dict = PyThreadState_GetDict(); |
|---|
| 54 | n/a | PyObject *v; |
|---|
| 55 | n/a | CFRunLoopRef rl; |
|---|
| 56 | n/a | PyThreadState **p_tstate; /* for use in the info field */ |
|---|
| 57 | n/a | CFRunLoopObserverContext context = {0, NULL, NULL, NULL, NULL}; |
|---|
| 58 | n/a | CFRunLoopObserverRef observer; |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | if (tstate_dict == NULL) |
|---|
| 61 | n/a | return NULL; |
|---|
| 62 | n/a | v = PyDict_GetItemString(tstate_dict, "autoGIL.InstalledAutoGIL"); |
|---|
| 63 | n/a | if (v != NULL) { |
|---|
| 64 | n/a | /* we've already installed a callback for this thread */ |
|---|
| 65 | n/a | Py_INCREF(Py_None); |
|---|
| 66 | n/a | return Py_None; |
|---|
| 67 | n/a | } |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | rl = CFRunLoopGetCurrent(); |
|---|
| 70 | n/a | if (rl == NULL) { |
|---|
| 71 | n/a | PyErr_SetString(AutoGILError, |
|---|
| 72 | n/a | "can't get run loop for current thread"); |
|---|
| 73 | n/a | return NULL; |
|---|
| 74 | n/a | } |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | p_tstate = PyMem_Malloc(sizeof(PyThreadState *)); |
|---|
| 77 | n/a | if (p_tstate == NULL) { |
|---|
| 78 | n/a | PyErr_SetString(PyExc_MemoryError, |
|---|
| 79 | n/a | "not enough memory to allocate " |
|---|
| 80 | n/a | "tstate pointer"); |
|---|
| 81 | n/a | return NULL; |
|---|
| 82 | n/a | } |
|---|
| 83 | n/a | *p_tstate = NULL; |
|---|
| 84 | n/a | context.info = (void *)p_tstate; |
|---|
| 85 | n/a | context.release = infoRelease; |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | observer = CFRunLoopObserverCreate( |
|---|
| 88 | n/a | NULL, |
|---|
| 89 | n/a | kCFRunLoopBeforeWaiting | kCFRunLoopAfterWaiting, |
|---|
| 90 | n/a | 1, 0, autoGILCallback, &context); |
|---|
| 91 | n/a | if (observer == NULL) { |
|---|
| 92 | n/a | PyErr_SetString(AutoGILError, |
|---|
| 93 | n/a | "can't create event loop observer"); |
|---|
| 94 | n/a | return NULL; |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | CFRunLoopAddObserver(rl, observer, kCFRunLoopDefaultMode); |
|---|
| 97 | n/a | /* XXX how to check for errors? */ |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | /* register that we have installed a callback for this thread */ |
|---|
| 100 | n/a | if (PyDict_SetItemString(tstate_dict, "autoGIL.InstalledAutoGIL", |
|---|
| 101 | n/a | Py_None) < 0) |
|---|
| 102 | n/a | return NULL; |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | Py_INCREF(Py_None); |
|---|
| 105 | n/a | return Py_None; |
|---|
| 106 | n/a | } |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | PyDoc_STRVAR(autoGIL_installAutoGIL_doc, |
|---|
| 109 | n/a | "installAutoGIL() -> None\n\ |
|---|
| 110 | n/a | Install an observer callback in the event loop (CFRunLoop) for the\n\ |
|---|
| 111 | n/a | current thread, that will lock and unlock the Global Interpreter Lock\n\ |
|---|
| 112 | n/a | (GIL) at appropriate times, allowing other Python threads to run while\n\ |
|---|
| 113 | n/a | the event loop is idle." |
|---|
| 114 | n/a | ); |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | static PyMethodDef autoGIL_methods[] = { |
|---|
| 117 | n/a | { |
|---|
| 118 | n/a | "installAutoGIL", |
|---|
| 119 | n/a | (PyCFunction)autoGIL_installAutoGIL, |
|---|
| 120 | n/a | METH_NOARGS, |
|---|
| 121 | n/a | autoGIL_installAutoGIL_doc |
|---|
| 122 | n/a | }, |
|---|
| 123 | n/a | { 0, 0, 0, 0 } /* sentinel */ |
|---|
| 124 | n/a | }; |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | PyDoc_STRVAR(autoGIL_docs, |
|---|
| 127 | n/a | "The autoGIL module provides a function (installAutoGIL) that\n\ |
|---|
| 128 | n/a | automatically locks and unlocks Python's Global Interpreter Lock\n\ |
|---|
| 129 | n/a | when running an event loop." |
|---|
| 130 | n/a | ); |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | PyMODINIT_FUNC |
|---|
| 133 | n/a | initautoGIL(void) |
|---|
| 134 | n/a | { |
|---|
| 135 | n/a | PyObject *mod; |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | if (PyErr_WarnPy3k("In 3.x, the autoGIL module is removed.", 1) < 0) |
|---|
| 138 | n/a | return; |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | mod = Py_InitModule4("autoGIL", autoGIL_methods, autoGIL_docs, |
|---|
| 141 | n/a | NULL, PYTHON_API_VERSION); |
|---|
| 142 | n/a | if (mod == NULL) |
|---|
| 143 | n/a | return; |
|---|
| 144 | n/a | AutoGILError = PyErr_NewException("autoGIL.AutoGILError", |
|---|
| 145 | n/a | PyExc_Exception, NULL); |
|---|
| 146 | n/a | if (AutoGILError == NULL) |
|---|
| 147 | n/a | return; |
|---|
| 148 | n/a | Py_INCREF(AutoGILError); |
|---|
| 149 | n/a | if (PyModule_AddObject(mod, "AutoGILError", |
|---|
| 150 | n/a | AutoGILError) < 0) |
|---|
| 151 | n/a | return; |
|---|
| 152 | n/a | } |
|---|