00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "Python.h"
00018 #include <string.h>
00019 #include <sys/epoll.h>
00020
00021 #define MAX_EVENTS 24
00022
00023
00024
00025
00026 static PyObject* _epoll_create(void) {
00027 int fd = epoll_create(MAX_EVENTS);
00028 if (fd == -1) {
00029 PyErr_SetFromErrno(PyExc_Exception);
00030 return NULL;
00031 }
00032
00033 return PyInt_FromLong(fd);
00034 }
00035
00036
00037
00038
00039
00040
00041 static PyObject* _epoll_ctl(PyObject* self, PyObject* args) {
00042 int epfd, op, fd, events;
00043 struct epoll_event event;
00044
00045 if (!PyArg_ParseTuple(args, "iiiI", &epfd, &op, &fd, &events)) {
00046 return NULL;
00047 }
00048
00049 memset(&event, 0, sizeof(event));
00050 event.events = events;
00051 event.data.fd = fd;
00052 if (epoll_ctl(epfd, op, fd, &event) == -1) {
00053 PyErr_SetFromErrno(PyExc_OSError);
00054 return NULL;
00055 }
00056
00057 Py_INCREF(Py_None);
00058 return Py_None;
00059 }
00060
00061
00062
00063
00064
00065
00066 static PyObject* _epoll_wait(PyObject* self, PyObject* args) {
00067 struct epoll_event events[MAX_EVENTS];
00068 int epfd, timeout, num_events, i;
00069 PyObject* list;
00070 PyObject* tuple;
00071
00072 if (!PyArg_ParseTuple(args, "ii", &epfd, &timeout)) {
00073 return NULL;
00074 }
00075
00076 Py_BEGIN_ALLOW_THREADS
00077 num_events = epoll_wait(epfd, events, MAX_EVENTS, timeout);
00078 Py_END_ALLOW_THREADS
00079 if (num_events == -1) {
00080 PyErr_SetFromErrno(PyExc_Exception);
00081 return NULL;
00082 }
00083
00084 list = PyList_New(num_events);
00085 for (i = 0; i < num_events; i++) {
00086 tuple = PyTuple_New(2);
00087 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(events[i].data.fd));
00088 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(events[i].events));
00089 PyList_SET_ITEM(list, i, tuple);
00090 }
00091 return list;
00092 }
00093
00094
00095
00096
00097 static PyMethodDef kEpollMethods[] = {
00098 {"epoll_create", (PyCFunction)_epoll_create, METH_NOARGS,
00099 "Create an epoll file descriptor"},
00100 {"epoll_ctl", _epoll_ctl, METH_VARARGS,
00101 "Control an epoll file descriptor"},
00102 {"epoll_wait", _epoll_wait, METH_VARARGS,
00103 "Wait for events on an epoll file descriptor"},
00104 {NULL, NULL, 0, NULL}
00105 };
00106
00107
00108
00109
00110 PyMODINIT_FUNC initepoll(void) {
00111 Py_InitModule("epoll", kEpollMethods);
00112 }