diff --git a/core/sharedarea.c b/core/sharedarea.c index 765a6026..aca579e4 100644 --- a/core/sharedarea.c +++ b/core/sharedarea.c @@ -30,6 +30,7 @@ int uwsgi_sharedarea_read(int id, uint64_t pos, char *blob, uint64_t len) { struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos); if (!sa) return -1; if (pos + len > sa->max_pos + 1) return -1; + if (len == 0) len = (sa->max_pos + 1) - pos; uwsgi_rlock(sa->lock); memcpy(blob, sa->area + pos, len); sa->hits++; @@ -226,11 +227,13 @@ struct uwsgi_sharedarea *uwsgi_sharedarea_init_keyval(int id, char *arg) { char *s_pages = NULL; char *s_file = NULL; char *s_fd = NULL; + char *s_ptr = NULL; char *s_size = NULL; if (uwsgi_kvlist_parse(arg, strlen(arg), ',', '=', "pages", &s_pages, "file", &s_file, "fd", &s_fd, + "ptr", &s_ptr, "size", &s_size, NULL)) { } diff --git a/plugins/psgi/uwsgi_plmodule.c b/plugins/psgi/uwsgi_plmodule.c index 3b2ec978..6835484c 100644 --- a/plugins/psgi/uwsgi_plmodule.c +++ b/plugins/psgi/uwsgi_plmodule.c @@ -711,7 +711,7 @@ XS(XS_sharedarea_read) { dXSARGS; int id; uint64_t pos; - uint64_t len = 1; + uint64_t len = 0; psgi_check_args(2); id = SvIV(ST(0)); @@ -733,6 +733,27 @@ XS(XS_sharedarea_read) { XSRETURN(1); } +XS(XS_sharedarea_write) { + dXSARGS; + int id; + uint64_t pos; + STRLEN vallen; + + psgi_check_args(3); + + id = SvIV(ST(0)); + pos = SvIV(ST(1)); + char *value = SvPV(ST(2), vallen); + + if (uwsgi_sharedarea_write(id, pos, value, vallen)) { + croak("unable to write to sharedarea %d", id); + XSRETURN_UNDEF; + } + + XSRETURN_YES; +} + + XS(XS_chunked_read) { dXSARGS; int timeout = 0; @@ -825,6 +846,7 @@ void init_perl_embedded_module() { psgi_xs(chunked_read_nb); psgi_xs(sharedarea_read); + psgi_xs(sharedarea_write); psgi_xs(sharedarea_wait); } diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index f3be7ea4..8fd735a6 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -1447,103 +1447,160 @@ PyObject *py_uwsgi_extract(PyObject * self, PyObject * args) { } -PyObject *py_uwsgi_sharedarea_inclong(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_sharedarea_inc64(PyObject * self, PyObject * args) { + int id; uint64_t pos = 0; - uint64_t value = 1; + int64_t value = 1; - if (!PyArg_ParseTuple(args, "l|l:sharedarea_inclong", &pos, &value)) { + if (!PyArg_ParseTuple(args, "il|l:sharedarea_inc64", &id, &pos, &value)) { return NULL; } - return PyInt_FromLong(value); + UWSGI_RELEASE_GIL + int ret = uwsgi_sharedarea_inc64(id, pos, value); + UWSGI_GET_GIL + + if (ret) { + return PyErr_Format(PyExc_ValueError, "error calling uwsgi_sharedarea_inc64()"); + } + + Py_INCREF(Py_None); + return Py_None; } -PyObject *py_uwsgi_sharedarea_writelong(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_sharedarea_write64(PyObject * self, PyObject * args) { + int id; uint64_t pos = 0; - uint64_t value = 0; + int64_t value = 0; - if (!PyArg_ParseTuple(args, "ll:sharedarea_writelong", &pos, &value)) { + if (!PyArg_ParseTuple(args, "ill:sharedarea_write64", &id, &pos, &value)) { return NULL; } - return PyInt_FromLong(value); + UWSGI_RELEASE_GIL + int ret = uwsgi_sharedarea_write64(id, pos, &value); + UWSGI_GET_GIL + if (ret) { + return PyErr_Format(PyExc_ValueError, "error calling uwsgi_sharedarea_write64()"); + } + + Py_INCREF(Py_None); + return Py_None; } PyObject *py_uwsgi_sharedarea_write(PyObject * self, PyObject * args) { + int id; uint64_t pos = 0; char *value; Py_ssize_t value_len = 0; - if (!PyArg_ParseTuple(args, "ls#:sharedarea_write", &pos, &value, &value_len)) { + if (!PyArg_ParseTuple(args, "ils#:sharedarea_write", &id, &pos, &value, &value_len)) { return NULL; } - return PyInt_FromLong(value_len); - + UWSGI_RELEASE_GIL + int ret = uwsgi_sharedarea_write(id, pos, value, value_len); + UWSGI_GET_GIL + + if (ret) { + return PyErr_Format(PyExc_ValueError, "error calling uwsgi_sharedarea_write()"); + } + + Py_INCREF(Py_None); + return Py_None; +} + +PyObject *py_uwsgi_sharedarea_write8(PyObject * self, PyObject * args) { + int id; + uint64_t pos = 0; + int8_t value; + + if (!PyArg_ParseTuple(args, "ilb:sharedarea_write8", &id, &pos, &value)) { + return NULL; + } + + UWSGI_RELEASE_GIL + int ret = uwsgi_sharedarea_write8(id, pos, &value); + UWSGI_GET_GIL + + if (ret) { + return PyErr_Format(PyExc_ValueError, "error calling uwsgi_sharedarea_write8()"); + } + + Py_INCREF(Py_None); + return Py_None; } -PyObject *py_uwsgi_sharedarea_writebyte(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_sharedarea_read64(PyObject * self, PyObject * args) { + int id; uint64_t pos = 0; - char value; + int64_t value; - if (!PyArg_ParseTuple(args, "lb:sharedarea_writebyte", &pos, &value)) { + if (!PyArg_ParseTuple(args, "il:sharedarea_read64", &id, &pos)) { return NULL; } - return PyInt_FromLong(value); + UWSGI_RELEASE_GIL + int ret = uwsgi_sharedarea_read64(id, pos, &value); + UWSGI_GET_GIL -} - -PyObject *py_uwsgi_sharedarea_readlong(PyObject * self, PyObject * args) { - uint64_t pos = 0; - uint64_t value; - - if (!PyArg_ParseTuple(args, "l:sharedarea_readlong", &pos)) { - return NULL; - } - - value = 1; + if (ret) { + return PyErr_Format(PyExc_ValueError, "error calling uwsgi_sharedarea_read64()"); + } return PyLong_FromLong(value); } - -PyObject *py_uwsgi_sharedarea_readbyte(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_sharedarea_read8(PyObject * self, PyObject * args) { + int id; uint64_t pos = 0; - uint8_t byte; + int8_t byte; - if (!PyArg_ParseTuple(args, "l:sharedarea_readbyte", &pos)) { + if (!PyArg_ParseTuple(args, "il:sharedarea_read8", &id, &pos)) { return NULL; } - byte = 1; + UWSGI_RELEASE_GIL + int ret = uwsgi_sharedarea_read8(id, pos, &byte); + UWSGI_GET_GIL + + if (ret) { + return PyErr_Format(PyExc_ValueError, "error calling uwsgi_sharedarea_read8()"); + } return PyInt_FromLong(byte); - } PyObject *py_uwsgi_sharedarea_read(PyObject * self, PyObject * args) { + int id; uint64_t pos = 0; - uint64_t len = 1; + uint64_t len = 0; - if (!PyArg_ParseTuple(args, "l|l:sharedarea_read", &pos, &len)) { + if (!PyArg_ParseTuple(args, "il|l:sharedarea_read", &id, &pos, &len)) { return NULL; } -/* PyObject *ret = PyString_FromStringAndSize(NULL, len); #ifdef PYTHREE char *storage = PyBytes_AsString(ret); #else char *storage = PyString_AS_STRING(ret); #endif -*/ - return NULL; + UWSGI_RELEASE_GIL + int r = uwsgi_sharedarea_read(id, pos, storage, len); + UWSGI_GET_GIL + + if (r) { + Py_DECREF(ret); + return PyErr_Format(PyExc_ValueError, "error calling uwsgi_sharedarea_read()"); + } + + return ret; } PyObject *py_uwsgi_spooler_freq(PyObject * self, PyObject * args) { @@ -2420,11 +2477,16 @@ static PyMethodDef uwsgi_advanced_methods[] = { static PyMethodDef uwsgi_sa_methods[] = { {"sharedarea_read", py_uwsgi_sharedarea_read, METH_VARARGS, ""}, {"sharedarea_write", py_uwsgi_sharedarea_write, METH_VARARGS, ""}, - {"sharedarea_readbyte", py_uwsgi_sharedarea_readbyte, METH_VARARGS, ""}, - {"sharedarea_writebyte", py_uwsgi_sharedarea_writebyte, METH_VARARGS, ""}, - {"sharedarea_readlong", py_uwsgi_sharedarea_readlong, METH_VARARGS, ""}, - {"sharedarea_writelong", py_uwsgi_sharedarea_writelong, METH_VARARGS, ""}, - {"sharedarea_inclong", py_uwsgi_sharedarea_inclong, METH_VARARGS, ""}, + {"sharedarea_readbyte", py_uwsgi_sharedarea_read8, METH_VARARGS, ""}, + {"sharedarea_writebyte", py_uwsgi_sharedarea_write8, METH_VARARGS, ""}, + {"sharedarea_read8", py_uwsgi_sharedarea_read8, METH_VARARGS, ""}, + {"sharedarea_write8", py_uwsgi_sharedarea_write8, METH_VARARGS, ""}, + {"sharedarea_readlong", py_uwsgi_sharedarea_read64, METH_VARARGS, ""}, + {"sharedarea_writelong", py_uwsgi_sharedarea_write64, METH_VARARGS, ""}, + {"sharedarea_read64", py_uwsgi_sharedarea_read64, METH_VARARGS, ""}, + {"sharedarea_write64", py_uwsgi_sharedarea_write64, METH_VARARGS, ""}, + {"sharedarea_inclong", py_uwsgi_sharedarea_inc64, METH_VARARGS, ""}, + {"sharedarea_inc64", py_uwsgi_sharedarea_inc64, METH_VARARGS, ""}, {NULL, NULL}, };