File: fiber.c

package info (click to toggle)
uwsgi 1.2.3%2Bdfsg-5%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,208 kB
  • sloc: ansic: 44,479; python: 3,383; sh: 971; makefile: 554; ruby: 514; java: 225; perl: 192; xml: 107; cpp: 38; erlang: 20; php: 13
file content (79 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "../rack/uwsgi_rack.h"

extern struct uwsgi_server uwsgi;
extern struct uwsgi_rack ur;

struct ufib {
	VALUE *fib;
} ufiber;

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;

	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,
};