mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-11-21 19:02:53 +00:00
completed ruby 1.9 fiber support
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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']
|
||||
@@ -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;i<uwsgi.async;i++) {
|
||||
fiber_list[i] = rb_fiber_new(fiber_request, Qnil);
|
||||
rb_gc_register_address(&fiber_list[i]);
|
||||
uwsgi.wsgi_requests[i]->async_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; i<uwsgi.async_nevents;i++) {
|
||||
|
||||
uwsgi_log("received I/O event on fd %d\n", (int) uwsgi.async_events[i].ASYNC_FD);
|
||||
if ( (int) uwsgi.async_events[i].ASYNC_FD == uwsgi.sockets[0].fd) {
|
||||
wsgi_req = find_first_accepting_wsgi_req();
|
||||
if (!wsgi_req) goto cycle;
|
||||
uwsgi_log("request passed to fiber core %d\n", wsgi_req->async_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
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -60,3 +60,5 @@ struct uwsgi_rack {
|
||||
pthread_mutex_t gvl;
|
||||
|
||||
};
|
||||
|
||||
void uwsgi_ruby_exception(void);
|
||||
|
||||
@@ -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':
|
||||
|
||||
@@ -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':
|
||||
|
||||
Reference in New Issue
Block a user