Created
April 21, 2011 11:51
-
-
Save peo3/934279 to your computer and use it in GitHub Desktop.
An extension module to call eventfd(2) in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Usage in python: | |
* | |
* import linux | |
* efd = linux.eventfd(0, 0) | |
* ... | |
* ret = struct.unpack('Q', os.read(efd, 8)) | |
* ... | |
* linux.close(efd) | |
*/ | |
#include <Python.h> | |
#include <unistd.h> | |
#include <sys/eventfd.h> | |
static PyObject * | |
linux_eventfd(PyObject *self, PyObject *args) | |
{ | |
int efd, initval, flags; | |
if (!PyArg_ParseTuple(args, "ii", &initval, &flags)) | |
return NULL; | |
efd = eventfd(initval, flags); | |
if (efd == -1) { | |
return PyErr_SetFromErrno(PyExc_OSError); | |
} else { | |
return Py_BuildValue("i", efd); | |
} | |
} | |
static PyObject * | |
linux_close(PyObject *self, PyObject *args) | |
{ | |
int ret, fd; | |
if (!PyArg_ParseTuple(args, "i", &fd)) | |
return NULL; | |
ret = close(fd); | |
if (ret == -1) { | |
return PyErr_SetFromErrno(PyExc_OSError); | |
} else { | |
return Py_BuildValue("i", ret); | |
} | |
} | |
static PyMethodDef LinuxSyscalls[] = { | |
{"eventfd", linux_eventfd, METH_VARARGS, | |
"Execute eventfd syscall."}, | |
{"close", linux_close, METH_VARARGS, | |
"Execute close syscall."}, | |
{NULL, NULL, 0, NULL} | |
}; | |
PyMODINIT_FUNC | |
initlinux(void) | |
{ | |
(void) Py_InitModule("linux", LinuxSyscalls); | |
} | |
/* | |
int | |
main(int argc, char *argv[]) | |
{ | |
Py_SetProgramName(argv[0]); | |
Py_Initialize(); | |
initlinux(); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment