polyphony 0.45.2 → 0.47.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +2 -0
  3. data/.gitmodules +0 -0
  4. data/CHANGELOG.md +39 -0
  5. data/Gemfile.lock +3 -3
  6. data/README.md +3 -3
  7. data/Rakefile +1 -1
  8. data/TODO.md +20 -28
  9. data/bin/test +4 -0
  10. data/examples/core/enumerable.rb +64 -0
  11. data/examples/io/raw.rb +14 -0
  12. data/examples/io/reline.rb +18 -0
  13. data/examples/performance/fiber_resume.rb +43 -0
  14. data/examples/performance/fiber_transfer.rb +13 -4
  15. data/examples/performance/multi_snooze.rb +0 -1
  16. data/examples/performance/thread-vs-fiber/compare.rb +59 -0
  17. data/examples/performance/thread-vs-fiber/em_server.rb +33 -0
  18. data/examples/performance/thread-vs-fiber/polyphony_server.rb +10 -21
  19. data/examples/performance/thread-vs-fiber/threaded_server.rb +22 -15
  20. data/examples/performance/thread_switch.rb +44 -0
  21. data/ext/liburing/liburing.h +585 -0
  22. data/ext/liburing/liburing/README.md +4 -0
  23. data/ext/liburing/liburing/barrier.h +73 -0
  24. data/ext/liburing/liburing/compat.h +15 -0
  25. data/ext/liburing/liburing/io_uring.h +343 -0
  26. data/ext/liburing/queue.c +333 -0
  27. data/ext/liburing/register.c +187 -0
  28. data/ext/liburing/setup.c +210 -0
  29. data/ext/liburing/syscall.c +54 -0
  30. data/ext/liburing/syscall.h +18 -0
  31. data/ext/polyphony/backend.h +1 -15
  32. data/ext/polyphony/backend_common.h +129 -0
  33. data/ext/polyphony/backend_io_uring.c +995 -0
  34. data/ext/polyphony/backend_io_uring_context.c +74 -0
  35. data/ext/polyphony/backend_io_uring_context.h +53 -0
  36. data/ext/polyphony/{libev_backend.c → backend_libev.c} +308 -297
  37. data/ext/polyphony/event.c +1 -1
  38. data/ext/polyphony/extconf.rb +31 -13
  39. data/ext/polyphony/fiber.c +60 -32
  40. data/ext/polyphony/libev.c +4 -0
  41. data/ext/polyphony/libev.h +8 -2
  42. data/ext/polyphony/liburing.c +8 -0
  43. data/ext/polyphony/playground.c +51 -0
  44. data/ext/polyphony/polyphony.c +9 -6
  45. data/ext/polyphony/polyphony.h +35 -19
  46. data/ext/polyphony/polyphony_ext.c +12 -4
  47. data/ext/polyphony/queue.c +100 -35
  48. data/ext/polyphony/runqueue.c +102 -0
  49. data/ext/polyphony/runqueue_ring_buffer.c +85 -0
  50. data/ext/polyphony/runqueue_ring_buffer.h +31 -0
  51. data/ext/polyphony/thread.c +42 -90
  52. data/lib/polyphony.rb +2 -2
  53. data/lib/polyphony/adapters/process.rb +0 -3
  54. data/lib/polyphony/adapters/trace.rb +2 -2
  55. data/lib/polyphony/core/exceptions.rb +0 -4
  56. data/lib/polyphony/core/global_api.rb +47 -23
  57. data/lib/polyphony/core/sync.rb +7 -5
  58. data/lib/polyphony/extensions/core.rb +14 -33
  59. data/lib/polyphony/extensions/debug.rb +13 -0
  60. data/lib/polyphony/extensions/fiber.rb +21 -3
  61. data/lib/polyphony/extensions/io.rb +15 -4
  62. data/lib/polyphony/extensions/openssl.rb +6 -0
  63. data/lib/polyphony/extensions/socket.rb +63 -10
  64. data/lib/polyphony/version.rb +1 -1
  65. data/polyphony.gemspec +1 -1
  66. data/test/helper.rb +36 -4
  67. data/test/io_uring_test.rb +55 -0
  68. data/test/stress.rb +4 -1
  69. data/test/test_backend.rb +63 -6
  70. data/test/test_ext.rb +1 -2
  71. data/test/test_fiber.rb +55 -20
  72. data/test/test_global_api.rb +132 -31
  73. data/test/test_io.rb +42 -0
  74. data/test/test_queue.rb +117 -0
  75. data/test/test_signal.rb +11 -8
  76. data/test/test_socket.rb +2 -2
  77. data/test/test_sync.rb +21 -0
  78. data/test/test_throttler.rb +3 -6
  79. data/test/test_trace.rb +7 -5
  80. metadata +36 -6
@@ -129,4 +129,121 @@ class QueueTest < MiniTest::Test
129
129
 
130
130
  assert_equal 0, @queue.size
131
131
  end
132
+ end
133
+
134
+ class CappedQueueTest < MiniTest::Test
135
+ def setup
136
+ super
137
+ @queue = Polyphony::Queue.new
138
+ @queue.cap(3)
139
+ end
140
+
141
+ def test_capped?
142
+ q = Polyphony::Queue.new
143
+ assert_nil q.capped?
144
+
145
+ q.cap(3)
146
+ assert_equal 3, q.capped?
147
+ end
148
+
149
+ def test_initalize_with_cap
150
+ q = Polyphony::Queue.new(42)
151
+ assert_equal 42, q.capped?
152
+ end
153
+
154
+ def test_capped_push
155
+ buffer = []
156
+ a = spin do
157
+ (1..5).each do |i|
158
+ @queue.push(i)
159
+ buffer << :"p#{i}"
160
+ end
161
+ @queue.push :stop
162
+ end
163
+
164
+ snooze
165
+
166
+ b = spin_loop do
167
+ i = @queue.shift
168
+ raise Polyphony::Terminate if i == :stop
169
+ buffer << :"s#{i}"
170
+ end
171
+
172
+ Fiber.join(a, b)
173
+ assert_equal [:p1, :p2, :s1, :p3, :s2, :p4, :s3, :p5, :s4, :s5], buffer
174
+ end
175
+
176
+ def test_capped_multi_push
177
+ buffer = []
178
+ a = spin(:a) do
179
+ (1..3).each do |i|
180
+ @queue.push(i)
181
+ buffer << :"p#{i}"
182
+ end
183
+ end
184
+
185
+ buffer = []
186
+ b = spin(:b) do
187
+ (4..6).each do |i|
188
+ @queue.push(i)
189
+ buffer << :"p#{i}"
190
+ end
191
+ @queue.push :stop
192
+ end
193
+
194
+ c = spin_loop do
195
+ i = @queue.shift
196
+ raise Polyphony::Terminate if i == :stop
197
+ buffer << :"s#{i}"
198
+ snooze
199
+ end
200
+
201
+ Fiber.join(a, b, c)
202
+ assert_equal [:p1, :p4, :s1, :p5, :p2, :s4, :p3, :s5, :p6, :s2, :s3, :s6], buffer
203
+ end
204
+
205
+ def test_capped_clear
206
+ buffer = []
207
+ a = spin(:a) do
208
+ (1..5).each do |i|
209
+ @queue.push(i)
210
+ buffer << i
211
+ end
212
+ end
213
+
214
+ snooze while buffer.size < 3
215
+ @queue.clear
216
+ buffer << :clear
217
+
218
+ a.join
219
+ assert_equal [1, 2, 3, :clear, 4, 5], buffer
220
+ end
221
+
222
+ def test_capped_delete
223
+ buffer = []
224
+ a = spin(:a) do
225
+ (1..5).each do |i|
226
+ @queue.push(i)
227
+ buffer << i
228
+ end
229
+ end
230
+
231
+ i = 0
232
+ spin_loop do
233
+ i += 1
234
+ snooze
235
+ end
236
+
237
+ 5.times { snooze }
238
+ assert_equal 5, i
239
+ @queue.delete 1
240
+ buffer << :"d#{i}"
241
+ 3.times { snooze }
242
+ assert_equal 8, i
243
+ @queue.delete 2
244
+ buffer << :"d#{i}"
245
+
246
+ a.join
247
+ assert_equal [1, 2, 3, :d5, 4, :d8, 5], buffer
248
+ end
132
249
  end
@@ -3,18 +3,21 @@
3
3
  require_relative 'helper'
4
4
 
5
5
  class SignalTrapTest < Minitest::Test
6
+ def test_int_signal
7
+ Thread.new { sleep 0.001; Process.kill('INT', Process.pid) }
8
+ assert_raises(Interrupt) { sleep 5 }
9
+ end
10
+
11
+ def test_term_signal
12
+ Thread.new { sleep 0.001; Process.kill('TERM', Process.pid) }
13
+ assert_raises(SystemExit) { sleep 5 }
14
+ end
15
+
6
16
  def test_signal_exception_handling
7
17
  i, o = IO.pipe
8
18
  pid = Polyphony.fork do
9
19
  i.close
10
- spin do
11
- spin do
12
- sleep 5
13
- rescue ::Interrupt => e
14
- # the signal should be raised only in the main fiber
15
- o.puts "1-interrupt"
16
- end.await
17
- end.await
20
+ sleep 5
18
21
  rescue ::Interrupt => e
19
22
  o.puts "3-interrupt"
20
23
  ensure
@@ -40,12 +40,12 @@ class HTTPClientTest < MiniTest::Test
40
40
  def test_http
41
41
  res = HTTParty.get('http://worldtimeapi.org/api/timezone/Europe/Paris')
42
42
  response = JSON.load(res.body)
43
- assert_equal "CEST", response['abbreviation']
43
+ assert_equal "CET", response['abbreviation']
44
44
  end
45
45
 
46
46
  def test_https
47
47
  res = HTTParty.get('https://worldtimeapi.org/api/timezone/Europe/Paris')
48
48
  response = JSON.load(res.body)
49
- assert_equal "CEST", response['abbreviation']
49
+ assert_equal "CET", response['abbreviation']
50
50
  end
51
51
  end
@@ -20,6 +20,27 @@ class MutexTest < MiniTest::Test
20
20
  assert_equal ['>> 1', '<< 1', '>> 2', '<< 2', '>> 3', '<< 3'], buf
21
21
  end
22
22
 
23
+ def test_mutex_race_condition
24
+ lock = Polyphony::Mutex.new
25
+ buf = []
26
+ f1 = spin do
27
+ lock.synchronize { buf << 1; snooze; lock.synchronize { buf << 1.1 }; snooze }
28
+ end
29
+ f2 = spin do
30
+ lock.synchronize { buf << 2 }
31
+ end
32
+ f3 = spin do
33
+ lock.synchronize { buf << 3 }
34
+ end
35
+
36
+ snooze
37
+ f2.terminate
38
+
39
+ f3.await
40
+
41
+ assert_equal [1, 1.1, 3], buf
42
+ end
43
+
23
44
  def test_condition_variable
24
45
  buf = []
25
46
  lock1 = Polyphony::Mutex.new
@@ -10,9 +10,7 @@ class ThrottlerTest < MiniTest::Test
10
10
  f = spin { loop { t.process { buffer << 1 } } }
11
11
  sleep 0.2
12
12
  f.stop
13
- elapsed = Time.now - t0
14
- expected = (elapsed * 10).to_i
15
- assert buffer.size >= expected - 1 && buffer.size <= expected + 1
13
+ assert_in_range 1..3, buffer.size
16
14
  ensure
17
15
  t.stop
18
16
  end
@@ -25,7 +23,7 @@ class ThrottlerTest < MiniTest::Test
25
23
  end
26
24
  sleep 0.25
27
25
  f.stop
28
- assert (2..6).include?(buffer.size)
26
+ assert_in_range 2..6, buffer.size
29
27
  ensure
30
28
  t.stop
31
29
  end
@@ -36,8 +34,7 @@ class ThrottlerTest < MiniTest::Test
36
34
  f = spin { loop { t.process { buffer << 1 } } }
37
35
  sleep 0.02
38
36
  f.stop
39
- assert buffer.size >= 2
40
- assert buffer.size <= 3
37
+ assert_in_range 2..3, buffer.size
41
38
  ensure
42
39
  t.stop
43
40
  end
@@ -35,7 +35,9 @@ class TraceTest < MiniTest::Test
35
35
  def test_2_fiber_trace
36
36
  records = []
37
37
  thread = Thread.current
38
- t = Polyphony::Trace.new(:fiber_all) { |r| records << r if Thread.current == thread && r[:event] =~ /^fiber_/ }
38
+ t = Polyphony::Trace.new(:fiber_all) do |r|
39
+ records << r if Thread.current == thread && r[:event] =~ /^fiber_/
40
+ end
39
41
  t.enable
40
42
  Polyphony.trace(true)
41
43
 
@@ -50,15 +52,15 @@ class TraceTest < MiniTest::Test
50
52
  [:current, :fiber_switchpoint],
51
53
  [:f, :fiber_run],
52
54
  [:f, :fiber_switchpoint],
53
- [:f, :fiber_ev_loop_enter],
55
+ [:f, :fiber_event_poll_enter],
54
56
  [:f, :fiber_schedule],
55
- [:f, :fiber_ev_loop_leave],
57
+ [:f, :fiber_event_poll_leave],
56
58
  [:f, :fiber_run],
57
59
  [:f, :fiber_terminate],
58
60
  [:current, :fiber_switchpoint],
59
- [:current, :fiber_ev_loop_enter],
61
+ [:current, :fiber_event_poll_enter],
60
62
  [:current, :fiber_schedule],
61
- [:current, :fiber_ev_loop_leave],
63
+ [:current, :fiber_event_poll_leave],
62
64
  [:current, :fiber_run]
63
65
  ], events
64
66
  ensure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyphony
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.45.2
4
+ version: 0.47.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-03 00:00:00.000000000 Z
11
+ date: 2020-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.5
19
+ version: 1.1.1
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.5
26
+ version: 1.1.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -278,6 +278,7 @@ extra_rdoc_files:
278
278
  files:
279
279
  - ".github/workflows/test.yml"
280
280
  - ".gitignore"
281
+ - ".gitmodules"
281
282
  - ".rubocop.yml"
282
283
  - ".vscode/launch.json"
283
284
  - CHANGELOG.md
@@ -289,6 +290,7 @@ files:
289
290
  - TODO.md
290
291
  - bin/polyphony-debug
291
292
  - bin/stress.rb
293
+ - bin/test
292
294
  - docs/_config.yml
293
295
  - docs/_includes/head.html
294
296
  - docs/_includes/title.html
@@ -346,6 +348,7 @@ files:
346
348
  - examples/core/await.rb
347
349
  - examples/core/channels.rb
348
350
  - examples/core/deferring-an-operation.rb
351
+ - examples/core/enumerable.rb
349
352
  - examples/core/erlang-style-genserver.rb
350
353
  - examples/core/forking.rb
351
354
  - examples/core/handling-signals.rb
@@ -377,11 +380,14 @@ files:
377
380
  - examples/io/open.rb
378
381
  - examples/io/pry.rb
379
382
  - examples/io/rack_server.rb
383
+ - examples/io/raw.rb
384
+ - examples/io/reline.rb
380
385
  - examples/io/system.rb
381
386
  - examples/io/tcpserver.rb
382
387
  - examples/io/tcpsocket.rb
383
388
  - examples/io/tunnel.rb
384
389
  - examples/io/zip.rb
390
+ - examples/performance/fiber_resume.rb
385
391
  - examples/performance/fiber_transfer.rb
386
392
  - examples/performance/fs_read.rb
387
393
  - examples/performance/mem-usage.rb
@@ -390,6 +396,8 @@ files:
390
396
  - examples/performance/snooze.rb
391
397
  - examples/performance/snooze_raw.rb
392
398
  - examples/performance/switch.rb
399
+ - examples/performance/thread-vs-fiber/compare.rb
400
+ - examples/performance/thread-vs-fiber/em_server.rb
393
401
  - examples/performance/thread-vs-fiber/httparty_multi.rb
394
402
  - examples/performance/thread-vs-fiber/httparty_threaded.rb
395
403
  - examples/performance/thread-vs-fiber/polyphony_mt_server.rb
@@ -397,6 +405,7 @@ files:
397
405
  - examples/performance/thread-vs-fiber/polyphony_server_read_loop.rb
398
406
  - examples/performance/thread-vs-fiber/threaded_server.rb
399
407
  - examples/performance/thread_pool_perf.rb
408
+ - examples/performance/thread_switch.rb
400
409
  - ext/libev/Changes
401
410
  - ext/libev/LICENSE
402
411
  - ext/libev/README
@@ -413,19 +422,38 @@ files:
413
422
  - ext/libev/ev_win32.c
414
423
  - ext/libev/ev_wrap.h
415
424
  - ext/libev/test_libev_win32.c
425
+ - ext/liburing/liburing.h
426
+ - ext/liburing/liburing/README.md
427
+ - ext/liburing/liburing/barrier.h
428
+ - ext/liburing/liburing/compat.h
429
+ - ext/liburing/liburing/io_uring.h
430
+ - ext/liburing/queue.c
431
+ - ext/liburing/register.c
432
+ - ext/liburing/setup.c
433
+ - ext/liburing/syscall.c
434
+ - ext/liburing/syscall.h
416
435
  - ext/polyphony/backend.h
436
+ - ext/polyphony/backend_common.h
437
+ - ext/polyphony/backend_io_uring.c
438
+ - ext/polyphony/backend_io_uring_context.c
439
+ - ext/polyphony/backend_io_uring_context.h
440
+ - ext/polyphony/backend_libev.c
417
441
  - ext/polyphony/event.c
418
442
  - ext/polyphony/extconf.rb
419
443
  - ext/polyphony/fiber.c
420
444
  - ext/polyphony/libev.c
421
445
  - ext/polyphony/libev.h
422
- - ext/polyphony/libev_backend.c
446
+ - ext/polyphony/liburing.c
447
+ - ext/polyphony/playground.c
423
448
  - ext/polyphony/polyphony.c
424
449
  - ext/polyphony/polyphony.h
425
450
  - ext/polyphony/polyphony_ext.c
426
451
  - ext/polyphony/queue.c
427
452
  - ext/polyphony/ring_buffer.c
428
453
  - ext/polyphony/ring_buffer.h
454
+ - ext/polyphony/runqueue.c
455
+ - ext/polyphony/runqueue_ring_buffer.c
456
+ - ext/polyphony/runqueue_ring_buffer.h
429
457
  - ext/polyphony/thread.c
430
458
  - ext/polyphony/tracing.c
431
459
  - lib/polyphony.rb
@@ -446,6 +474,7 @@ files:
446
474
  - lib/polyphony/core/thread_pool.rb
447
475
  - lib/polyphony/core/throttler.rb
448
476
  - lib/polyphony/extensions/core.rb
477
+ - lib/polyphony/extensions/debug.rb
449
478
  - lib/polyphony/extensions/fiber.rb
450
479
  - lib/polyphony/extensions/io.rb
451
480
  - lib/polyphony/extensions/openssl.rb
@@ -457,6 +486,7 @@ files:
457
486
  - test/coverage.rb
458
487
  - test/eg.rb
459
488
  - test/helper.rb
489
+ - test/io_uring_test.rb
460
490
  - test/q.rb
461
491
  - test/run.rb
462
492
  - test/stress.rb
@@ -505,7 +535,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
505
535
  - !ruby/object:Gem::Version
506
536
  version: '0'
507
537
  requirements: []
508
- rubygems_version: 3.1.2
538
+ rubygems_version: 3.1.4
509
539
  signing_key:
510
540
  specification_version: 4
511
541
  summary: Fine grained concurrency for Ruby