From a6e6bff08253e3e56a5aaab7929dfc1bdb63317d Mon Sep 17 00:00:00 2001 From: Unbit Date: Tue, 10 Dec 2013 10:09:38 +0100 Subject: [PATCH] sharedarea can be mapped to generic objects --- core/sharedarea.c | 40 ++++++++++++++++++++++++++++++++- plugins/python/python_plugin.c | 15 +++++++++++++ plugins/python/uwsgi_pymodule.c | 13 +++++++++++ plugins/python/uwsgi_python.h | 2 ++ uwsgi.h | 1 + 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/core/sharedarea.c b/core/sharedarea.c index cba0d6f6..59835633 100644 --- a/core/sharedarea.c +++ b/core/sharedarea.c @@ -308,9 +308,47 @@ struct uwsgi_sharedarea *uwsgi_sharedarea_init_keyval(char *arg) { "ptr", &s_ptr, "size", &s_size, NULL)) { + uwsgi_log("invalid sharedarea keyval syntax\n"); + exit(1); } - return NULL; + uint64_t len = 0; + int pages = 0; + if (s_size) { + len = uwsgi_n64(s_size); + pages = len / uwsgi.page_size; + if (len % uwsgi.page_size != 0) pages++; + } + + if (s_pages) { + pages = atoi(s_pages); + } + + char *area = NULL; + struct uwsgi_sharedarea *sa = NULL; + if (s_file) { + } + else if (s_fd) { + } + else if (s_ptr) { + } + + if (pages) { + if (!area) { + sa = uwsgi_sharedarea_init(pages); + } + else { + uwsgi_sharedarea_init_ptr(area, len); + } + } + + if (s_pages) free(s_pages); + if (s_file) free(s_file); + if (s_fd) free(s_fd); + if (s_ptr) free(s_ptr); + if (s_size) free(s_size); + + return sa; } diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index b88c8295..934699dd 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -164,6 +164,10 @@ struct uwsgi_option uwsgi_python_options[] = { {"python-raw", required_argument, 0, "load a python file for managing raw requests", uwsgi_opt_set_str, &up.raw, 0}, +#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) + {"py-sharedarea", required_argument, 0, "create a sharedarea from a python bytearray object of the specified size", uwsgi_opt_add_string_list, &up.sharedarea, 0}, +#endif + {0, 0, 0, 0, 0, 0, 0}, }; @@ -263,6 +267,17 @@ pep405: up.swap_ts = simple_swap_ts; up.reset_ts = simple_reset_ts; +#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) + struct uwsgi_string_list *usl = NULL; + uwsgi_foreach(usl, up.sharedarea) { + uint64_t len = uwsgi_n64(usl->value); + PyObject *obj = PyByteArray_FromStringAndSize(NULL, len); + char *storage = PyByteArray_AsString(obj); + Py_INCREF(obj); + struct uwsgi_sharedarea *sa = uwsgi_sharedarea_init_ptr(storage, len); + sa->obj = obj; + } +#endif uwsgi_log_initial("Python main interpreter initialized at %p\n", up.main_thread); diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index f07941b3..cd561ef4 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -1708,6 +1708,18 @@ PyObject *py_uwsgi_sharedarea_memoryview(PyObject * self, PyObject * args) { return PyErr_Format(PyExc_ValueError, "cannot get a memoryview object from sharedarea %d", id); return PyMemoryView_FromBuffer(&info); } + +PyObject *py_uwsgi_sharedarea_object(PyObject * self, PyObject * args) { + int id; + if (!PyArg_ParseTuple(args, "i:sharedarea_object", &id)) { + return NULL; + } + struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, 0); + if (!sa) { + return PyErr_Format(PyExc_ValueError, "cannot get an object from sharedarea %d", id); + } + return (PyObject *) sa->obj; +} #endif PyObject *py_uwsgi_spooler_freq(PyObject * self, PyObject * args) { @@ -2599,6 +2611,7 @@ static PyMethodDef uwsgi_sa_methods[] = { {"sharedarea_unlock", py_uwsgi_sharedarea_unlock, METH_VARARGS, ""}, #if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) {"sharedarea_memoryview", py_uwsgi_sharedarea_memoryview, METH_VARARGS, ""}, + {"sharedarea_object", py_uwsgi_sharedarea_object, METH_VARARGS, ""}, #endif {NULL, NULL}, }; diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index ce4a54ba..edb6221c 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -191,6 +191,8 @@ struct uwsgi_python { char *raw; PyObject *raw_callable; + + struct uwsgi_string_list *sharedarea; }; diff --git a/uwsgi.h b/uwsgi.h index 00ff2a4b..94af72b9 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -722,6 +722,7 @@ struct uwsgi_sharedarea { uint64_t hits; uint8_t honour_used; uint64_t used; + void *obj; }; // maintain alignment here !!!