From 773d853bb41679af1a2d5dd1737e833ab442de59 Mon Sep 17 00:00:00 2001 From: Unbit Date: Sun, 3 Mar 2013 13:28:06 +0100 Subject: [PATCH] the RPC subsystem is no more dependent on cow --- core/mule.c | 1 + core/rpc.c | 29 +++++++++---------- plugins/jvm/jvm.h | 1 + plugins/jvm/jvm_plugin.c | 51 +++++++++++++++++---------------- plugins/python/uwsgi_pymodule.c | 9 +++--- uwsgi.h | 2 +- 6 files changed, 49 insertions(+), 44 deletions(-) diff --git a/core/mule.c b/core/mule.c index 9cc1e31e..e736d9cd 100644 --- a/core/mule.c +++ b/core/mule.c @@ -56,6 +56,7 @@ void uwsgi_mule(int id) { // avoid race conditions uwsgi.mules[id - 1].id = id; uwsgi.mules[id - 1].pid = getpid(); + uwsgi.mypid = uwsgi.mules[id - 1].pid; uwsgi_fixup_fds(0, id, NULL); diff --git a/core/rpc.c b/core/rpc.c index 6aaa5070..15a129bb 100644 --- a/core/rpc.c +++ b/core/rpc.c @@ -7,26 +7,23 @@ int uwsgi_register_rpc(char *name, uint8_t modifier1, uint8_t args, void *func) struct uwsgi_rpc *urpc; int ret = -1; - if (uwsgi.mywid != 0) { - uwsgi_log("you can register RPC functions only in the master\n"); + if (uwsgi.mywid == 0 && uwsgi.workers[0].pid != uwsgi.mypid) { + uwsgi_log("only the master and the workers can register RPC functions\n"); return -1; } uwsgi_lock(uwsgi.rpc_table_lock); - if (uwsgi.shared->rpc_count < uwsgi.rpc_max) { - uwsgi_log("rpc_max = %d %d\n", uwsgi.rpc_max, uwsgi.shared->rpc_count); - urpc = &uwsgi.rpc_table[uwsgi.shared->rpc_count]; - uwsgi_log("rpc_max = %d\n", uwsgi.rpc_max); + if (uwsgi.shared->rpc_count[uwsgi.mywid] < uwsgi.rpc_max) { + int pos = (uwsgi.mywid * uwsgi.rpc_max) + uwsgi.shared->rpc_count[uwsgi.mywid]; + urpc = &uwsgi.rpc_table[pos]; - uwsgi_log("NAME = %s %d %p\n", name, strlen(name), urpc->name); memcpy(urpc->name, name, strlen(name)); - uwsgi_log("NAME = %s\n", name); urpc->modifier1 = modifier1; urpc->args = args; urpc->func = func; - uwsgi.shared->rpc_count++; + uwsgi.shared->rpc_count[uwsgi.mywid]++; ret = 0; uwsgi_log("registered RPC function %s\n", name); @@ -43,10 +40,12 @@ uint16_t uwsgi_rpc(char *name, uint8_t argc, char *argv[], uint16_t argvs[], cha uint64_t i; uint16_t ret = 0; - for (i = 0; i < uwsgi.shared->rpc_count; i++) { - if (uwsgi.rpc_table[i].name[0] != 0) { - if (!strcmp(uwsgi.rpc_table[i].name, name)) { - urpc = &uwsgi.rpc_table[i]; + int pos = (uwsgi.mywid * uwsgi.rpc_max); + + for (i = 0; i < uwsgi.shared->rpc_count[uwsgi.mywid]; i++) { + if (uwsgi.rpc_table[pos + i].name[0] != 0) { + if (!strcmp(uwsgi.rpc_table[pos + i].name, name)) { + urpc = &uwsgi.rpc_table[pos + i]; break; } } @@ -149,6 +148,6 @@ error: void uwsgi_rpc_init() { - uwsgi_log("ALLOCATE\n"); - uwsgi.rpc_table = uwsgi_calloc_shared(sizeof(struct uwsgi_rpc) * uwsgi.rpc_max); + uwsgi.rpc_table = uwsgi_calloc_shared((sizeof(struct uwsgi_rpc) * uwsgi.rpc_max) * (uwsgi.numproc+1)); + uwsgi.shared->rpc_count = uwsgi_calloc_shared(sizeof(uint64_t) * (uwsgi.numproc+1)); } diff --git a/plugins/jvm/jvm.h b/plugins/jvm/jvm.h index 4419e652..4a163fbd 100644 --- a/plugins/jvm/jvm.h +++ b/plugins/jvm/jvm.h @@ -5,6 +5,7 @@ struct uwsgi_jvm { JNIEnv *env; + JavaVMInitArgs vm_args; struct uwsgi_string_list *classpath; struct uwsgi_string_list *classes; diff --git a/plugins/jvm/jvm_plugin.c b/plugins/jvm/jvm_plugin.c index 977ef6cd..8b62d9b6 100644 --- a/plugins/jvm/jvm_plugin.c +++ b/plugins/jvm/jvm_plugin.c @@ -137,34 +137,38 @@ void uwsgi_jvm_throw(char *message) { static int uwsgi_jvm_init(void) { + return 0; +} + +static void uwsgi_jvm_create(void) { + JavaVM *jvm; - JavaVMInitArgs vm_args; JavaVMOption options[1]; - vm_args.version = JNI_VERSION_1_2; + ujvm.vm_args.version = JNI_VERSION_1_2; - JNI_GetDefaultJavaVMInitArgs(&vm_args); + JNI_GetDefaultJavaVMInitArgs(&ujvm.vm_args); - options[0].optionString = "-Djava.class.path=."; + options[0].optionString = "-Djava.class.path=."; - char *old_cp = NULL ; - struct uwsgi_string_list *cp = ujvm.classpath; - while(cp) { - if (old_cp) { - options[0].optionString = uwsgi_concat3(old_cp, ":", cp->value); - free(old_cp); - } - else { - options[0].optionString = uwsgi_concat3(options[0].optionString, ":", cp->value); - } - old_cp = options[0].optionString ; - cp = cp->next; - } + char *old_cp = NULL ; + struct uwsgi_string_list *cp = ujvm.classpath; + while(cp) { + if (old_cp) { + options[0].optionString = uwsgi_concat3(old_cp, ":", cp->value); + free(old_cp); + } + else { + options[0].optionString = uwsgi_concat3(options[0].optionString, ":", cp->value); + } + old_cp = options[0].optionString ; + cp = cp->next; + } - vm_args.options = options; - vm_args.nOptions = 1; + ujvm.vm_args.options = options; + ujvm.vm_args.nOptions = 1; - if (JNI_CreateJavaVM(&jvm, (void **) &ujvm.env, &vm_args)) { + if (JNI_CreateJavaVM(&jvm, (void **) &ujvm.env, &ujvm.vm_args)) { uwsgi_log("unable to initialize the JVM\n"); exit(1); } @@ -228,9 +232,6 @@ static int uwsgi_jvm_init(void) { usl = usl->next; } - - return 1; - } // get the raw body of a java string @@ -273,7 +274,7 @@ static uint16_t uwsgi_jvm_rpc(void *func, uint8_t argc, char **argv, uint16_t ar char *b = uwsgi_jvm_str2c(ret); memcpy(buffer, b, rlen); uwsgi_jvm_release_chars(ret, b); - (*ujvm.env)->DeleteLocalRef(ujvm.env, ret); + (*ujvm.env)->DeleteLocalRef(ujvm.env, ret); return rlen; } end: @@ -290,6 +291,8 @@ struct uwsgi_plugin jvm_plugin = { .init = uwsgi_jvm_init, .options = uwsgi_jvm_options, + .post_fork = uwsgi_jvm_create, + .signal_handler = uwsgi_jvm_signal_handler, .rpc = uwsgi_jvm_rpc, }; diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 4c667f4f..c5c2a596 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -334,11 +334,12 @@ PyObject *py_uwsgi_call(PyObject * self, PyObject * args) { PyObject *py_uwsgi_rpc_list(PyObject * self, PyObject * args) { uint64_t i; - PyObject *rpc_list = PyTuple_New(uwsgi.shared->rpc_count); + PyObject *rpc_list = PyTuple_New(uwsgi.shared->rpc_count[uwsgi.mywid]); - for (i = 0; i < uwsgi.shared->rpc_count; i++) { - if (uwsgi.rpc_table[i].name[0] != 0) { - PyTuple_SetItem(rpc_list, i, PyString_FromString(uwsgi.rpc_table[i].name)); + int pos = (uwsgi.mywid * uwsgi.rpc_max); + for (i = 0; i < uwsgi.shared->rpc_count[uwsgi.mywid]; i++) { + if (uwsgi.rpc_table[pos + i].name[0] != 0) { + PyTuple_SetItem(rpc_list, i, PyString_FromString(uwsgi.rpc_table[pos + i].name)); } } diff --git a/uwsgi.h b/uwsgi.h index 40565672..87193e9b 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -2318,7 +2318,7 @@ struct uwsgi_rpc { struct uwsgi_signal_rb_timer rb_timers[MAX_TIMERS]; int rb_timers_cnt; - uint64_t rpc_count; + uint64_t *rpc_count; int worker_log_pipe[2]; // used for request logging