Skip to content

Commit

Permalink
Remove readv experimentation replaced by read+buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ask committed Nov 12, 2013
1 parent 8aad7b9 commit 9085742
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 225 deletions.
103 changes: 0 additions & 103 deletions Modules/_billiard/multiprocessing.c
Expand Up @@ -196,102 +196,6 @@ Billiard_multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
PyLong_FromVoidPtr(buffer), buffer_len);
}

/* readv() */

#if defined(HAVE_READV)

static Py_ssize_t
_Billiard_iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq,
int cnt, int type)
{
int i, j;
Py_ssize_t blen, total = 0;

*iov = PyMem_New(struct iovec, cnt);
if (*iov == NULL) {
PyErr_NoMemory();
return total;
}

*buf = PyMem_New(Py_buffer, cnt);
if (*buf == NULL) {
PyMem_Del(*iov);
PyErr_NoMemory();
return total;
}

for (i = 0; i < cnt; i++) {
PyObject *item = PySequence_GetItem(seq, i);
if (item == NULL)
goto fail;
if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
Py_DECREF(item);
goto fail;
}
Py_DECREF(item);
(*iov)[i].iov_base = (*buf)[i].buf;
blen = (*buf)[i].len;
(*iov)[i].iov_len = blen;
total += blen;
}
return total;

fail:
PyMem_Del(*iov);
for (j = 0; j < i; j++) {
PyBuffer_Release(&(*buf)[j]);
}
PyMem_Del(*buf);
return 0;
}

static void
_Billiard_iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
{
int i;
PyMem_Del(iov);
for (i = 0; i < cnt; i++) {
PyBuffer_Release(&buf[i]);
}
PyMem_Del(buf);
}


static PyObject*
Billiard_readv(PyObject *self, PyObject *args)
{
int fd, cnt;
Py_ssize_t n;
PyObject *seq;
struct iovec *iov;
Py_buffer *buf;

if (!PyArg_ParseTuple(args, "iO", &fd, &seq))
return NULL;
if (!PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError,
"readv() arg 2 must be a sequence");
return NULL;
}
cnt = PySequence_Size(seq);

if (!_Billiard_iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
return NULL;

Py_BEGIN_ALLOW_THREADS
n = readv(fd, iov, cnt);
Py_END_ALLOW_THREADS

_Billiard_iov_cleanup(iov, buf, cnt);
if (n < 0)
return PyErr_SetFromErrno(PyExc_OSError);
return PyLong_FromSsize_t(n);
}


#endif /* HAVE_READV */


#if !defined(MS_WINDOWS)

static PyObject *
Expand Down Expand Up @@ -373,13 +277,6 @@ static PyMethodDef Billiard_module_methods[] = {
{"read", Billiard_read, METH_VARARGS,
"read(fd, buffer) -> bytes\n\n"
"Read from file descriptor into buffer."},
#endif
#if HAVE_READV
{"readv", Billiard_readv, METH_VARARGS,
"readv(fd, buffers) -> bytesread\n\n"
"Read from a file descriptor into a number of writable buffers.\n"
"buffers is an arbitrary sequence of writable buffers.\n\n"
"Returns the total number of bytes read."},
#endif
{NULL}
};
Expand Down
95 changes: 0 additions & 95 deletions Modules/_billiard/socket_connection.c
Expand Up @@ -18,101 +18,6 @@
# define CLOSE(h) close(h)
#endif

/* readv() */

#if (defined (HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
|| defined(__APPLE__)) || defined(HAVE_READY) | defined(HAVE_WRITEV))

static Py_ssize_t
_Billiard_iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq,
int cnt, int type)
{
int i, j;
Py_ssize_t blen, total = 0;

*iov = PyMem_New(struct iovec, cnt);
if (*iov == NULL) {
PyErr_NoMemory();
return total;
}

*buf = PyMem_New(Py_buffer, cnt);
if (*buf == NULL) {
PyMem_Del(*iov);
PyErr_NoMemory();
return total;
}

for (i = 0; i < cnt; i++) {
PyObject *item = PySequence_GetItem(seq, i);
if (item == NULL)
goto fail;
if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
Py_DECREF(item);
goto fail;
}
Py_DECREF(item);
(*iov)[i].iov_base = (*buf)[i].buf;
blen = (*buf)[i].len;
(*iov)[i].iov_len = blen;
total += blen;
}
return total;

fail:
PyMem_Del(*iov);
for (j = 0; j < i; j++) {
PyBuffer_Release(&(*buf)[j]);
}
PyMem_Del(*buf);
return 0;
}

static void
_Billiard_iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
{
int i;
PyMem_Del(iov);
for (i = 0; i < cnt; i++) {
PyBuffer_Release(&buf[i]);
}
PyMem_Del(buf);
}


static PyObject*
_Billiard_readv(PyObject *args)
{
int fd, cnt;
Py_ssize_t n;
PyObject *seq;
struct iovec *iov;
Py_buffer *buf;

if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
return NULL;
if (!PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError,
"readv() arg 2 must be a sequence");
return NULL;
}
cnt = PySequence_Size(seq);

if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
return NULL;

Py_BEGIN_ALLOW_THREADS;
n = readv(fd, iov, cnt);
Py_END_ALLOW_THREADS;

iov_cleanup(iov, buf, cnt);
return PyLong_FromSsize_t(n);
}


# endif // HAVE_READV


/*
* Send string to file descriptor
*/
Expand Down
50 changes: 23 additions & 27 deletions setup.py
Expand Up @@ -95,7 +95,6 @@ def add_doc(m):
libraries = ['ws2_32']
elif sys.platform.startswith('darwin'): # Mac OSX
macros = dict(
HAVE_READV=1,
HAVE_SEM_OPEN=1,
HAVE_SEM_TIMEDWAIT=0,
HAVE_FD_TRANSFER=1,
Expand All @@ -107,7 +106,6 @@ def add_doc(m):
HAVE_SEM_OPEN=1,
HAVE_SEM_TIMEDWAIT=1,
HAVE_FD_TRANSFER=0,
HAVE_READV=0,
HAVE_BROKEN_SEM_UNLINK=1
)
libraries = []
Expand All @@ -118,48 +116,46 @@ def add_doc(m):
HAVE_SEM_OPEN=0,
HAVE_SEM_TIMEDWAIT=0,
HAVE_FD_TRANSFER=1,
HAVE_READV=0,
)
libraries = []
elif sys.platform in ('freebsd7', 'freebsd8', 'freebsd9', 'freebsd10'):
macros = dict( # FreeBSD 7+
HAVE_SEM_OPEN=bool(sysconfig.get_config_var('HAVE_SEM_OPEN') and not bool(sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED'))),
HAVE_SEM_TIMEDWAIT=1,
HAVE_FD_TRANSFER=1,
HAVE_READV=1,
)
HAVE_SEM_OPEN=bool(
sysconfig.get_config_var('HAVE_SEM_OPEN') and not
bool(sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED'))
),
HAVE_SEM_TIMEDWAIT=1,
HAVE_FD_TRANSFER=1,
)
libraries = []
elif sys.platform.startswith('openbsd'):
macros = dict( # OpenBSD
HAVE_SEM_OPEN=0, # Not implemented
HAVE_SEM_TIMEDWAIT=0,
HAVE_FD_TRANSFER=1,
)
)
libraries = []
else: # Linux and other unices
macros = dict(
HAVE_SEM_OPEN=1,
HAVE_SEM_TIMEDWAIT=1,
HAVE_FD_TRANSFER=1,
HAVE_READV=1,
)
libraries = ['rt']

if 'linux' in sys.platform.lower():
macros['HAVE_READV'] = 1


if sys.platform == 'win32':
multiprocessing_srcs = ['Modules/_billiard/multiprocessing.c',
'Modules/_billiard/semaphore.c',
'Modules/_billiard/pipe_connection.c',
'Modules/_billiard/socket_connection.c',
'Modules/_billiard/win32_functions.c'
]
multiprocessing_srcs = [
'Modules/_billiard/multiprocessing.c',
'Modules/_billiard/semaphore.c',
'Modules/_billiard/pipe_connection.c',
'Modules/_billiard/socket_connection.c',
'Modules/_billiard/win32_functions.c',
]
else:
multiprocessing_srcs = ['Modules/_billiard/multiprocessing.c',
'Modules/_billiard/socket_connection.c'
]
multiprocessing_srcs = [
'Modules/_billiard/multiprocessing.c',
'Modules/_billiard/socket_connection.c',
]

if macros.get('HAVE_SEM_OPEN', False):
multiprocessing_srcs.append('Modules/_billiard/semaphore.c')
Expand Down Expand Up @@ -199,14 +195,14 @@ def run_setup(with_extensions=True):
extensions = []
if with_extensions:
extensions = [
Extension('_billiard',
Extension(
'_billiard',
sources=multiprocessing_srcs,
define_macros=macros.items(),
libraries=libraries,
include_dirs=['Modules/_billiard'],
depends=(glob.glob('Modules/_billiard/*.h') +
['setup.py'])
),
depends=glob.glob('Modules/_billiard/*.h') + ['setup.py'],
),
]
setup(
name='billiard',
Expand Down

0 comments on commit 9085742

Please sign in to comment.