From f6c01d11e9e67a4e3af737375993d470ee697b98 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Tue, 10 May 2011 08:50:06 +0200 Subject: [PATCH] completed ruby 1.9 fiber support --- fibers.ru | 5 +- plugins/fiber/fiber.c | 85 ++++++++++++++++++++ plugins/fiber/uwsgiplugin.py | 27 +++++++ plugins/rack/fiber.c | 146 ---------------------------------- plugins/rack/rack_plugin.c | 24 +++--- plugins/rack/uwsgi_rack.h | 2 + plugins/rack/uwsgiplugin.py | 1 - plugins/ruby19/uwsgiplugin.py | 1 - 8 files changed, 126 insertions(+), 165 deletions(-) create mode 100644 plugins/fiber/fiber.c create mode 100644 plugins/fiber/uwsgiplugin.py delete mode 100644 plugins/rack/fiber.c diff --git a/fibers.ru b/fibers.ru index 9a05928e..9e42a866 100644 --- a/fibers.ru +++ b/fibers.ru @@ -2,9 +2,10 @@ require 'fiber' class SuspendingBody def each - for i in 1..10 + for i in 1..100 yield "number: #{i}\n" - Fiber.yield + UWSGI.suspend + #Fiber.yield end end diff --git a/plugins/fiber/fiber.c b/plugins/fiber/fiber.c new file mode 100644 index 00000000..74537dcb --- /dev/null +++ b/plugins/fiber/fiber.c @@ -0,0 +1,85 @@ +#include "../rack/uwsgi_rack.h" + +extern struct uwsgi_server uwsgi; +extern struct uwsgi_rack ur; + +struct ufib { + VALUE *fib; +} ufiber; + +struct option fiber_options[] = { + { 0, 0, 0, 0 } +}; + +VALUE uwsgi_fiber_request() { + + uwsgi.wsgi_req->async_status = uwsgi.p[uwsgi.wsgi_req->uh.modifier1]->request(uwsgi.wsgi_req); + uwsgi.wsgi_req->suspended = 0; + + return Qnil; +} + +inline static void fiber_schedule_to_req() { + + int id = uwsgi.wsgi_req->async_id; + + if (!uwsgi.wsgi_req->suspended) { + ufiber.fib[id] = rb_fiber_new(uwsgi_fiber_request, Qnil); + rb_gc_register_address(&ufiber.fib[id]); + uwsgi.wsgi_req->suspended = 1; + } + + rb_fiber_resume(ufiber.fib[id], 0, NULL); + + if (uwsgi.wsgi_req->suspended) { + uwsgi.wsgi_req->async_status = UWSGI_AGAIN; + } + +} + +inline static void fiber_schedule_to_main(struct wsgi_request *wsgi_req) { + + rb_fiber_yield(0, NULL); + uwsgi.wsgi_req = wsgi_req; +} + +VALUE protected_async_loop() { + + async_loop(NULL); + return Qnil; +} + +void fiber_loop() { + + int error = 0; + + ufiber.fib = uwsgi_malloc( sizeof(VALUE) * uwsgi.async ); + + uwsgi.schedule_to_main = fiber_schedule_to_main; + uwsgi.schedule_to_req = fiber_schedule_to_req; + + // must run all the rack/ruby plugins without protection + ur.unprotected = 1; + + rb_protect(protected_async_loop, 0, &error); + + if (error) { + uwsgi_ruby_exception(); + exit(1); + } + + // never here +} + +int fiber_init() { + uwsgi_register_loop( (char *) "fiber", fiber_loop); + return 0; +} + + +struct uwsgi_plugin fiber_plugin = { + + .name = "fiber", + .init = fiber_init, + .options = fiber_options, +}; diff --git a/plugins/fiber/uwsgiplugin.py b/plugins/fiber/uwsgiplugin.py new file mode 100644 index 00000000..f66ab90e --- /dev/null +++ b/plugins/fiber/uwsgiplugin.py @@ -0,0 +1,27 @@ +import os + +try: + RUBYPATH = os.environ['UWSGICONFIG_RUBYPATH'] +except: + RUBYPATH = 'ruby' + +NAME='fiber' +CFLAGS = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['CFLAGS']\"").read().rstrip().split() + +CFLAGS.append('-Wno-unused-parameter') + +includedir = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['rubyhdrdir']\"").read().rstrip() +if includedir == 'nil': + includedir = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['archdir']\"").read().rstrip() + CFLAGS.append('-I' + includedir) +else: + CFLAGS.append('-I' + includedir) + archdir = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['archdir']\"").read().rstrip() + arch = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['arch']\"").read().rstrip() + CFLAGS.append('-I' + archdir) + CFLAGS.append('-I' + archdir + '/' + arch) + CFLAGS.append('-I' + includedir + '/' + arch) +LDFLAGS = [] +LIBS = [] + +GCC_LIST = ['fiber'] diff --git a/plugins/rack/fiber.c b/plugins/rack/fiber.c deleted file mode 100644 index 5ebac72c..00000000 --- a/plugins/rack/fiber.c +++ /dev/null @@ -1,146 +0,0 @@ -#include "uwsgi_rack.h" - -extern struct uwsgi_server uwsgi; -extern struct uwsgi_rack ur; - -VALUE fiber_list[200]; - -void uwsgi_ruby_exception(void); - -VALUE fiber_request() { - - struct wsgi_request *wsgi_req = uwsgi.wsgi_req; - int async_id = wsgi_req->async_id; - - - for(;;) { - wsgi_req_setup(wsgi_req, async_id); - - wsgi_req->async_status = UWSGI_ACCEPTING; - - rb_fiber_yield(0, NULL); - - if (wsgi_req_simple_accept(wsgi_req, uwsgi.sockets_poll[0].fd)) { - continue; - } - - wsgi_req->async_status = UWSGI_OK; - - rb_fiber_yield(0, NULL); - - // reinitialize switches counter - - wsgi_req->switches = 0; - if (wsgi_req_recv(wsgi_req)) { - continue; - } - - while(wsgi_req->async_status == UWSGI_AGAIN) { - rb_fiber_yield(0, NULL); - wsgi_req->async_status = uwsgi.p[wsgi_req->uh.modifier1]->request(wsgi_req); - } - - - - rb_fiber_yield(0, NULL); - - uwsgi_close_request(wsgi_req); - - } - - return Qnil; -} - - -VALUE protected_fiber_loop() { - - int i, current = 0; - - struct wsgi_request *wsgi_req; - - // create a ruby fiber for each async core - - uwsgi_log("create a fiber for each async core...\n"); - for(i=0;iasync_status = UWSGI_ACCEPTING; - uwsgi.wsgi_requests[i]->async_id = i; - uwsgi.wsgi_req = uwsgi.wsgi_requests[i]; - rb_fiber_resume(fiber_list[i], 0, NULL); - uwsgi_log("fiber %d ready\n", i); - } - - - for(;;) { - - //uwsgi.async_running = u_green_blocking(); - uwsgi.async_running = 0; - //timeout = u_green_get_timeout(); - int timeout = 0; - uwsgi.async_nevents = async_wait(uwsgi.async_queue, uwsgi.async_events, uwsgi.async, uwsgi.async_running, timeout); - //u_green_expire_timeouts(); - - if (uwsgi.async_nevents < 0) { - continue; - } - - - for(i=0; iasync_id); - uwsgi.wsgi_req = wsgi_req; - rb_fiber_resume(fiber_list[wsgi_req->async_id], 0, NULL); - } - else { - wsgi_req = find_wsgi_req_by_fd((int)uwsgi.async_events[i].ASYNC_FD, -1); - if (wsgi_req) { - uwsgi.wsgi_req = wsgi_req; - rb_fiber_resume(fiber_list[wsgi_req->async_id], 0, NULL); - } - else { - async_del(uwsgi.async_queue, (int) uwsgi.async_events[i].ASYNC_FD, uwsgi.async_events[i].ASYNC_EV); - } - } - - } - - -cycle: - - //uwsgi_log("resuming fiber %d %p\n", current, fiber_list[current]); - uwsgi.wsgi_req = uwsgi.wsgi_requests[current]; - if (uwsgi.wsgi_req->async_status != UWSGI_ACCEPTING) { - uwsgi_log("passing control to fiber %d\n", current); - rb_fiber_resume(fiber_list[current], 0, NULL); - uwsgi_log("returned to main fiber\n"); - } - current++; - if (current >= uwsgi.async) current = 0; - } - - - return Qnil; -} - -void fiber_loop() { - - int error; - - // must run all the rack/ruby plugins without protection - ur.unprotected = 1; - - rb_protect(protected_fiber_loop, 0, &error); - - if (error) { - uwsgi_ruby_exception(); - exit(1); - } - - // never here - -} diff --git a/plugins/rack/rack_plugin.c b/plugins/rack/rack_plugin.c index ad677719..264c8c6f 100644 --- a/plugins/rack/rack_plugin.c +++ b/plugins/rack/rack_plugin.c @@ -150,6 +150,8 @@ VALUE rb_uwsgi_io_gets(VALUE obj, VALUE args) { Data_Get_Struct(obj, struct wsgi_request, wsgi_req); // return the whole body as string + + uwsgi_log("rack.input gets is not implemented (req %p)\n", wsgi_req); return Qnil; } @@ -159,6 +161,7 @@ VALUE rb_uwsgi_io_each(VALUE obj, VALUE args) { Data_Get_Struct(obj, struct wsgi_request, wsgi_req); // yield strings chunks + uwsgi_log("rack.input each is not implemented (req %p)\n", wsgi_req); return Qnil; } @@ -183,6 +186,11 @@ VALUE rb_uwsgi_io_read(VALUE obj, VALUE args) { if (post_body) { //RUBY_GVL_UNLOCK len = fread( post_body, wsgi_req->post_cl, 1, wsgi_req->async_post); + if (!len) { + uwsgi_error("fread()"); + free(post_body); + return Qnil; + } //RUBY_GVL_LOCK chunk = rb_str_new(post_body, wsgi_req->post_cl); free(post_body); @@ -269,12 +277,6 @@ VALUE require_rails(VALUE arg) { VALUE init_rack_app(VALUE); -#ifdef RUBY19 -VALUE uwsgi_ruby_fiber_yield() { - return rb_fiber_yield(0, NULL); -} -#endif - VALUE uwsgi_ruby_suspend(VALUE *arg) { struct wsgi_request *wsgi_req = current_wsgi_req(); @@ -315,16 +317,8 @@ int uwsgi_rack_init(){ ruby_script("uwsgi"); -#ifdef RUBY19 - uwsgi_register_loop( (char *) "fiber", fiber_loop); -#endif - VALUE rb_uwsgi_embedded = rb_define_module("UWSGI"); rb_define_module_function(rb_uwsgi_embedded, "suspend", uwsgi_ruby_suspend, 0); -#ifdef RUBY19 - rb_define_module_function(rb_uwsgi_embedded, "fiber_yield", uwsgi_ruby_fiber_yield, 0); -#endif - if (ur.rack) { ur.dispatcher = rb_protect(init_rack_app, rb_str_new2(ur.rack), &error); @@ -517,7 +511,7 @@ clear: int uwsgi_rack_request(struct wsgi_request *wsgi_req) { - int error; + int error = 0; int i; VALUE env, ret, status, headers, body; diff --git a/plugins/rack/uwsgi_rack.h b/plugins/rack/uwsgi_rack.h index 96a95028..b62c31b6 100644 --- a/plugins/rack/uwsgi_rack.h +++ b/plugins/rack/uwsgi_rack.h @@ -60,3 +60,5 @@ struct uwsgi_rack { pthread_mutex_t gvl; }; + +void uwsgi_ruby_exception(void); diff --git a/plugins/rack/uwsgiplugin.py b/plugins/rack/uwsgiplugin.py index b978435a..c2bd5de8 100644 --- a/plugins/rack/uwsgiplugin.py +++ b/plugins/rack/uwsgiplugin.py @@ -17,7 +17,6 @@ GCC_LIST = ['rack_plugin'] if v[0] == '1' and v[1] == '9': CFLAGS.append('-DRUBY19') CFLAGS.append('-Wno-unused-parameter') - GCC_LIST.append('fiber') includedir = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['rubyhdrdir']\"").read().rstrip() if includedir == 'nil': diff --git a/plugins/ruby19/uwsgiplugin.py b/plugins/ruby19/uwsgiplugin.py index d382e222..91d593f3 100644 --- a/plugins/ruby19/uwsgiplugin.py +++ b/plugins/ruby19/uwsgiplugin.py @@ -13,7 +13,6 @@ v = version.split('.') if v[0] == '1' and v[1] == '9': CFLAGS.append('-DRUBY19') - GCC_LIST.append('../rack/fiber') includedir = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['rubyhdrdir']\"").read().rstrip() if includedir == 'nil':