cool.io 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/.gitignore +25 -0
  2. data/CHANGES +199 -0
  3. data/LICENSE +20 -0
  4. data/README.markdown +4 -0
  5. data/Rakefile +98 -0
  6. data/VERSION +1 -0
  7. data/examples/echo_client.rb +38 -0
  8. data/examples/echo_server.rb +27 -0
  9. data/examples/google.rb +9 -0
  10. data/examples/httpclient.rb +38 -0
  11. data/ext/cool.io/.gitignore +5 -0
  12. data/ext/cool.io/cool.io.h +58 -0
  13. data/ext/cool.io/cool.io_ext.c +25 -0
  14. data/ext/cool.io/ev_wrap.h +8 -0
  15. data/ext/cool.io/extconf.rb +69 -0
  16. data/ext/cool.io/iowatcher.c +189 -0
  17. data/ext/cool.io/libev.c +8 -0
  18. data/ext/cool.io/loop.c +303 -0
  19. data/ext/cool.io/stat_watcher.c +191 -0
  20. data/ext/cool.io/timer_watcher.c +219 -0
  21. data/ext/cool.io/utils.c +122 -0
  22. data/ext/cool.io/watcher.c +264 -0
  23. data/ext/cool.io/watcher.h +71 -0
  24. data/ext/http11_client/.gitignore +5 -0
  25. data/ext/http11_client/ext_help.h +14 -0
  26. data/ext/http11_client/extconf.rb +6 -0
  27. data/ext/http11_client/http11_client.c +300 -0
  28. data/ext/http11_client/http11_parser.c +403 -0
  29. data/ext/http11_client/http11_parser.h +48 -0
  30. data/ext/http11_client/http11_parser.rl +173 -0
  31. data/ext/libev/Changes +364 -0
  32. data/ext/libev/LICENSE +36 -0
  33. data/ext/libev/README +58 -0
  34. data/ext/libev/README.embed +3 -0
  35. data/ext/libev/ev.c +3867 -0
  36. data/ext/libev/ev.h +826 -0
  37. data/ext/libev/ev_epoll.c +234 -0
  38. data/ext/libev/ev_kqueue.c +198 -0
  39. data/ext/libev/ev_poll.c +148 -0
  40. data/ext/libev/ev_port.c +164 -0
  41. data/ext/libev/ev_select.c +307 -0
  42. data/ext/libev/ev_vars.h +197 -0
  43. data/ext/libev/ev_win32.c +153 -0
  44. data/ext/libev/ev_wrap.h +186 -0
  45. data/ext/libev/test_libev_win32.c +123 -0
  46. data/ext/libev/update_ev_wrap +19 -0
  47. data/lib/.gitignore +2 -0
  48. data/lib/cool.io.rb +30 -0
  49. data/lib/cool.io/async_watcher.rb +43 -0
  50. data/lib/cool.io/dns_resolver.rb +220 -0
  51. data/lib/cool.io/eventmachine.rb +234 -0
  52. data/lib/cool.io/http_client.rb +419 -0
  53. data/lib/cool.io/io.rb +174 -0
  54. data/lib/cool.io/iowatcher.rb +17 -0
  55. data/lib/cool.io/listener.rb +93 -0
  56. data/lib/cool.io/loop.rb +130 -0
  57. data/lib/cool.io/meta.rb +49 -0
  58. data/lib/cool.io/server.rb +74 -0
  59. data/lib/cool.io/socket.rb +224 -0
  60. data/lib/cool.io/timer_watcher.rb +17 -0
  61. data/lib/coolio.rb +2 -0
  62. data/lib/rev.rb +4 -0
  63. data/spec/async_watcher_spec.rb +57 -0
  64. data/spec/possible_tests/schedules_other_threads.rb +48 -0
  65. data/spec/possible_tests/test_on_resolve_failed.rb +9 -0
  66. data/spec/possible_tests/test_resolves.rb +27 -0
  67. data/spec/possible_tests/test_write_during_resolve.rb +27 -0
  68. data/spec/possible_tests/works_straight.rb +71 -0
  69. data/spec/spec_helper.rb +5 -0
  70. data/spec/timer_watcher_spec.rb +55 -0
  71. data/spec/unix_listener_spec.rb +25 -0
  72. data/spec/unix_server_spec.rb +25 -0
  73. metadata +184 -0
@@ -0,0 +1,5 @@
1
+ Makefile
2
+ mkmf.log
3
+ *.o
4
+ *.so
5
+ *.bundle
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Copyright (C) 2007-10 Tony Arcieri
3
+ * You may redistribute this under the terms of the Ruby license.
4
+ * See LICENSE for details
5
+ */
6
+
7
+ #ifndef COOLIO_H
8
+ #define COOLIO_H
9
+
10
+ #include "ruby.h"
11
+ #include "rubyio.h"
12
+
13
+ #ifdef GetReadFile
14
+ #define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
15
+ #else
16
+
17
+ #if !HAVE_RB_IO_T || (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8)
18
+ #define FPTR_TO_FD(fptr) fileno(fptr->f)
19
+ #else
20
+ #define FPTR_TO_FD(fptr) fptr->fd
21
+ #endif
22
+
23
+ #endif
24
+
25
+ struct Coolio_Event
26
+ {
27
+ /* These values are used to extract events from libev callbacks */
28
+ VALUE watcher;
29
+ int revents;
30
+ };
31
+
32
+ struct Coolio_Loop
33
+ {
34
+ struct ev_loop *ev_loop;
35
+
36
+ int running;
37
+ int events_received;
38
+ int eventbuf_size;
39
+ struct Coolio_Event *eventbuf;
40
+ };
41
+
42
+ struct Coolio_Watcher
43
+ {
44
+ union {
45
+ struct ev_io ev_io;
46
+ struct ev_timer ev_timer;
47
+ struct ev_stat ev_stat;
48
+ } event_types;
49
+
50
+ int enabled;
51
+ VALUE loop;
52
+
53
+ void (*dispatch_callback)(VALUE self, int revents);
54
+ };
55
+
56
+ void Coolio_Loop_process_event(VALUE watcher, int revents);
57
+
58
+ #endif
@@ -0,0 +1,25 @@
1
+ /*
2
+ * Copyright (C) 2007-10 Tony Arcieri
3
+ * You may redistribute this under the terms of the Ruby license.
4
+ * See LICENSE for details
5
+ */
6
+
7
+
8
+ #include "ruby.h"
9
+
10
+ #include "ev_wrap.h"
11
+ #include "cool.io.h"
12
+
13
+ static VALUE mCoolio = Qnil;
14
+
15
+ /* Initialize the coolness */
16
+ void Init_cool()
17
+ {
18
+ /* Initializers for other modules */
19
+ Init_coolio_loop();
20
+ Init_coolio_watcher();
21
+ Init_coolio_iowatcher();
22
+ Init_coolio_timer_watcher();
23
+ Init_coolio_stat_watcher();
24
+ Init_coolio_utils();
25
+ }
@@ -0,0 +1,8 @@
1
+ #define EV_STANDALONE /* keeps ev from requiring config.h */
2
+ #ifdef _WIN32
3
+ # define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */
4
+ # define FD_SETSIZE 2048 /* wishful thinking, as msvcrt6 [?] seems to only allow 512 fd's and 256 sockets max */
5
+ #endif
6
+
7
+ #include "../libev/ev.h"
8
+
@@ -0,0 +1,69 @@
1
+ require 'mkmf'
2
+
3
+ libs = []
4
+
5
+ $defs << "-DRUBY_VERSION_CODE=#{RUBY_VERSION.gsub(/\D/, '')}"
6
+
7
+ if have_func('rb_thread_blocking_region')
8
+ $defs << '-DHAVE_RB_THREAD_BLOCKING_REGION'
9
+ end
10
+
11
+ if have_func('rb_thread_alone')
12
+ $defs << '-DHAVE_RB_THREAD_ALONE'
13
+ end
14
+
15
+ if have_func('rb_str_set_len')
16
+ $defs << '-DHAVE_RB_STR_SET_LEN'
17
+ end
18
+
19
+ if have_library('rt', 'clock_gettime')
20
+ libs << "-lrt"
21
+ end
22
+
23
+ if have_header('sys/select.h')
24
+ $defs << '-DEV_USE_SELECT'
25
+ end
26
+
27
+ if have_header('poll.h')
28
+ $defs << '-DEV_USE_POLL'
29
+ end
30
+
31
+ if have_header('sys/epoll.h')
32
+ $defs << '-DEV_USE_EPOLL'
33
+ end
34
+
35
+ if have_header('sys/event.h') and have_header('sys/queue.h')
36
+ $defs << '-DEV_USE_KQUEUE'
37
+ end
38
+
39
+ if have_header('port.h')
40
+ $defs << '-DEV_USE_PORT'
41
+ end
42
+
43
+ if have_header('sys/resource.h')
44
+ $defs << '-DHAVE_SYS_RESOURCE_H'
45
+ end
46
+
47
+ # ncpu detection specifics
48
+ case RUBY_PLATFORM
49
+ when /linux/
50
+ $defs << '-DHAVE_LINUX_PROCFS'
51
+ else
52
+ if have_func('sysctlbyname', ['sys/param.h', 'sys/sysctl.h'])
53
+ $defs << '-DHAVE_SYSCTLBYNAME'
54
+ end
55
+ end
56
+
57
+ $LIBS << ' ' << libs.join(' ')
58
+
59
+ dir_config('cool.io_ext')
60
+ create_makefile('cool.io_ext')
61
+
62
+ # win32 needs to link in "just the right order" for some reason or ioctlsocket will be mapped to an [inverted] ruby specific version. See libev mailing list for (not so helpful discussion--true cause I'm not sure, but this overcomes the symptom)
63
+ if RUBY_PLATFORM =~ /mingw|win32/
64
+ makefile_contents = File.read 'Makefile'
65
+
66
+ makefile_contents.gsub! 'LIBS = $(LIBRUBYARG_SHARED)', 'LIBS = -lws2_32 $(LIBRUBYARG_SHARED)'
67
+ File.open('Makefile', 'w') { |f| f.write makefile_contents }
68
+ end
69
+
@@ -0,0 +1,189 @@
1
+ /*
2
+ * Copyright (C) 2007-10 Tony Arcieri
3
+ * You may redistribute this under the terms of the Ruby license.
4
+ * See LICENSE for details
5
+ */
6
+
7
+ #include "ruby.h"
8
+ #include "rubyio.h"
9
+
10
+ #include "ev_wrap.h"
11
+
12
+ #include "cool.io.h"
13
+ #include "watcher.h"
14
+
15
+ static VALUE mCoolio = Qnil;
16
+ static VALUE cCoolio_Watcher = Qnil;
17
+ static VALUE cCoolio_Loop = Qnil;
18
+ static VALUE cCoolio_IOWatcher = Qnil;
19
+
20
+ static VALUE Coolio_IOWatcher_initialize(int argc, VALUE *argv, VALUE self);
21
+ static VALUE Coolio_IOWatcher_attach(VALUE self, VALUE loop);
22
+ static VALUE Coolio_IOWatcher_detach(VALUE self);
23
+ static VALUE Coolio_IOWatcher_enable(VALUE self);
24
+ static VALUE Coolio_IOWatcher_disable(VALUE self);
25
+ static VALUE Coolio_IOWatcher_on_readable(VALUE self);
26
+ static VALUE Coolio_IOWatcher_on_writable(VALUE self);
27
+
28
+ static void Coolio_IOWatcher_libev_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents);
29
+ static void Coolio_IOWatcher_dispatch_callback(VALUE self, int revents);
30
+
31
+ /*
32
+ * Coolio::IOWatcher monitors Ruby IO objects for readability or writability.
33
+ * This allows your application to block while the kernel is writing out
34
+ * data and fill the read or write buffer whenever there is space available.
35
+ */
36
+ void Init_coolio_iowatcher()
37
+ {
38
+ mCoolio = rb_define_module("Coolio");
39
+ cCoolio_Watcher = rb_define_class_under(mCoolio, "Watcher", rb_cObject);
40
+ cCoolio_IOWatcher = rb_define_class_under(mCoolio, "IOWatcher", cCoolio_Watcher);
41
+ cCoolio_Loop = rb_define_class_under(mCoolio, "Loop", rb_cObject);
42
+
43
+ rb_define_method(cCoolio_IOWatcher, "initialize", Coolio_IOWatcher_initialize, -1);
44
+ rb_define_method(cCoolio_IOWatcher, "attach", Coolio_IOWatcher_attach, 1);
45
+ rb_define_method(cCoolio_IOWatcher, "detach", Coolio_IOWatcher_detach, 0);
46
+ rb_define_method(cCoolio_IOWatcher, "enable", Coolio_IOWatcher_enable, 0);
47
+ rb_define_method(cCoolio_IOWatcher, "disable", Coolio_IOWatcher_disable, 0);
48
+ rb_define_method(cCoolio_IOWatcher, "on_readable", Coolio_IOWatcher_on_readable, 0);
49
+ rb_define_method(cCoolio_IOWatcher, "on_writable", Coolio_IOWatcher_on_writable, 0);
50
+ }
51
+
52
+ /**
53
+ * call-seq:
54
+ * Coolio::IOWatcher.initialize(IO, events = 'r') -> Coolio::IOWatcher
55
+ *
56
+ * Create a new Coolio::IOWatcher for the given IO object and add it to the given Coolio::Loop
57
+ */
58
+ static VALUE Coolio_IOWatcher_initialize(int argc, VALUE *argv, VALUE self)
59
+ {
60
+ VALUE io, flags;
61
+ char *flags_str;
62
+ int events;
63
+ struct Coolio_Watcher *watcher_data;
64
+ #if HAVE_RB_IO_T
65
+ rb_io_t *fptr;
66
+ #else
67
+ OpenFile *fptr;
68
+ #endif
69
+
70
+ rb_scan_args(argc, argv, "11", &io, &flags);
71
+
72
+ if(flags != Qnil)
73
+ flags_str = RSTRING_PTR(rb_String(flags));
74
+ else
75
+ flags_str = "r";
76
+
77
+ if(!strcmp(flags_str, "r"))
78
+ events = EV_READ;
79
+ else if(!strcmp(flags_str, "w"))
80
+ events = EV_WRITE;
81
+ else if(!strcmp(flags_str, "rw"))
82
+ events = EV_READ | EV_WRITE;
83
+ else
84
+ rb_raise(rb_eArgError, "invalid event type: '%s' (must be 'r', 'w', or 'rw')", flags_str);
85
+
86
+ Data_Get_Struct(self, struct Coolio_Watcher, watcher_data);
87
+ GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
88
+
89
+ watcher_data->dispatch_callback = Coolio_IOWatcher_dispatch_callback;
90
+ ev_io_init(&watcher_data->event_types.ev_io, Coolio_IOWatcher_libev_callback, FPTR_TO_FD(fptr), events);
91
+ watcher_data->event_types.ev_io.data = (void *)self;
92
+
93
+ return Qnil;
94
+ }
95
+
96
+ /**
97
+ * call-seq:
98
+ * Coolio::IOWatcher.attach(loop) -> Coolio::IOWatcher
99
+ *
100
+ * Attach the IO watcher to the given Coolio::Loop. If the watcher is already attached
101
+ * to a loop, detach it from the old one and attach it to the new one.
102
+ */
103
+ static VALUE Coolio_IOWatcher_attach(VALUE self, VALUE loop)
104
+ {
105
+ Watcher_Attach(io, Coolio_IOWatcher_detach, self, loop);
106
+
107
+ return self;
108
+ }
109
+
110
+ /**
111
+ * call-seq:
112
+ * Coolio::IOWatcher.detach -> Coolio::IOWatcher
113
+ *
114
+ * Detach the IO watcher from its current Coolio::Loop.
115
+ */
116
+ static VALUE Coolio_IOWatcher_detach(VALUE self)
117
+ {
118
+ Watcher_Detach(io, self);
119
+
120
+ return self;
121
+ }
122
+
123
+ /**
124
+ * call-seq:
125
+ * Coolio::IOWatcher.enable -> Coolio::IOWatcher
126
+ *
127
+ * Re-enable an IO watcher which has been temporarily disabled. See the
128
+ * disable method for a more thorough explanation.
129
+ */
130
+ static VALUE Coolio_IOWatcher_enable(VALUE self)
131
+ {
132
+ Watcher_Enable(io, self);
133
+
134
+ return self;
135
+ }
136
+
137
+ /**
138
+ * call-seq:
139
+ * Coolio::IOWatcher.disable -> Coolio::IOWatcher
140
+ *
141
+ * Temporarily disable an IO watcher which is attached to a loop.
142
+ * This is useful if you wish to toggle event monitoring on and off.
143
+ */
144
+ static VALUE Coolio_IOWatcher_disable(VALUE self)
145
+ {
146
+ Watcher_Disable(io, self);
147
+
148
+ return self;
149
+ }
150
+
151
+ /**
152
+ * call-seq:
153
+ * Coolio::IOWatcher#on_readable -> nil
154
+ *
155
+ * Called whenever the IO object associated with the IOWatcher is readable
156
+ */
157
+ static VALUE Coolio_IOWatcher_on_readable(VALUE self)
158
+ {
159
+ return Qnil;
160
+ }
161
+
162
+ /**
163
+ * call-seq:
164
+ * Coolio::IOWatcher#on_writable -> nil
165
+ *
166
+ * Called whenever the IO object associated with the IOWatcher is writable
167
+ */
168
+
169
+ static VALUE Coolio_IOWatcher_on_writable(VALUE self)
170
+ {
171
+ return Qnil;
172
+ }
173
+
174
+ /* libev callback */
175
+ static void Coolio_IOWatcher_libev_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents)
176
+ {
177
+ Coolio_Loop_process_event((VALUE)io->data, revents);
178
+ }
179
+
180
+ /* Coolio::Loop dispatch callback */
181
+ static void Coolio_IOWatcher_dispatch_callback(VALUE self, int revents)
182
+ {
183
+ if(revents & EV_READ)
184
+ rb_funcall(self, rb_intern("on_readable"), 0, 0);
185
+ else if(revents & EV_WRITE)
186
+ rb_funcall(self, rb_intern("on_writable"), 0, 0);
187
+ else
188
+ rb_raise(rb_eRuntimeError, "unknown revents value for ev_io: %d", revents);
189
+ }
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (C) 2007 Tony Arcieri
3
+ * You may redistribute this under the terms of the Ruby license.
4
+ * See LICENSE for details
5
+ */
6
+
7
+ #include "ev_wrap.h"
8
+ #include "../libev/ev.c"
@@ -0,0 +1,303 @@
1
+ /*
2
+ * Copyright (C) 2007-10 Tony Arcieri
3
+ * You may redistribute this under the terms of the Ruby license.
4
+ * See LICENSE for details
5
+ */
6
+
7
+ #include <assert.h>
8
+ #include "ruby.h"
9
+ #include "rubysig.h"
10
+ #include "ev_wrap.h"
11
+
12
+ #include "cool.io.h"
13
+
14
+ #if defined(HAVE_RB_THREAD_BLOCKING_REGION)
15
+ # define Coolio_Loop_may_block_safely() (1)
16
+ #elif defined(HAVE_RB_THREAD_ALONE)
17
+ # define Coolio_Loop_may_block_safely() (rb_thread_alone())
18
+ #else /* just in case Ruby changes: */
19
+ # define Coolio_Loop_may_block_safely() (0)
20
+ #endif
21
+
22
+ static VALUE mCoolio = Qnil;
23
+ static VALUE cCoolio_Loop = Qnil;
24
+
25
+ static VALUE Coolio_Loop_allocate(VALUE klass);
26
+ static void Coolio_Loop_mark(struct Coolio_Loop *loop);
27
+ static void Coolio_Loop_free(struct Coolio_Loop *loop);
28
+
29
+ static VALUE Coolio_Loop_initialize(VALUE self);
30
+ static VALUE Coolio_Loop_ev_loop_new(VALUE self, VALUE flags);
31
+ static VALUE Coolio_Loop_run_once(VALUE self);
32
+ static VALUE Coolio_Loop_run_nonblock(VALUE self);
33
+
34
+ static void Coolio_Loop_ev_loop_oneshot(struct Coolio_Loop *loop_data);
35
+ static void Coolio_Loop_dispatch_events(struct Coolio_Loop *loop_data);
36
+
37
+ #define DEFAULT_EVENTBUF_SIZE 32
38
+ #define RUN_LOOP(loop_data, options) \
39
+ loop_data->running = 1; \
40
+ ev_loop(loop_data->ev_loop, options); \
41
+ loop_data->running = 0;
42
+
43
+ /*
44
+ * Coolio::Loop represents an event loop. Event watchers can be attached and
45
+ * unattached. When an event loop is run, all currently attached watchers
46
+ * are monitored for events, and their respective callbacks are signaled
47
+ * whenever events occur.
48
+ */
49
+ void Init_coolio_loop()
50
+ {
51
+ mCoolio = rb_define_module("Coolio");
52
+ cCoolio_Loop = rb_define_class_under(mCoolio, "Loop", rb_cObject);
53
+ rb_define_alloc_func(cCoolio_Loop, Coolio_Loop_allocate);
54
+
55
+ rb_define_method(cCoolio_Loop, "initialize", Coolio_Loop_initialize, 0);
56
+ rb_define_private_method(cCoolio_Loop, "ev_loop_new", Coolio_Loop_ev_loop_new, 1);
57
+ rb_define_method(cCoolio_Loop, "run_once", Coolio_Loop_run_once, 0);
58
+ rb_define_method(cCoolio_Loop, "run_nonblock", Coolio_Loop_run_nonblock, 0);
59
+ }
60
+
61
+ static VALUE Coolio_Loop_allocate(VALUE klass)
62
+ {
63
+ struct Coolio_Loop *loop = (struct Coolio_Loop *)xmalloc(sizeof(struct Coolio_Loop));
64
+
65
+ loop->ev_loop = 0;
66
+ loop->running = 0;
67
+ loop->events_received = 0;
68
+ loop->eventbuf_size = DEFAULT_EVENTBUF_SIZE;
69
+ loop->eventbuf = (struct Coolio_Event *)xmalloc(sizeof(struct Coolio_Event) * DEFAULT_EVENTBUF_SIZE);
70
+
71
+ return Data_Wrap_Struct(klass, Coolio_Loop_mark, Coolio_Loop_free, loop);
72
+ }
73
+
74
+ static void Coolio_Loop_mark(struct Coolio_Loop *loop)
75
+ {
76
+ }
77
+
78
+ static void Coolio_Loop_free(struct Coolio_Loop *loop)
79
+ {
80
+ if(!loop->ev_loop)
81
+ return;
82
+
83
+ ev_loop_destroy(loop->ev_loop);
84
+
85
+ xfree(loop->eventbuf);
86
+ xfree(loop);
87
+ }
88
+
89
+ static VALUE Coolio_Loop_initialize(VALUE self)
90
+ {
91
+ Coolio_Loop_ev_loop_new(self, INT2NUM(0));
92
+ }
93
+
94
+ /* Wrapper for populating a Coolio_Loop struct with a new event loop */
95
+ static VALUE Coolio_Loop_ev_loop_new(VALUE self, VALUE flags)
96
+ {
97
+ struct Coolio_Loop *loop_data;
98
+ Data_Get_Struct(self, struct Coolio_Loop, loop_data);
99
+
100
+ if(loop_data->ev_loop)
101
+ rb_raise(rb_eRuntimeError, "loop already initialized");
102
+
103
+ loop_data->ev_loop = ev_loop_new(NUM2INT(flags));
104
+
105
+ return Qnil;
106
+ }
107
+
108
+ /* libev callback for receiving events */
109
+ void Coolio_Loop_process_event(VALUE watcher, int revents)
110
+ {
111
+ struct Coolio_Loop *loop_data;
112
+ struct Coolio_Watcher *watcher_data;
113
+
114
+ /* The Global VM lock isn't held right now, but hopefully
115
+ * we can still do this safely */
116
+ Data_Get_Struct(watcher, struct Coolio_Watcher, watcher_data);
117
+ Data_Get_Struct(watcher_data->loop, struct Coolio_Loop, loop_data);
118
+
119
+ /* Well, what better place to explain how this all works than
120
+ * where the most wonky and convoluted stuff is going on!
121
+ *
122
+ * Our call path up to here looks a little something like:
123
+ *
124
+ * -> release GVL -> event syscall -> libev callback
125
+ * (GVL = Global VM Lock) ^^^ You are here
126
+ *
127
+ * We released the GVL in the Coolio_Loop_run_once() function
128
+ * so other Ruby threads can run while we make a blocking
129
+ * system call (one of epoll, kqueue, port, poll, or select,
130
+ * depending on the platform).
131
+ *
132
+ * More specifically, this is a libev callback abstraction
133
+ * called from a real libev callback in every watcher,
134
+ * hence this function not being static. The real libev
135
+ * callbacks are event-specific and handled in a watcher.
136
+ *
137
+ * For syscalls like epoll and kqueue, the kernel tells libev
138
+ * a pointer (to a structure with a pointer) to the watcher
139
+ * object. No data structure lookups are required at all
140
+ * (beyond structs), it's smooth O(1) sailing the entire way.
141
+ * Then libev calls out to the watcher's callback, which
142
+ * calls this function.
143
+ *
144
+ * Now, you may be curious: if the watcher already knew what
145
+ * event fired, why the hell is it telling the loop? Why
146
+ * doesn't it just rb_funcall() the appropriate callback?
147
+ *
148
+ * Well, the problem is the Global VM Lock isn't held right
149
+ * now, so we can't rb_funcall() anything. In order to get
150
+ * it back we have to:
151
+ *
152
+ * stash event and return -> acquire GVL -> dispatch to Ruby
153
+ *
154
+ * Which is kinda ugly and confusing, but still gives us
155
+ * an O(1) event loop whose heart is in the kernel itself. w00t!
156
+ *
157
+ * So, stash the event in the loop's data struct. When we return
158
+ * the ev_loop() call being made in the Coolio_Loop_run_once_blocking()
159
+ * function below will also return, at which point the GVL is
160
+ * reacquired and we can call out to Ruby */
161
+
162
+ /* Grow the event buffer if it's too small */
163
+ if(loop_data->events_received >= loop_data->eventbuf_size) {
164
+ loop_data->eventbuf_size *= 2;
165
+ loop_data->eventbuf = (struct Coolio_Event *)xrealloc(
166
+ loop_data->eventbuf,
167
+ sizeof(struct Coolio_Event) * loop_data->eventbuf_size
168
+ );
169
+ }
170
+
171
+ loop_data->eventbuf[loop_data->events_received].watcher = watcher;
172
+ loop_data->eventbuf[loop_data->events_received].revents = revents;
173
+
174
+ loop_data->events_received++;
175
+ }
176
+
177
+ /**
178
+ * call-seq:
179
+ * Coolio::Loop.run_once -> nil
180
+ *
181
+ * Run the Coolio::Loop once, blocking until events are received.
182
+ */
183
+ static VALUE Coolio_Loop_run_once(VALUE self)
184
+ {
185
+ VALUE nevents;
186
+
187
+ if (Coolio_Loop_may_block_safely()) {
188
+ struct Coolio_Loop *loop_data;
189
+
190
+ Data_Get_Struct(self, struct Coolio_Loop, loop_data);
191
+
192
+ assert(loop_data->ev_loop && !loop_data->events_received);
193
+
194
+ Coolio_Loop_ev_loop_oneshot(loop_data);
195
+ Coolio_Loop_dispatch_events(loop_data);
196
+
197
+ nevents = INT2NUM(loop_data->events_received);
198
+ loop_data->events_received = 0;
199
+
200
+ } else {
201
+ nevents = Coolio_Loop_run_nonblock(self);
202
+ rb_thread_schedule();
203
+ }
204
+
205
+ return nevents;
206
+ }
207
+
208
+ /* Ruby 1.9 supports blocking system calls through rb_thread_blocking_region() */
209
+ #ifdef HAVE_RB_THREAD_BLOCKING_REGION
210
+ #define HAVE_EV_LOOP_ONESHOT
211
+ static VALUE Coolio_Loop_ev_loop_oneshot_blocking(void *ptr)
212
+ {
213
+ /* The libev loop has now escaped through the Global VM Lock unscathed! */
214
+ struct Coolio_Loop *loop_data = (struct Coolio_Loop *)ptr;
215
+
216
+ RUN_LOOP(loop_data, EVLOOP_ONESHOT);
217
+
218
+ return Qnil;
219
+ }
220
+
221
+ static void Coolio_Loop_ev_loop_oneshot(struct Coolio_Loop *loop_data)
222
+ {
223
+ /* Use Ruby 1.9's rb_thread_blocking_region call to make a blocking system call */
224
+ rb_thread_blocking_region(Coolio_Loop_ev_loop_oneshot_blocking, loop_data, RUBY_UBF_IO, 0);
225
+ }
226
+ #endif
227
+
228
+ /* Ruby 1.8 requires us to periodically run the event loop then defer back to
229
+ * the green threads scheduler */
230
+ #ifndef HAVE_EV_LOOP_ONESHOT
231
+ #define BLOCKING_INTERVAL 0.01 /* Block for 10ms at a time */
232
+
233
+ /* Stub for scheduler's ev_timer callback */
234
+ static void timer_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
235
+ {
236
+ ev_timer_again (ev_loop, timer);
237
+ }
238
+
239
+ /* Run the event loop, calling rb_thread_schedule every 10ms */
240
+ static void Coolio_Loop_ev_loop_oneshot(struct Coolio_Loop *loop_data)
241
+ {
242
+ struct ev_timer timer;
243
+ struct timeval tv;
244
+
245
+ /* Set up an ev_timer to unblock the loop every 10ms */
246
+ ev_timer_init(&timer, timer_callback, BLOCKING_INTERVAL, BLOCKING_INTERVAL);
247
+ ev_timer_start(loop_data->ev_loop, &timer);
248
+
249
+ /* Loop until we receive events */
250
+ while(!loop_data->events_received) {
251
+ TRAP_BEG;
252
+ RUN_LOOP(loop_data, EVLOOP_ONESHOT);
253
+ TRAP_END;
254
+
255
+ rb_thread_schedule();
256
+ }
257
+
258
+ ev_timer_stop(loop_data->ev_loop, &timer);
259
+ }
260
+ #endif
261
+
262
+ /**
263
+ * call-seq:
264
+ * Coolio::Loop.run_nonblock -> nil
265
+ *
266
+ * Run the Coolio::Loop once, but return immediately if there are no pending events.
267
+ */
268
+ static VALUE Coolio_Loop_run_nonblock(VALUE self)
269
+ {
270
+ struct Coolio_Loop *loop_data;
271
+ VALUE nevents;
272
+
273
+ Data_Get_Struct(self, struct Coolio_Loop, loop_data);
274
+
275
+ assert(loop_data->ev_loop && !loop_data->events_received);
276
+
277
+ TRAP_BEG;
278
+ RUN_LOOP(loop_data, EVLOOP_NONBLOCK);
279
+ TRAP_END;
280
+ Coolio_Loop_dispatch_events(loop_data);
281
+
282
+ nevents = INT2NUM(loop_data->events_received);
283
+ loop_data->events_received = 0;
284
+
285
+ return nevents;
286
+ }
287
+
288
+ static void Coolio_Loop_dispatch_events(struct Coolio_Loop *loop_data)
289
+ {
290
+ int i;
291
+ struct Coolio_Watcher *watcher_data;
292
+
293
+ for(i = 0; i < loop_data->events_received; i++) {
294
+ /* A watcher with pending events may have been detached from the loop
295
+ * during the dispatch process. If so, the watcher clears the pending
296
+ * events, so skip over them */
297
+ if(loop_data->eventbuf[i].watcher == Qnil)
298
+ continue;
299
+
300
+ Data_Get_Struct(loop_data->eventbuf[i].watcher, struct Coolio_Watcher, watcher_data);
301
+ watcher_data->dispatch_callback(loop_data->eventbuf[i].watcher, loop_data->eventbuf[i].revents);
302
+ }
303
+ }