static void t_bootstrap(void *boot_raw) { struct bootstate *boot = (struct bootstate *) boot_raw; PyThreadState *tstate; PyObject *res; tstate = PyThreadState_New(boot->interp); PyEval_AcquireThread(tstate); res = PyEval_CallObjectWithKeywords( boot->func, boot->args, boot->keyw); Py_DECREF(boot->func); Py_DECREF(boot->args); Py_XDECREF(boot->keyw); PyMem_DEL(boot_raw); if (res == NULL) { if (PyErr_ExceptionMatches(PyExc_SystemExit)) PyErr_Clear(); else { PySys_WriteStderr("Unhandled exception in thread:\n"); PyErr_PrintEx(0); } } else Py_DECREF(res); PyThreadState_Clear(tstate); PyThreadState_DeleteCurrent(); PyThread_exit_thread(); }
static void bpo20891_thread(void *lockp) { PyThread_type_lock lock = *((PyThread_type_lock*)lockp); PyGILState_STATE state = PyGILState_Ensure(); if (!PyGILState_Check()) { fprintf(stderr, "PyGILState_Check failed!"); abort(); } PyGILState_Release(state); PyThread_release_lock(lock); PyThread_exit_thread(); }
static void t_bootstrap(void *boot_raw) { struct bootstate *boot = (struct bootstate *) boot_raw; PyThreadState *tstate; PyObject *res; tstate = boot->tstate; tstate->thread_id = PyThread_get_thread_ident(); _PyThreadState_Init(tstate); PyEval_AcquireThread(tstate); res = PyEval_CallObjectWithKeywords( boot->func, boot->args, boot->keyw); if (res == NULL) { if (PyErr_ExceptionMatches(PyExc_SystemExit)) PyErr_Clear(); else { PyObject *file; PySys_WriteStderr( "Unhandled exception in thread started by "); file = PySys_GetObject("stderr"); if (file != NULL && file != Py_None) PyFile_WriteObject(boot->func, file, 0); else PyObject_Print(boot->func, stderr, 0); PySys_WriteStderr("\n"); PyErr_PrintEx(0); } } else Py_DECREF(res); Py_DECREF(boot->func); Py_DECREF(boot->args); Py_XDECREF(boot->keyw); PyMem_DEL(boot_raw); PyThreadState_Clear(tstate); PyThreadState_DeleteCurrent(); PyThread_exit_thread(); }
static void t_bootstrap(void* rawself) { // printf("Hello, iam Threadi :)\n"); PyObject * callback; PyThreadState *tstate; PyObject* args = NULL; char buf; tstate = PyThreadState_New(interp); PyEval_AcquireThread(tstate); int retval, i, fd_glow; PyObject *res = NULL; while (global_nfds > 0) { Py_BEGIN_ALLOW_THREADS retval = poll(global_pfd, global_nfds, 20); // timeout to check if cb still set Py_END_ALLOW_THREADS if (retval == 0) continue; // poll timed out if (retval < 0) { PyErr_SetFromErrno(PyExc_IOError); break; } // printf("Threadi: nfds: %i\n", global_nfds); for (i = 0; i < global_nfds; i++) { if (global_pfd[i].revents != 0) { // fd_glow = global_pfd[i].fd; lseek(global_pfd[i].fd, 0, SEEK_SET); read(global_pfd[i].fd, &buf, 1 ); res = NULL; args = Py_BuildValue("(i)", buf - 48); // we got ASCII '0' callback = get_poll_cb(global_pfd[i].fd)->callback; // printf("Threadi: fd: %i, revents: %i\n", global_pfd[i].fd, global_pfd[i].revents); PyGILState_STATE s = PyGILState_Ensure(); res = PyObject_CallObject(callback, args); PyGILState_Release(s); // printf("Threadi :5\n"); Py_DECREF(args); if (res == NULL) { Py_XDECREF(res); break; /* Pass error back */ } /* Here maybe use res maybe exit loop at some defined return value (i.e. res == -1) */ Py_DECREF(res); } } } if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_SystemExit)) PyErr_Clear(); else { PySys_WriteStderr("Unhandled exception in poll thread:\n"); PyErr_PrintEx(0); } } PyThread_release_lock(lock); // printf("terminating poll thread\n"); // PyEval_ReleaseThread(tstate); lock = NULL; PyThreadState_Clear(tstate); PyThreadState_DeleteCurrent(); PyThread_exit_thread(); }