polyphony 0.43.2 → 0.43.8
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.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +2 -2
- data/CHANGELOG.md +43 -0
- data/Gemfile.lock +2 -2
- data/README.md +2 -0
- data/TODO.md +2 -3
- data/docs/_includes/head.html +40 -0
- data/docs/_includes/title.html +1 -0
- data/docs/_user-guide/web-server.md +11 -11
- data/docs/getting-started/overview.md +4 -4
- data/docs/index.md +4 -3
- data/docs/main-concepts/design-principles.md +23 -34
- data/docs/main-concepts/fiber-scheduling.md +1 -1
- data/docs/polyphony-logo.png +0 -0
- data/examples/adapters/concurrent-ruby.rb +9 -0
- data/examples/core/xx-daemon.rb +14 -0
- data/examples/io/xx-happy-eyeballs.rb +21 -22
- data/examples/io/xx-zip.rb +19 -0
- data/examples/performance/fiber_transfer.rb +47 -0
- data/examples/performance/mem-usage.rb +34 -28
- data/examples/performance/messaging.rb +29 -0
- data/examples/performance/multi_snooze.rb +11 -9
- data/examples/xx-spin.rb +32 -0
- data/ext/polyphony/libev_agent.c +181 -24
- data/ext/polyphony/polyphony.c +0 -2
- data/ext/polyphony/polyphony.h +14 -7
- data/ext/polyphony/polyphony_ext.c +2 -2
- data/ext/polyphony/queue.c +168 -0
- data/ext/polyphony/ring_buffer.c +96 -0
- data/ext/polyphony/ring_buffer.h +28 -0
- data/ext/polyphony/thread.c +16 -8
- data/lib/polyphony.rb +28 -12
- data/lib/polyphony/core/global_api.rb +5 -3
- data/lib/polyphony/core/resource_pool.rb +19 -9
- data/lib/polyphony/core/thread_pool.rb +1 -1
- data/lib/polyphony/event.rb +5 -15
- data/lib/polyphony/extensions/core.rb +40 -0
- data/lib/polyphony/extensions/fiber.rb +9 -14
- data/lib/polyphony/extensions/io.rb +17 -16
- data/lib/polyphony/extensions/openssl.rb +8 -0
- data/lib/polyphony/extensions/socket.rb +12 -0
- data/lib/polyphony/version.rb +1 -1
- data/test/helper.rb +1 -1
- data/test/q.rb +24 -0
- data/test/test_agent.rb +3 -3
- data/test/test_event.rb +11 -0
- data/test/test_fiber.rb +3 -3
- data/test/test_global_api.rb +48 -15
- data/test/test_io.rb +24 -2
- data/test/test_queue.rb +39 -1
- data/test/test_resource_pool.rb +12 -0
- data/test/test_throttler.rb +6 -5
- data/test/test_trace.rb +18 -17
- metadata +15 -4
- data/ext/polyphony/libev_queue.c +0 -217
data/ext/polyphony/libev_queue.c
DELETED
@@ -1,217 +0,0 @@
|
|
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
|
-
GetQueue(self, queue);
|
124
|
-
if (queue->shift_queue.count > 0) {
|
125
|
-
struct async_watcher *watcher = async_queue_pop(&queue->shift_queue);
|
126
|
-
if (watcher) {
|
127
|
-
ev_async_send(watcher->ev_loop, &watcher->async);
|
128
|
-
}
|
129
|
-
}
|
130
|
-
rb_ary_push(queue->items, value);
|
131
|
-
return self;
|
132
|
-
}
|
133
|
-
|
134
|
-
struct ev_loop *LibevAgent_ev_loop(VALUE self);
|
135
|
-
|
136
|
-
void async_queue_callback(struct ev_loop *ev_loop, struct ev_async *ev_async, int revents) {
|
137
|
-
struct async_watcher *watcher = (struct async_watcher *)ev_async;
|
138
|
-
Fiber_make_runnable(watcher->fiber, Qnil);
|
139
|
-
}
|
140
|
-
|
141
|
-
VALUE libev_agent_await(VALUE self);
|
142
|
-
|
143
|
-
VALUE LibevQueue_shift(VALUE self) {
|
144
|
-
LibevQueue_t *queue;
|
145
|
-
GetQueue(self, queue);
|
146
|
-
|
147
|
-
if (RARRAY_LEN(queue->items) == 0) {
|
148
|
-
struct async_watcher watcher;
|
149
|
-
VALUE agent = rb_ivar_get(rb_thread_current(), ID_ivar_agent);
|
150
|
-
VALUE switchpoint_result = Qnil;
|
151
|
-
|
152
|
-
watcher.ev_loop = LibevAgent_ev_loop(agent);
|
153
|
-
watcher.fiber = rb_fiber_current();
|
154
|
-
async_queue_push(&queue->shift_queue, &watcher);
|
155
|
-
ev_async_init(&watcher.async, async_queue_callback);
|
156
|
-
ev_async_start(watcher.ev_loop, &watcher.async);
|
157
|
-
|
158
|
-
switchpoint_result = libev_agent_await(agent);
|
159
|
-
ev_async_stop(watcher.ev_loop, &watcher.async);
|
160
|
-
|
161
|
-
if (RTEST(rb_obj_is_kind_of(switchpoint_result, rb_eException))) {
|
162
|
-
async_queue_remove_by_fiber(&queue->shift_queue, watcher.fiber);
|
163
|
-
return rb_funcall(rb_mKernel, ID_raise, 1, switchpoint_result);
|
164
|
-
}
|
165
|
-
RB_GC_GUARD(watcher.fiber);
|
166
|
-
RB_GC_GUARD(agent);
|
167
|
-
RB_GC_GUARD(switchpoint_result);
|
168
|
-
}
|
169
|
-
|
170
|
-
return rb_ary_shift(queue->items);
|
171
|
-
}
|
172
|
-
|
173
|
-
VALUE LibevQueue_shift_each(VALUE self) {
|
174
|
-
LibevQueue_t *queue;
|
175
|
-
VALUE old_queue;
|
176
|
-
GetQueue(self, queue);
|
177
|
-
old_queue = queue->items;
|
178
|
-
queue->items = rb_ary_new();
|
179
|
-
|
180
|
-
if (rb_block_given_p()) {
|
181
|
-
long len = RARRAY_LEN(old_queue);
|
182
|
-
long i;
|
183
|
-
for (i = 0; i < len; i++) {
|
184
|
-
rb_yield(RARRAY_AREF(old_queue, i));
|
185
|
-
}
|
186
|
-
RB_GC_GUARD(old_queue);
|
187
|
-
return self;
|
188
|
-
}
|
189
|
-
else {
|
190
|
-
RB_GC_GUARD(old_queue);
|
191
|
-
return old_queue;
|
192
|
-
}
|
193
|
-
}
|
194
|
-
|
195
|
-
VALUE LibevQueue_empty_p(VALUE self) {
|
196
|
-
LibevQueue_t *queue;
|
197
|
-
GetQueue(self, queue);
|
198
|
-
|
199
|
-
return (RARRAY_LEN(queue->items) == 0) ? Qtrue : Qfalse;
|
200
|
-
}
|
201
|
-
|
202
|
-
void Init_LibevQueue() {
|
203
|
-
cLibevQueue = rb_define_class_under(mPolyphony, "LibevQueue", rb_cData);
|
204
|
-
rb_define_alloc_func(cLibevQueue, LibevQueue_allocate);
|
205
|
-
|
206
|
-
rb_define_method(cLibevQueue, "initialize", LibevQueue_initialize, 0);
|
207
|
-
rb_define_method(cLibevQueue, "push", LibevQueue_push, 1);
|
208
|
-
rb_define_method(cLibevQueue, "<<", LibevQueue_push, 1);
|
209
|
-
|
210
|
-
rb_define_method(cLibevQueue, "pop", LibevQueue_shift, 0);
|
211
|
-
rb_define_method(cLibevQueue, "shift", LibevQueue_shift, 0);
|
212
|
-
|
213
|
-
rb_define_method(cLibevQueue, "shift_each", LibevQueue_shift_each, 0);
|
214
|
-
rb_define_method(cLibevQueue, "empty?", LibevQueue_empty_p, 0);
|
215
|
-
}
|
216
|
-
|
217
|
-
|