-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy paththreaderPy3.c
More file actions
48 lines (43 loc) · 1.46 KB
/
threaderPy3.c
File metadata and controls
48 lines (43 loc) · 1.46 KB
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
#include <Python.h>
#include<pthread.h>
static PyObject* threader_killThread(PyObject*, PyObject*);
static PyObject* threader_enableCancel(PyObject*, PyObject*);
static PyObject*
threader_killThread(PyObject* self, PyObject* arg) {
long id = 0;
if (!PyArg_ParseTuple(arg, "l", &id))
return NULL;
int succeeded = pthread_cancel(id);
printf("Ended thread %ld, with return code %d\n ", id, succeeded);
return Py_BuildValue("l", succeeded);
}
static PyObject*
threader_enableCancel(PyObject* self, PyObject* arg) {
int succeeded = 0;
int a = 0;
succeeded = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &a);
printf("%d\n ", a);
succeeded = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &a);
printf("%d\n ", a);
printf("Set cancel status %d\n ", succeeded);
return Py_BuildValue("i", succeeded);
}
static PyMethodDef ThreaderMethods[] = {
{"killThread", threader_killThread, METH_VARARGS,
"Cancel a thread."},
{"enableCancel", threader_enableCancel, METH_VARARGS,
"Set cancel state to PTHREAD_CANCEL_ENABLE."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef threadermodule = {
PyModuleDef_HEAD_INIT,
"threader", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
ThreaderMethods
};
PyMODINIT_FUNC
PyInit_threader(void)
{
return PyModule_Create(&threadermodule);
}