|
From: David B. <dav...@us...> - 2004-11-28 20:45:32
|
Update of /cvsroot/frontierkernel/Frontier/Common/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22827 Modified Files: Tag: sidewinder-branch langpython.c Log Message: python threading support Index: langpython.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langpython.c,v retrieving revision 1.2.4.20 retrieving revision 1.2.4.21 diff -C2 -d -r1.2.4.20 -r1.2.4.21 *** langpython.c 28 Nov 2004 18:30:43 -0000 1.2.4.20 --- langpython.c 28 Nov 2004 20:45:18 -0000 1.2.4.21 *************** *** 218,221 **** --- 218,226 ---- PyObject * (*ptr_PyObject_CallObject) (PyObject *, PyObject *); + int (*ptr_PySys_SetArgv) (int, char **); + + void (*ptr_PyEval_RestoreThread) (PyThreadState *); + PyThreadState * (*ptr_PyEval_SaveThread) (void); + } py; *************** *** 316,319 **** --- 321,329 ---- #define PyObject_CallObject (*py.ptr_PyObject_CallObject) + #define PySys_SetArgv (*py.ptr_PySys_SetArgv) + + #define PyEval_SaveThread (*py.ptr_PyEval_SaveThread) + #define PyEval_RestoreThread (*py.ptr_PyEval_RestoreThread) + static boolean importsymbol (typythonlib hm, char *symbol, void **ptraddress) { *************** *** 450,453 **** --- 460,468 ---- fl = fl && IMPORTSYMBOL (hm, PyObject_CallObject); + fl = fl && IMPORTSYMBOL (hm, PySys_SetArgv); + + fl = fl && IMPORTSYMBOL (hm, PyEval_SaveThread); + fl = fl && IMPORTSYMBOL (hm, PyEval_RestoreThread); + if (fl) { *************** *** 519,522 **** --- 534,549 ---- static PyObject *frontierModule = NULL; + PyThreadState *_save; + + static void + getpythonlock() { + PyEval_RestoreThread(_save); + } + + static void + releasepythonlock() { + _save = PyEval_SaveThread(); + } + static boolean convertPython(PyObject *, hdlhashtable, bigstring); *************** *** 937,947 **** fetch(PyObject *self, PyObject *args) { char *as; if (!PyArg_ParseTuple(args, "s:fetch", &as)) { PyErr_SetString(PyExc_ValueError, "Requires a string representing an address in the Frontier database"); return NULL; } ! return fetchAux(as); } --- 964,982 ---- fetch(PyObject *self, PyObject *args) { char *as; + PyObject *result; + + getpythonlock(); if (!PyArg_ParseTuple(args, "s:fetch", &as)) { PyErr_SetString(PyExc_ValueError, "Requires a string representing an address in the Frontier database"); + releasepythonlock(); return NULL; } ! result = fetchAux(as); ! ! releasepythonlock(); ! ! return result; } *************** *** 953,958 **** --- 988,996 ---- PyObject *result; + getpythonlock(); + if (!PyArg_ParseTuple(args, "s:evaluate", &s)) { PyErr_SetString(PyExc_ValueError, "Couldn't execute because evaluate requires a string argument."); + releasepythonlock(); return NULL; } *************** *** 960,963 **** --- 998,1002 ---- if (!newfilledhandle(s, strlen(s), &htext)) { PyErr_SetString(PyExc_SystemError, "Couldn't execute because I couldn't allocate space for the string."); + releasepythonlock(); return NULL; } *************** *** 965,968 **** --- 1004,1008 ---- if (!langrun(htext, &v)) { PyErr_SetString(PyExc_SyntaxError, "Couldn't execute because of a syntax error in the UserTalk code."); + releasepythonlock(); return NULL; } *************** *** 970,976 **** --- 1010,1018 ---- if (!convertfrontiervalue(&v, &result)) { PyErr_SetString(PyExc_SystemError, "Couldn't execute because of an error converting the UserTalk return value."); + releasepythonlock(); return NULL; } + releasepythonlock(); return result; } *************** *** 1069,1076 **** --- 1111,1121 ---- copyctopstring(name, loc); + getpythonlock(); + if (!hashtablelookup(self->table, loc, &val, &node)) { char msg[300]; sprintf(msg, "No table entry named %s.", name); PyErr_SetString(PyExc_KeyError, msg); + releasepythonlock(); return NULL; } *************** *** 1080,1086 **** --- 1125,1133 ---- sprintf(msg, "Cannot convert table entry named %s.", name); PyErr_SetString(PyExc_SystemError, msg); + releasepythonlock(); return NULL; } + releasepythonlock(); return result; } *************** *** 1088,1097 **** static PyObject * Table_getattr(frontier_TableObject *self, char *name) { ! PyObject *method = Py_FindMethod(tableMethods, (PyObject *)self, name); ! if (method != NULL) return method; PyErr_Clear(); return Table_get(self, name); } --- 1135,1152 ---- static PyObject * Table_getattr(frontier_TableObject *self, char *name) { ! PyObject *method; ! ! getpythonlock(); ! ! method = Py_FindMethod(tableMethods, (PyObject *)self, name); ! if (method != NULL) { ! releasepythonlock(); return method; + } PyErr_Clear(); + releasepythonlock(); + return Table_get(self, name); } *************** *** 1103,1109 **** copyctopstring(name, loc); ! if (!convertPython(val, self->table, loc)) return -1; ! return 0; } --- 1158,1169 ---- copyctopstring(name, loc); ! getpythonlock(); ! ! if (!convertPython(val, self->table, loc)) { ! releasepythonlock(); return -1; ! } ! ! releasepythonlock(); return 0; } *************** *** 1124,1129 **** --- 1184,1192 ---- PyObject *result; + getpythonlock(); + if (!hashgetnthnode(self->table, index, &node)) { PyErr_SetString(PyExc_IndexError, "Index out of bounds"); + releasepythonlock(); return NULL; } *************** *** 1131,1137 **** --- 1194,1202 ---- if (!convertfrontiervalue(&((**node).val), &result)) { PyErr_SetString(PyExc_ValueError, "Cannot convert value"); + releasepythonlock(); return NULL; } + releasepythonlock(); return result; } *************** *** 1140,1151 **** Table_setIndex(frontier_TableObject *self, int index, PyObject *val) { hdlhashnode node; if (!hashgetnthnode(self->table, index, &node)) { PyErr_SetString(PyExc_IndexError, "Index out of bounds"); return 0; } ! if (!convertPython(val, self->table, (**node).hashkey)) return -1; return 0; } --- 1205,1223 ---- Table_setIndex(frontier_TableObject *self, int index, PyObject *val) { hdlhashnode node; + + getpythonlock(); + if (!hashgetnthnode(self->table, index, &node)) { PyErr_SetString(PyExc_IndexError, "Index out of bounds"); + releasepythonlock(); return 0; } ! if (!convertPython(val, self->table, (**node).hashkey)) { ! releasepythonlock(); return -1; + } + releasepythonlock(); return 0; } *************** *** 1154,1160 **** Table_getItem(frontier_TableObject *self, PyObject *key) { char *keyName; ! if (PyInt_Check(key)) return Table_getIndex(self, PyInt_AsLong(key)); keyName = PyString_AsString(PyObject_Str(key)); return Table_get(self, keyName); } --- 1226,1239 ---- Table_getItem(frontier_TableObject *self, PyObject *key) { char *keyName; ! ! getpythonlock(); ! ! if (PyInt_Check(key)) { ! releasepythonlock(); return Table_getIndex(self, PyInt_AsLong(key)); + } keyName = PyString_AsString(PyObject_Str(key)); + + releasepythonlock(); return Table_get(self, keyName); } *************** *** 1163,1169 **** Table_setItem(frontier_TableObject *self, PyObject *key, PyObject *val) { char *keyName; ! if (PyInt_Check(key)) return Table_setIndex(self, PyInt_AsLong(key), val); keyName = PyString_AsString(PyObject_Str(key)); return Table_setattr(self, keyName, val); } --- 1242,1255 ---- Table_setItem(frontier_TableObject *self, PyObject *key, PyObject *val) { char *keyName; ! ! getpythonlock(); ! ! if (PyInt_Check(key)) { ! releasepythonlock(); return Table_setIndex(self, PyInt_AsLong(key), val); + } keyName = PyString_AsString(PyObject_Str(key)); + + releasepythonlock(); return Table_setattr(self, keyName, val); } *************** *** 1196,1200 **** --- 1282,1288 ---- copyptocstring((**node).hashkey, name); + getpythonlock(); PyList_Append(keyList, PyString_FromString(name)); + releasepythonlock(); return true; *************** *** 1203,1213 **** static PyObject * Table_keys(frontier_TableObject *self) { ! PyObject *keyList = PyList_New(0); ! if (!hashtablevisit(self->table, Table_keysCallback, (ptrvoid)keyList)) { PyErr_SetString(PyExc_SystemError, "Cannot create key list"); return NULL; } return keyList; } --- 1291,1307 ---- static PyObject * Table_keys(frontier_TableObject *self) { ! PyObject *keyList; ! ! getpythonlock(); ! ! keyList = PyList_New(0); ! if (!hashtablevisit(self->table, Table_keysCallback, (ptrvoid)keyList)) { PyErr_SetString(PyExc_SystemError, "Cannot create key list"); + releasepythonlock(); return NULL; } + releasepythonlock(); return keyList; } *************** *** 1218,1224 **** --- 1312,1322 ---- bigstring loc; hdlhashtable newTable; + PyObject *nt; + + getpythonlock(); if (!PyArg_ParseTuple(args, "s:newTable", &name)) { PyErr_SetString(PyExc_TypeError, "Table.newTable requires a string argument"); + releasepythonlock(); return NULL; } *************** *** 1231,1235 **** } ! return newTableObject(newTable); } --- 1329,1337 ---- } ! nt = newTableObject(newTable); ! ! releasepythonlock(); ! ! return nt; } *************** *** 1411,1417 **** static PyObject * newTableObject(hdlhashtable hdl) { ! PyObject *class = PyObject_GetAttrString(frontierModule, "Table"); frontier_TableObject *obj = PyObject_New(frontier_TableObject, (PyTypeObject *)class); obj->table = hdl; return (PyObject *)obj; } --- 1513,1522 ---- static PyObject * newTableObject(hdlhashtable hdl) { ! PyObject *class; ! ! class = PyObject_GetAttrString(frontierModule, "Table"); frontier_TableObject *obj = PyObject_New(frontier_TableObject, (PyTypeObject *)class); obj->table = hdl; + return (PyObject *)obj; } *************** *** 1419,1424 **** static PyObject * newOutlineObject() { ! PyObject *class = PyObject_GetAttrString(frontierModule, "Outline"); frontier_OutlineObject *obj = PyObject_New(frontier_OutlineObject, (PyTypeObject *)class); return (PyObject *)obj; } --- 1524,1532 ---- static PyObject * newOutlineObject() { ! PyObject *class; ! ! class = PyObject_GetAttrString(frontierModule, "Outline"); frontier_OutlineObject *obj = PyObject_New(frontier_OutlineObject, (PyTypeObject *)class); + return (PyObject *)obj; } *************** *** 1426,1431 **** static PyObject * newWPTextObject() { ! PyObject *class = PyObject_GetAttrString(frontierModule, "WPText"); frontier_WPTextObject *obj = PyObject_New(frontier_WPTextObject, (PyTypeObject *)class); return (PyObject *)obj; } --- 1534,1542 ---- static PyObject * newWPTextObject() { ! PyObject *class; ! ! class = PyObject_GetAttrString(frontierModule, "WPText"); frontier_WPTextObject *obj = PyObject_New(frontier_WPTextObject, (PyTypeObject *)class); + return (PyObject *)obj; } *************** *** 1433,1438 **** static PyObject * newScriptObject() { ! PyObject *class = PyObject_GetAttrString(frontierModule, "Script"); frontier_ScriptObject *obj = PyObject_New(frontier_ScriptObject, (PyTypeObject *)class); return (PyObject *)obj; } --- 1544,1552 ---- static PyObject * newScriptObject() { ! PyObject *class; ! ! class = PyObject_GetAttrString(frontierModule, "Script"); frontier_ScriptObject *obj = PyObject_New(frontier_ScriptObject, (PyTypeObject *)class); + return (PyObject *)obj; } *************** *** 1440,1446 **** static PyObject * newInternalValueObject(tyvaluetype vt) { ! PyObject *class = PyObject_GetAttrString(frontierModule, "InternalValue"); frontier_InternalValue *obj = PyObject_New(frontier_InternalValue, (PyTypeObject *)class); obj->vartype = vt; return (PyObject *)obj; } --- 1554,1563 ---- static PyObject * newInternalValueObject(tyvaluetype vt) { ! PyObject *class; ! ! class = PyObject_GetAttrString(frontierModule, "InternalValue"); frontier_InternalValue *obj = PyObject_New(frontier_InternalValue, (PyTypeObject *)class); obj->vartype = vt; + return (PyObject *)obj; } *************** *** 1448,1454 **** static PyObject * newExternalValueObject(tyexternalid ei) { ! PyObject *class = PyObject_GetAttrString(frontierModule, "ExternalValue"); frontier_ExternalValue *obj = PyObject_New(frontier_ExternalValue, (PyTypeObject *)class); obj->extvartype = ei; return (PyObject *)obj; } --- 1565,1574 ---- static PyObject * newExternalValueObject(tyexternalid ei) { ! PyObject *class; ! ! class = PyObject_GetAttrString(frontierModule, "ExternalValue"); frontier_ExternalValue *obj = PyObject_New(frontier_ExternalValue, (PyTypeObject *)class); obj->extvartype = ei; + return (PyObject *)obj; } *************** *** 1512,1515 **** --- 1632,1637 ---- currentCookie = time(NULL); + + releasepythonlock(); } return true; *************** *** 1520,1523 **** --- 1642,1646 ---- return false; + getpythonlock(); if (Py_IsInitialized()) { disposeGlobals(); *************** *** 1600,1604 **** pythonconvertparameters(hdltreenode hparams) { register hdltreenode nomad = hparams; ! PyObject *argList = PyList_New(0); while (nomad != nil) { --- 1723,1732 ---- pythonconvertparameters(hdltreenode hparams) { register hdltreenode nomad = hparams; ! PyObject *argList; ! PyObject *tuple; ! ! getpythonlock(); ! ! argList = PyList_New(0); while (nomad != nil) { *************** *** 1616,1623 **** PyList_Append(argList, arg); } ! return PyList_AsTuple(argList); } boolean callpythonscript(tyvaluerecord code, hdltreenode hparam1, bigstring bsname, tyvaluerecord *vreturned, boolean runbutton) { PyObject *globals; --- 1744,1757 ---- PyList_Append(argList, arg); } + + tuple = PyList_AsTuple(argList); + + releasepythonlock(); ! return tuple; } + char *defArgv[] = {""}; + boolean callpythonscript(tyvaluerecord code, hdltreenode hparam1, bigstring bsname, tyvaluerecord *vreturned, boolean runbutton) { PyObject *globals; *************** *** 1627,1630 **** --- 1761,1766 ---- startInterpreter(); + getpythonlock(); + globals = PyDict_New(); *************** *** 1639,1644 **** // htext = code.data.stringvalue; ! if (!enlargehandle(htext, gethandlesize(htext) + 1, "\0")) return false; // lock the text --- 1775,1782 ---- // htext = code.data.stringvalue; ! if (!enlargehandle(htext, gethandlesize(htext) + 1, "\0")) { ! releasepythonlock(); return false; + } // lock the text *************** *** 1647,1650 **** --- 1785,1789 ---- // run it convertreturns(htext); + PySys_SetArgv(1, defArgv); result = PyRun_String(*htext, Py_file_input, globals, globals); if (result) Py_DECREF(result); *************** *** 1657,1660 **** --- 1796,1800 ---- PyErr_Print(); PyErr_Clear(); + releasepythonlock(); return false; } *************** *** 1668,1673 **** // find the function that was called func = PyDict_GetItemString(globals, funcName); ! if (!func) return false; // convert the calling parameters --- 1808,1815 ---- // find the function that was called func = PyDict_GetItemString(globals, funcName); ! if (!func) { ! releasepythonlock(); return false; + } // convert the calling parameters *************** *** 1675,1678 **** --- 1817,1821 ---- // call it + PySys_SetArgv(1, defArgv); result = PyObject_CallObject(func, argList); *************** *** 1683,1686 **** --- 1826,1830 ---- PyErr_Print(); PyErr_Clear(); + releasepythonlock(); return false; } *************** *** 1689,1692 **** --- 1833,1837 ---- if (!pythontofrontier(result, vreturned)) { if (result) Py_DECREF(result); + releasepythonlock(); return false; } *************** *** 1694,1697 **** --- 1839,1843 ---- } + releasepythonlock(); return true; } |