1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/* Define this to compile as a stand-alone "alarm" module */
/*#undef STANDALONE*/
#ifdef STANDALONE
# include <Python.h>
#else
# include "alarm.h"
#endif
/************************************************************/
#ifdef WITH_THREAD
/************************************************************/
#include <pythread.h>
typedef struct {
PyObject_HEAD
PyInterpreterState* interp;
PyThread_type_lock lock;
PyObject* args;
enum { st_waiting, st_running, st_stopped } state;
} PyAlarmObject;
staticforward PyTypeObject PyAlarm_Type;
static void t_bootstrap(void* rawself)
{
PyAlarmObject* self = (PyAlarmObject*) rawself;
PyThreadState *tstate;
PyObject* args = NULL;
tstate = PyThreadState_New(self->interp);
PyEval_AcquireThread(tstate);
while (1)
{
PyObject *res, *sleepfn, *sleeparg, *callback, *cbarg = NULL;
Py_XDECREF(args);
args = self->args;
Py_XINCREF(args);
if (args == NULL || args == Py_None)
break;
if (!PyArg_ParseTuple(args, "OOO|O", &sleepfn, &sleeparg,
&callback, &cbarg))
break;
res = PyObject_CallObject(sleepfn, sleeparg);
if (res == NULL)
break;
Py_DECREF(res);
res = NULL;
if (self->args == NULL)
break;
PyThread_acquire_lock(self->lock, WAIT_LOCK);
if (self->args != NULL)
{
self->state = st_running;
res = PyObject_CallObject(callback, cbarg);
self->state = st_waiting;
}
PyThread_release_lock(self->lock);
Py_DECREF(args);
args = self->args;
self->args = res;
}
Py_XDECREF(args);
Py_XDECREF(self->args);
self->args = NULL;
self->state = st_stopped;
if (PyErr_Occurred())
{
if (PyErr_ExceptionMatches(PyExc_SystemExit))
PyErr_Clear();
else
{
PySys_WriteStderr("Unhandled exception in alarm:\n");
PyErr_PrintEx(0);
}
}
Py_DECREF(self);
PyThreadState_Clear(tstate);
PyThreadState_DeleteCurrent();
PyThread_exit_thread();
}
/***************************************************************/
static void alarm_dealloc(PyAlarmObject* self)
{
Py_XDECREF(self->args);
if (self->lock != NULL)
PyThread_free_lock(self->lock);
PyObject_Del((PyObject*) self);
}
#if 0
static int alarm_traverse(PyAlarmObject* self, visitproc visit, void* arg)
{
if (self->args != NULL)
return visit(self->args, arg);
else
return 0;
}
#endif
PSY_INLINE int alarm_clear(PyAlarmObject* self)
{
PyObject* nargs = self->args;
self->args = NULL;
Py_XDECREF(nargs);
return 0;
}
static PyObject* alarmstate(PyAlarmObject* self, PyObject* args)
{
char* s;
if (!PyArg_ParseTuple(args, ""))
return NULL;
switch (self->state) {
case st_waiting: s = "waiting"; break;
case st_running: s = "running"; break;
default: s = "stopped"; break;
}
return PyString_FromString(s);
}
static PyObject* alarmstop(PyAlarmObject* self, PyObject* args)
{
int wait;
if (!PyArg_ParseTuple(args, "i", &wait))
return NULL;
alarm_clear(self);
if (wait && self->state != st_stopped)
{
Py_BEGIN_ALLOW_THREADS
PyThread_acquire_lock(self->lock, WAIT_LOCK);
PyThread_release_lock(self->lock);
Py_END_ALLOW_THREADS
}
Py_INCREF(Py_None);
return Py_None;
}
#ifdef STANDALONE
static PyObject* new_alarm(PyObject* dummy, PyObject* args)
#else
DEFINEFN
PyObject* psyco_new_alarm(PyObject* dummy, PyObject* args)
#endif
{
PyAlarmObject* self;
long ident;
self = PyObject_New(PyAlarmObject, &PyAlarm_Type);
if (self == NULL)
return NULL;
PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
self->interp = PyThreadState_Get()->interp;
self->lock = PyThread_allocate_lock();
Py_INCREF(args);
self->args = args;
self->state = st_waiting;
if (self->lock == NULL) goto error;
Py_INCREF(self); /* t_bootstrap consumes a ref */
ident = PyThread_start_new_thread(t_bootstrap, (void*) self);
if (ident == -1)
{
Py_DECREF(self);
PyErr_SetString(PyExc_RuntimeError, "can't start new thread");
goto error;
}
return (PyObject*) self;
error:
Py_DECREF(self);
return NULL;
}
/***************************************************************/
static PyMethodDef alarm_methods[] = {
{"state", (PyCFunction)alarmstate, METH_VARARGS},
{"stop", (PyCFunction)alarmstop, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
static PyObject* alarm_getattr(PyObject* self, char* name)
{
return Py_FindMethod(alarm_methods, self, name);
}
statichere PyTypeObject PyAlarm_Type = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"alarm", /*tp_name*/
sizeof(PyAlarmObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)alarm_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)alarm_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
};
#ifdef STANDALONE
static PyMethodDef AlarmMethods[] = {
{"alarm", new_alarm, METH_VARARGS},
{NULL, NULL} /* Sentinel */
};
void initalarm()
{
PyObject* m;
PyAlarm_Type.ob_type = &PyType_Type;
m = Py_InitModule("alarm", AlarmMethods);
if (m == NULL)
return;
Py_INCREF(&PyAlarm_Type);
if (PyModule_AddObject(m, "AlarmType", (PyObject*) &PyAlarm_Type))
return;
}
#else
INITIALIZATIONFN
void psyco_alarm_init(void)
{
PyAlarm_Type.ob_type = &PyType_Type;
}
#endif
/************************************************************/
#else /* if !defined(WITH_THREAD) */
/************************************************************/
#ifdef STANDALONE
# error "Thread support is required for the alarm module"
#else
DEFINEFN
PyObject* psyco_new_alarm(PyObject* dummy, PyObject* args)
{
PyErr_SetString(PyExc_PsycoError,
"Python and Psyco must be compiled with thread support.");
return NULL;
}
INITIALIZATIONFN
void psyco_alarm_init(void)
{
}
#endif
/************************************************************/
#endif /* WITH_THREAD */
/************************************************************/
|