polyphony 0.36 → 0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +11 -2
  3. data/.gitignore +2 -2
  4. data/.rubocop.yml +30 -0
  5. data/CHANGELOG.md +28 -2
  6. data/Gemfile +0 -11
  7. data/Gemfile.lock +15 -14
  8. data/README.md +2 -1
  9. data/Rakefile +7 -3
  10. data/TODO.md +28 -95
  11. data/docs/_config.yml +56 -7
  12. data/docs/_sass/custom/custom.scss +0 -30
  13. data/docs/_sass/overrides.scss +0 -46
  14. data/docs/{user-guide → _user-guide}/all-about-timers.md +0 -0
  15. data/docs/_user-guide/index.md +9 -0
  16. data/docs/{user-guide → _user-guide}/web-server.md +0 -0
  17. data/docs/api-reference/fiber.md +2 -2
  18. data/docs/api-reference/index.md +9 -0
  19. data/docs/api-reference/polyphony-process.md +1 -1
  20. data/docs/api-reference/thread.md +1 -1
  21. data/docs/faq.md +21 -11
  22. data/docs/getting-started/index.md +10 -0
  23. data/docs/getting-started/installing.md +2 -6
  24. data/docs/getting-started/overview.md +507 -0
  25. data/docs/getting-started/tutorial.md +27 -19
  26. data/docs/index.md +3 -2
  27. data/docs/main-concepts/concurrency.md +0 -5
  28. data/docs/main-concepts/design-principles.md +69 -21
  29. data/docs/main-concepts/extending.md +1 -1
  30. data/docs/main-concepts/index.md +9 -0
  31. data/examples/core/01-spinning-up-fibers.rb +1 -0
  32. data/examples/core/03-interrupting.rb +4 -1
  33. data/examples/core/04-handling-signals.rb +19 -0
  34. data/examples/core/xx-agent.rb +102 -0
  35. data/examples/core/xx-fork-cleanup.rb +22 -0
  36. data/examples/core/xx-sleeping.rb +14 -6
  37. data/examples/io/tunnel.rb +48 -0
  38. data/examples/io/xx-irb.rb +1 -1
  39. data/examples/performance/thread-vs-fiber/polyphony_mt_server.rb +7 -6
  40. data/examples/performance/thread-vs-fiber/polyphony_server.rb +13 -36
  41. data/examples/performance/thread-vs-fiber/polyphony_server_read_loop.rb +58 -0
  42. data/examples/performance/xx-array.rb +11 -0
  43. data/examples/performance/xx-fiber-switch.rb +9 -0
  44. data/examples/performance/xx-snooze.rb +15 -0
  45. data/ext/{gyro → polyphony}/extconf.rb +2 -2
  46. data/ext/{gyro → polyphony}/fiber.c +18 -22
  47. data/ext/{gyro → polyphony}/libev.c +0 -0
  48. data/ext/{gyro → polyphony}/libev.h +0 -0
  49. data/ext/polyphony/libev_agent.c +718 -0
  50. data/ext/polyphony/libev_queue.c +216 -0
  51. data/ext/{gyro/gyro.c → polyphony/polyphony.c} +16 -46
  52. data/ext/{gyro/gyro.h → polyphony/polyphony.h} +25 -39
  53. data/ext/polyphony/polyphony_ext.c +23 -0
  54. data/ext/{gyro → polyphony}/socket.c +21 -18
  55. data/ext/polyphony/thread.c +206 -0
  56. data/ext/{gyro → polyphony}/tracing.c +1 -1
  57. data/lib/polyphony.rb +40 -44
  58. data/lib/polyphony/adapters/fs.rb +1 -4
  59. data/lib/polyphony/adapters/irb.rb +1 -1
  60. data/lib/polyphony/adapters/postgres.rb +6 -5
  61. data/lib/polyphony/adapters/process.rb +27 -23
  62. data/lib/polyphony/adapters/trace.rb +110 -105
  63. data/lib/polyphony/core/channel.rb +35 -35
  64. data/lib/polyphony/core/exceptions.rb +29 -29
  65. data/lib/polyphony/core/global_api.rb +94 -91
  66. data/lib/polyphony/core/resource_pool.rb +83 -83
  67. data/lib/polyphony/core/sync.rb +16 -16
  68. data/lib/polyphony/core/thread_pool.rb +49 -37
  69. data/lib/polyphony/core/throttler.rb +30 -23
  70. data/lib/polyphony/event.rb +27 -0
  71. data/lib/polyphony/extensions/core.rb +25 -17
  72. data/lib/polyphony/extensions/fiber.rb +269 -267
  73. data/lib/polyphony/extensions/io.rb +56 -26
  74. data/lib/polyphony/extensions/openssl.rb +5 -9
  75. data/lib/polyphony/extensions/socket.rb +29 -10
  76. data/lib/polyphony/extensions/thread.rb +19 -12
  77. data/lib/polyphony/net.rb +64 -60
  78. data/lib/polyphony/version.rb +1 -1
  79. data/polyphony.gemspec +4 -7
  80. data/test/helper.rb +14 -1
  81. data/test/stress.rb +17 -12
  82. data/test/test_agent.rb +124 -0
  83. data/test/{test_async.rb → test_event.rb} +15 -7
  84. data/test/test_ext.rb +25 -4
  85. data/test/test_fiber.rb +19 -10
  86. data/test/test_global_api.rb +4 -4
  87. data/test/test_io.rb +46 -24
  88. data/test/test_queue.rb +74 -0
  89. data/test/test_signal.rb +3 -40
  90. data/test/test_socket.rb +33 -0
  91. data/test/test_thread.rb +38 -16
  92. data/test/test_thread_pool.rb +2 -2
  93. data/test/test_throttler.rb +0 -1
  94. data/test/test_trace.rb +6 -5
  95. metadata +41 -57
  96. data/docs/_includes/nav.html +0 -51
  97. data/docs/_includes/prevnext.html +0 -17
  98. data/docs/_layouts/default.html +0 -106
  99. data/docs/api-reference.md +0 -11
  100. data/docs/api-reference/gyro-async.md +0 -57
  101. data/docs/api-reference/gyro-child.md +0 -29
  102. data/docs/api-reference/gyro-queue.md +0 -44
  103. data/docs/api-reference/gyro-timer.md +0 -51
  104. data/docs/api-reference/gyro.md +0 -25
  105. data/docs/getting-started.md +0 -10
  106. data/docs/main-concepts.md +0 -10
  107. data/docs/user-guide.md +0 -10
  108. data/examples/core/forever_sleep.rb +0 -19
  109. data/ext/gyro/async.c +0 -148
  110. data/ext/gyro/child.c +0 -127
  111. data/ext/gyro/gyro_ext.c +0 -33
  112. data/ext/gyro/io.c +0 -474
  113. data/ext/gyro/queue.c +0 -142
  114. data/ext/gyro/selector.c +0 -205
  115. data/ext/gyro/signal.c +0 -118
  116. data/ext/gyro/thread.c +0 -298
  117. data/ext/gyro/timer.c +0 -134
  118. data/test/test_timer.rb +0 -56
@@ -0,0 +1,216 @@
1
+ #include "polyphony.h"
2
+
3
+ struct async_watcher {
4
+ ev_async async;
5
+ struct ev_loop *ev_loop;
6
+ VALUE fiber;
7
+ };
8
+
9
+ struct async_queue {
10
+ struct async_watcher **queue;
11
+ unsigned int len;
12
+ unsigned int count;
13
+ unsigned int push_idx;
14
+ unsigned int pop_idx;
15
+ };
16
+
17
+ void async_queue_init(struct async_queue *queue) {
18
+ queue->len = 4;
19
+ queue->count = 0;
20
+ queue->queue = malloc(sizeof(struct async_watcher *) * queue->len);
21
+ queue->push_idx = 0;
22
+ queue->pop_idx = 0;
23
+ }
24
+
25
+ void async_queue_free(struct async_queue *queue) {
26
+ free(queue->queue);
27
+ }
28
+
29
+ void async_queue_push(struct async_queue *queue, struct async_watcher *watcher) {
30
+ if (queue->push_idx == queue->len) {
31
+ queue->len = queue->len * 2;
32
+ queue->queue = realloc(queue->queue, sizeof(struct async_watcher *) * queue->len);
33
+ }
34
+ if (queue->count == 0) {
35
+ queue->push_idx = 0;
36
+ queue->pop_idx = 0;
37
+ }
38
+ queue->count++;
39
+ queue->queue[queue->push_idx++] = watcher;
40
+ }
41
+
42
+ struct async_watcher *async_queue_pop(struct async_queue *queue) {
43
+ if (queue->count == 0) return 0;
44
+
45
+ queue->count--;
46
+
47
+ return queue->queue[queue->pop_idx++];
48
+ }
49
+
50
+ void async_queue_remove_at_idx(struct async_queue *queue, unsigned int remove_idx) {
51
+ queue->count--;
52
+ queue->push_idx--;
53
+ if (remove_idx < queue->push_idx)
54
+ memmove(
55
+ queue->queue + remove_idx,
56
+ queue->queue + remove_idx + 1,
57
+ (queue->push_idx - remove_idx) * sizeof(struct async_watcher *)
58
+ );
59
+ }
60
+
61
+ void async_queue_remove_by_fiber(struct async_queue *queue, VALUE fiber) {
62
+ if (queue->count == 0) return;
63
+
64
+ for (unsigned idx = queue->pop_idx; idx < queue->push_idx; idx++) {
65
+ if (queue->queue[idx]->fiber == fiber) {
66
+ async_queue_remove_at_idx(queue, idx);
67
+ return;
68
+ }
69
+ }
70
+ }
71
+
72
+ typedef struct queue {
73
+ VALUE items;
74
+ struct async_queue shift_queue;
75
+ } LibevQueue_t;
76
+
77
+
78
+ VALUE cLibevQueue = Qnil;
79
+
80
+ static void LibevQueue_mark(void *ptr) {
81
+ LibevQueue_t *queue = ptr;
82
+ rb_gc_mark(queue->items);
83
+ }
84
+
85
+ static void LibevQueue_free(void *ptr) {
86
+ LibevQueue_t *queue = ptr;
87
+ async_queue_free(&queue->shift_queue);
88
+ xfree(ptr);
89
+ }
90
+
91
+ static size_t LibevQueue_size(const void *ptr) {
92
+ return sizeof(LibevQueue_t);
93
+ }
94
+
95
+ static const rb_data_type_t LibevQueue_type = {
96
+ "Queue",
97
+ {LibevQueue_mark, LibevQueue_free, LibevQueue_size,},
98
+ 0, 0, 0
99
+ };
100
+
101
+ static VALUE LibevQueue_allocate(VALUE klass) {
102
+ LibevQueue_t *queue;
103
+
104
+ queue = ALLOC(LibevQueue_t);
105
+ return TypedData_Wrap_Struct(klass, &LibevQueue_type, queue);
106
+ }
107
+
108
+ #define GetQueue(obj, queue) \
109
+ TypedData_Get_Struct((obj), LibevQueue_t, &LibevQueue_type, (queue))
110
+
111
+ static VALUE LibevQueue_initialize(VALUE self) {
112
+ LibevQueue_t *queue;
113
+ GetQueue(self, queue);
114
+
115
+ queue->items = rb_ary_new();
116
+ async_queue_init(&queue->shift_queue);
117
+
118
+ return self;
119
+ }
120
+
121
+ VALUE LibevQueue_push(VALUE self, VALUE value) {
122
+ LibevQueue_t *queue;
123
+ struct async_watcher *watcher;
124
+ GetQueue(self, queue);
125
+ watcher = async_queue_pop(&queue->shift_queue);
126
+ if (watcher) {
127
+ ev_async_send(watcher->ev_loop, &watcher->async);
128
+ }
129
+ rb_ary_push(queue->items, value);
130
+ return self;
131
+ }
132
+
133
+ struct ev_loop *LibevAgent_ev_loop(VALUE self);
134
+
135
+ void async_queue_callback(struct ev_loop *ev_loop, struct ev_async *ev_async, int revents) {
136
+ struct async_watcher *watcher = (struct async_watcher *)ev_async;
137
+ Fiber_make_runnable(watcher->fiber, Qnil);
138
+ }
139
+
140
+ VALUE libev_agent_await(VALUE self);
141
+
142
+ VALUE LibevQueue_shift(VALUE self) {
143
+ LibevQueue_t *queue;
144
+ GetQueue(self, queue);
145
+
146
+ if (RARRAY_LEN(queue->items) == 0) {
147
+ struct async_watcher watcher;
148
+ VALUE agent = rb_ivar_get(rb_thread_current(), ID_ivar_agent);
149
+ VALUE switchpoint_result = Qnil;
150
+
151
+ watcher.ev_loop = LibevAgent_ev_loop(agent);
152
+ watcher.fiber = rb_fiber_current();
153
+ async_queue_push(&queue->shift_queue, &watcher);
154
+ ev_async_init(&watcher.async, async_queue_callback);
155
+ ev_async_start(watcher.ev_loop, &watcher.async);
156
+
157
+ switchpoint_result = libev_agent_await(agent);
158
+ ev_async_stop(watcher.ev_loop, &watcher.async);
159
+
160
+ if (RTEST(rb_obj_is_kind_of(switchpoint_result, rb_eException))) {
161
+ async_queue_remove_by_fiber(&queue->shift_queue, watcher.fiber);
162
+ return rb_funcall(rb_mKernel, ID_raise, 1, switchpoint_result);
163
+ }
164
+ RB_GC_GUARD(watcher.fiber);
165
+ RB_GC_GUARD(agent);
166
+ RB_GC_GUARD(switchpoint_result);
167
+ }
168
+
169
+ return rb_ary_shift(queue->items);
170
+ }
171
+
172
+ VALUE LibevQueue_shift_each(VALUE self) {
173
+ LibevQueue_t *queue;
174
+ VALUE old_queue;
175
+ GetQueue(self, queue);
176
+ old_queue = queue->items;
177
+ queue->items = rb_ary_new();
178
+
179
+ if (rb_block_given_p()) {
180
+ long len = RARRAY_LEN(old_queue);
181
+ long i;
182
+ for (i = 0; i < len; i++) {
183
+ rb_yield(RARRAY_AREF(old_queue, i));
184
+ }
185
+ RB_GC_GUARD(old_queue);
186
+ return self;
187
+ }
188
+ else {
189
+ RB_GC_GUARD(old_queue);
190
+ return old_queue;
191
+ }
192
+ }
193
+
194
+ VALUE LibevQueue_empty_p(VALUE self) {
195
+ LibevQueue_t *queue;
196
+ GetQueue(self, queue);
197
+
198
+ return (RARRAY_LEN(queue->items) == 0) ? Qtrue : Qfalse;
199
+ }
200
+
201
+ void Init_LibevQueue() {
202
+ cLibevQueue = rb_define_class_under(mPolyphony, "LibevQueue", rb_cData);
203
+ rb_define_alloc_func(cLibevQueue, LibevQueue_allocate);
204
+
205
+ rb_define_method(cLibevQueue, "initialize", LibevQueue_initialize, 0);
206
+ rb_define_method(cLibevQueue, "push", LibevQueue_push, 1);
207
+ rb_define_method(cLibevQueue, "<<", LibevQueue_push, 1);
208
+
209
+ rb_define_method(cLibevQueue, "pop", LibevQueue_shift, 0);
210
+ rb_define_method(cLibevQueue, "shift", LibevQueue_shift, 0);
211
+
212
+ rb_define_method(cLibevQueue, "shift_each", LibevQueue_shift_each, 0);
213
+ rb_define_method(cLibevQueue, "empty?", LibevQueue_empty_p, 0);
214
+ }
215
+
216
+
@@ -1,7 +1,8 @@
1
- #include "gyro.h"
1
+ #include "polyphony.h"
2
2
 
3
- VALUE mGyro;
3
+ VALUE mPolyphony;
4
4
 
5
+ ID ID_await_no_raise;
5
6
  ID ID_call;
6
7
  ID ID_caller;
7
8
  ID ID_clear;
@@ -24,66 +25,39 @@ ID ID_R;
24
25
  ID ID_W;
25
26
  ID ID_RW;
26
27
 
27
- static VALUE Gyro_break_set(VALUE self) {
28
- // break_flag = 1;
29
- ev_break(Gyro_Selector_current_thread_ev_loop(), EVBREAK_ALL);
30
- return Qnil;
31
- }
32
-
33
- // static VALUE Gyro_break_get(VALUE self) {
34
- // return (break_flag == 0) ? Qfalse : Qtrue;
35
- // }
36
-
37
- VALUE Gyro_snooze(VALUE self) {
28
+ VALUE Polyphony_snooze(VALUE self) {
29
+ VALUE ret;
38
30
  VALUE fiber = rb_fiber_current();
39
- Fiber_make_runnable(fiber, Qnil);
40
31
 
41
- VALUE ret = Thread_switch_fiber(rb_thread_current());
32
+ Fiber_make_runnable(fiber, Qnil);
33
+ ret = Thread_switch_fiber(rb_thread_current());
42
34
  TEST_RESUME_EXCEPTION(ret);
43
35
  RB_GC_GUARD(ret);
44
36
  return ret;
45
37
  }
46
38
 
47
- static VALUE Gyro_post_fork(VALUE self) {
48
- Thread_post_fork(rb_thread_current());
49
- return Qnil;
50
- }
51
-
52
- static VALUE Gyro_ref(VALUE self) {
53
- return Thread_ref(rb_thread_current());
54
- }
55
-
56
- static VALUE Gyro_unref(VALUE self) {
57
- return Thread_unref(rb_thread_current());
58
- }
59
-
60
- static VALUE Gyro_suspend(VALUE self) {
39
+ static VALUE Polyphony_suspend(VALUE self) {
61
40
  VALUE ret = Thread_switch_fiber(rb_thread_current());
62
-
41
+
63
42
  TEST_RESUME_EXCEPTION(ret);
64
43
  RB_GC_GUARD(ret);
65
44
  return ret;
66
45
  }
67
46
 
68
- VALUE Gyro_trace(VALUE self, VALUE enabled) {
47
+ VALUE Polyphony_trace(VALUE self, VALUE enabled) {
69
48
  __tracing_enabled__ = RTEST(enabled) ? 1 : 0;
70
49
  return Qnil;
71
50
  }
72
51
 
73
- void Init_Gyro() {
74
- mGyro = rb_define_module("Gyro");
52
+ void Init_Polyphony() {
53
+ mPolyphony = rb_define_module("Polyphony");
75
54
 
76
- rb_define_singleton_method(mGyro, "post_fork", Gyro_post_fork, 0);
77
- rb_define_singleton_method(mGyro, "ref", Gyro_ref, 0);
78
- rb_define_singleton_method(mGyro, "unref", Gyro_unref, 0);
79
- rb_define_singleton_method(mGyro, "trace", Gyro_trace, 1);
55
+ rb_define_singleton_method(mPolyphony, "trace", Polyphony_trace, 1);
80
56
 
81
- rb_define_singleton_method(mGyro, "break!", Gyro_break_set, 0);
82
- // rb_define_singleton_method(mGyro, "break?", Gyro_break_get, 0);
83
-
84
- rb_define_global_function("snooze", Gyro_snooze, 0);
85
- rb_define_global_function("suspend", Gyro_suspend, 0);
57
+ rb_define_global_function("snooze", Polyphony_snooze, 0);
58
+ rb_define_global_function("suspend", Polyphony_suspend, 0);
86
59
 
60
+ ID_await_no_raise = rb_intern("await_no_raise");
87
61
  ID_call = rb_intern("call");
88
62
  ID_caller = rb_intern("caller");
89
63
  ID_clear = rb_intern("clear");
@@ -102,8 +76,4 @@ void Init_Gyro() {
102
76
  ID_size = rb_intern("size");
103
77
  ID_switch_fiber = rb_intern("switch_fiber");
104
78
  ID_transfer = rb_intern("transfer");
105
-
106
- ID_R = rb_intern("r");
107
- ID_RW = rb_intern("rw");
108
- ID_W = rb_intern("w");
109
79
  }
@@ -7,28 +7,29 @@
7
7
 
8
8
  // debugging
9
9
  #define OBJ_ID(obj) (NUM2LONG(rb_funcall(obj, rb_intern("object_id"), 0)))
10
- #define INSPECT(...) (rb_funcall(rb_cObject, rb_intern("p"), __VA_ARGS__))
10
+ #define INSPECT(obj) { VALUE s = rb_funcall(obj, rb_intern("inspect"), 0); printf("%s\n", StringValueCStr(s));}
11
11
  #define FIBER_TRACE(...) if (__tracing_enabled__) { \
12
12
  rb_funcall(rb_cObject, ID_fiber_trace, __VA_ARGS__); \
13
13
  }
14
14
 
15
+ #define TEST_EXCEPTION(ret) (RTEST(rb_obj_is_kind_of(ret, rb_eException)))
16
+
15
17
  #define TEST_RESUME_EXCEPTION(ret) if (RTEST(rb_obj_is_kind_of(ret, rb_eException))) { \
16
18
  return rb_funcall(rb_mKernel, ID_raise, 1, ret); \
17
19
  }
18
20
 
19
- extern VALUE mGyro;
20
- extern VALUE cGyro_Async;
21
- extern VALUE cGyro_IO;
22
- extern VALUE cGyro_Queue;
23
- extern VALUE cGyro_Selector;
24
- extern VALUE cGyro_Timer;
21
+ extern VALUE mPolyphony;
22
+ extern VALUE cLibevQueue;
23
+ extern VALUE cEvent;
25
24
 
25
+ extern ID ID_await_no_raise;
26
26
  extern ID ID_call;
27
27
  extern ID ID_caller;
28
28
  extern ID ID_clear;
29
29
  extern ID ID_each;
30
30
  extern ID ID_fiber_trace;
31
31
  extern ID ID_inspect;
32
+ extern ID ID_ivar_agent;
32
33
  extern ID ID_ivar_running;
33
34
  extern ID ID_ivar_thread;
34
35
  extern ID ID_new;
@@ -39,9 +40,6 @@ extern ID ID_signal;
39
40
  extern ID ID_size;
40
41
  extern ID ID_switch_fiber;
41
42
  extern ID ID_transfer;
42
- extern ID ID_R;
43
- extern ID ID_W;
44
- extern ID ID_RW;
45
43
 
46
44
  extern VALUE SYM_fiber_create;
47
45
  extern VALUE SYM_fiber_ev_loop_enter;
@@ -59,43 +57,31 @@ enum {
59
57
  FIBER_STATE_SCHEDULED = 2
60
58
  };
61
59
 
62
- VALUE Fiber_auto_async(VALUE self);
63
- VALUE Fiber_auto_io(VALUE self);
64
- void Fiber_make_runnable(VALUE fiber, VALUE value);
65
-
66
- VALUE Gyro_Async_await(VALUE async);
67
- VALUE Gyro_Async_await_no_raise(VALUE async);
68
-
69
- VALUE Gyro_IO_auto_io(int fd, int events);
70
- VALUE Gyro_IO_await(VALUE self);
60
+ // watcher flags
61
+ enum {
62
+ // a watcher's active field will be set to this after fork
63
+ GYRO_WATCHER_POST_FORK = 0xFF
64
+ };
71
65
 
72
- void Gyro_Selector_add_active_watcher(VALUE self, VALUE watcher);
73
- VALUE Gyro_Selector_break_out_of_ev_loop(VALUE self);
74
- struct ev_loop *Gyro_Selector_current_thread_ev_loop();
75
- struct ev_loop *Gyro_Selector_ev_loop(VALUE selector);
76
- ev_tstamp Gyro_Selector_now(VALUE selector);
77
- long Gyro_Selector_pending_count(VALUE self);
78
- VALUE Gyro_Selector_post_fork(VALUE self);
79
- void Gyro_Selector_remove_active_watcher(VALUE self, VALUE watcher);
80
- VALUE Gyro_Selector_run(VALUE self, VALUE current_fiber);
81
- void Gyro_Selector_run_no_wait(VALUE self, VALUE current_fiber, long runnable_count);
82
- VALUE Gyro_switchpoint();
66
+ VALUE Fiber_auto_watcher(VALUE self);
67
+ void Fiber_make_runnable(VALUE fiber, VALUE value);
83
68
 
69
+ VALUE LibevAgent_poll(VALUE self, VALUE nowait, VALUE current_fiber, VALUE queue);
70
+ VALUE LibevAgent_break(VALUE self);
71
+ VALUE LibevAgent_pending_count(VALUE self);
72
+ VALUE LibevAgent_wait_io(VALUE self, VALUE io, VALUE write);
84
73
 
85
- VALUE Gyro_snooze(VALUE self);
86
- VALUE Gyro_Timer_await(VALUE self);
74
+ VALUE LibevAgent_ref(VALUE self);
75
+ VALUE LibevAgent_unref(VALUE self);
76
+ int LibevAgent_ref_count(VALUE self);
77
+ void LibevAgent_reset_ref_count(VALUE self);
87
78
 
88
- VALUE IO_read_watcher(VALUE io);
89
- VALUE IO_write_watcher(VALUE io);
79
+ VALUE Polyphony_snooze(VALUE self);
90
80
 
91
- VALUE Gyro_Queue_push(VALUE self, VALUE value);
81
+ VALUE Polyphony_Queue_push(VALUE self, VALUE value);
92
82
 
93
- VALUE Thread_current_event_selector();
94
- VALUE Thread_post_fork(VALUE thread);
95
- VALUE Thread_ref(VALUE thread);
96
83
  VALUE Thread_schedule_fiber(VALUE thread, VALUE fiber, VALUE value);
97
84
  VALUE Thread_switch_fiber(VALUE thread);
98
- VALUE Thread_unref(VALUE thread);
99
85
 
100
86
  int io_setstrbuf(VALUE *str, long len);
101
87
  void io_set_read_length(VALUE str, long n, int shrinkable);
@@ -0,0 +1,23 @@
1
+ #include "polyphony.h"
2
+
3
+ void Init_Fiber();
4
+ void Init_Polyphony();
5
+ void Init_LibevAgent();
6
+ void Init_LibevQueue();
7
+ void Init_Socket();
8
+ void Init_Thread();
9
+ void Init_Tracing();
10
+
11
+ void Init_polyphony_ext() {
12
+ ev_set_allocator(xrealloc);
13
+
14
+ Init_Polyphony();
15
+ Init_LibevAgent();
16
+ Init_LibevQueue();
17
+
18
+ Init_Fiber();
19
+ Init_Socket();
20
+ Init_Thread();
21
+
22
+ Init_Tracing();
23
+ }
@@ -1,4 +1,4 @@
1
- #include "gyro.h"
1
+ #include "polyphony.h"
2
2
  #include <sys/socket.h>
3
3
 
4
4
  static VALUE cTCPSocket;
@@ -71,8 +71,8 @@ static VALUE BasicSocket_send(int argc, VALUE *argv, VALUE sock) {
71
71
  ssize_t n;
72
72
  rb_blocking_function_t *func;
73
73
  const char *funcname;
74
- VALUE write_watcher = Qnil;
75
-
74
+ VALUE agent = Qnil;
75
+
76
76
  rb_scan_args(argc, argv, "21", &arg.mesg, &flags, &to);
77
77
 
78
78
  StringValue(arg.mesg);
@@ -94,9 +94,9 @@ static VALUE BasicSocket_send(int argc, VALUE *argv, VALUE sock) {
94
94
  arg.fd = fptr->fd;
95
95
  arg.flags = NUM2INT(flags);
96
96
  while ((n = (ssize_t)func(&arg)) < 0) {
97
- if (NIL_P(write_watcher))
98
- write_watcher = Gyro_IO_auto_io(fptr->fd, EV_WRITE);
99
- Gyro_IO_await(write_watcher);
97
+ if (NIL_P(agent))
98
+ agent = rb_ivar_get(rb_thread_current(), ID_ivar_agent);
99
+ LibevAgent_wait_io(agent, sock, Qtrue);
100
100
  }
101
101
  return SSIZET2NUM(n);
102
102
  }
@@ -112,7 +112,7 @@ static VALUE BasicSocket_recv(int argc, VALUE *argv, VALUE sock) {
112
112
  rb_io_t *fptr;
113
113
  long n;
114
114
  int shrinkable;
115
- VALUE read_watcher = Qnil;
115
+ VALUE agent = Qnil;
116
116
 
117
117
 
118
118
  VALUE str = argc >= 3 ? argv[2] : Qnil;
@@ -131,9 +131,9 @@ static VALUE BasicSocket_recv(int argc, VALUE *argv, VALUE sock) {
131
131
  if (n < 0) {
132
132
  int e = errno;
133
133
  if (e == EWOULDBLOCK || e == EAGAIN) {
134
- if (NIL_P(read_watcher))
135
- read_watcher = Gyro_IO_auto_io(fptr->fd, EV_READ);
136
- Gyro_IO_await(read_watcher);
134
+ if (NIL_P(agent))
135
+ agent = rb_ivar_get(rb_thread_current(), ID_ivar_agent);
136
+ LibevAgent_wait_io(agent, sock, Qnil);
137
137
  }
138
138
  else
139
139
  rb_syserr_fail(e, strerror(e));
@@ -157,7 +157,7 @@ static VALUE Socket_accept(VALUE sock) {
157
157
  int fd;
158
158
  struct sockaddr addr;
159
159
  socklen_t len = (socklen_t)sizeof addr;
160
- VALUE read_watcher = Qnil;
160
+ VALUE agent = Qnil;
161
161
 
162
162
  GetOpenFile(sock, fptr);
163
163
  rb_io_set_nonblock(fptr);
@@ -168,9 +168,9 @@ static VALUE Socket_accept(VALUE sock) {
168
168
  if (fd < 0) {
169
169
  int e = errno;
170
170
  if (e == EWOULDBLOCK || e == EAGAIN) {
171
- if (NIL_P(read_watcher))
172
- read_watcher = Gyro_IO_auto_io(fptr->fd, EV_READ);
173
- Gyro_IO_await(read_watcher);
171
+ if (NIL_P(agent))
172
+ agent = rb_ivar_get(rb_thread_current(), ID_ivar_agent);
173
+ LibevAgent_wait_io(agent, sock, Qnil);
174
174
  }
175
175
  else
176
176
  rb_syserr_fail(e, strerror(e));
@@ -196,15 +196,18 @@ static VALUE Socket_accept(VALUE sock) {
196
196
  }
197
197
 
198
198
  void Init_Socket() {
199
+ VALUE cBasicSocket;
200
+ VALUE cSocket;
201
+
199
202
  rb_require("socket");
200
- VALUE cBasicSocket = rb_const_get(rb_cObject, rb_intern("BasicSocket"));
203
+ cBasicSocket = rb_const_get(rb_cObject, rb_intern("BasicSocket"));
201
204
 
202
205
  rb_define_method(cBasicSocket, "send", BasicSocket_send, -1);
203
206
  rb_define_method(cBasicSocket, "recv", BasicSocket_recv, -1);
204
207
 
205
- VALUE cSocket = rb_const_get(rb_cObject, rb_intern("Socket"));
206
-
207
- rb_define_method(cSocket, "accept", Socket_accept, 0);
208
+ cSocket = rb_const_get(rb_cObject, rb_intern("Socket"));
209
+
210
+ // rb_define_method(cSocket, "accept", Socket_accept, 0);
208
211
 
209
212
  cTCPSocket = rb_const_get(rb_cObject, rb_intern("TCPSocket"));
210
213
  }