polyphony 0.19 → 0.20
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/.gitignore +1 -1
 - data/.rubocop.yml +87 -1
 - data/CHANGELOG.md +35 -0
 - data/Gemfile.lock +17 -6
 - data/README.md +200 -139
 - data/Rakefile +4 -4
 - data/TODO.md +35 -7
 - data/bin/poly +11 -0
 - data/docs/getting-started/getting-started.md +1 -1
 - data/docs/summary.md +3 -0
 - data/docs/technical-overview/exception-handling.md +94 -0
 - data/docs/technical-overview/fiber-scheduling.md +99 -0
 - data/examples/core/cancel.rb +8 -4
 - data/examples/core/channel_echo.rb +18 -17
 - data/examples/core/defer.rb +12 -0
 - data/examples/core/enumerator.rb +4 -4
 - data/examples/core/fiber_error.rb +9 -0
 - data/examples/core/fiber_error_with_backtrace.rb +73 -0
 - data/examples/core/fork.rb +6 -6
 - data/examples/core/genserver.rb +16 -8
 - data/examples/core/lock.rb +3 -3
 - data/examples/core/move_on.rb +4 -3
 - data/examples/core/move_on_twice.rb +5 -5
 - data/examples/core/move_on_with_ensure.rb +8 -11
 - data/examples/core/move_on_with_value.rb +14 -0
 - data/examples/core/{multiple_spawn.rb → multiple_spin.rb} +5 -5
 - data/examples/core/nested_cancel.rb +5 -5
 - data/examples/core/{nested_multiple_spawn.rb → nested_multiple_spin.rb} +6 -6
 - data/examples/core/nested_spin.rb +17 -0
 - data/examples/core/pingpong.rb +21 -0
 - data/examples/core/pulse.rb +4 -5
 - data/examples/core/resource.rb +6 -4
 - data/examples/core/resource_cancel.rb +6 -9
 - data/examples/core/resource_delegate.rb +3 -3
 - data/examples/core/sleep.rb +3 -3
 - data/examples/core/sleep_spin.rb +19 -0
 - data/examples/core/snooze.rb +32 -0
 - data/examples/core/spin.rb +14 -0
 - data/examples/core/{spawn_cancel.rb → spin_cancel.rb} +6 -7
 - data/examples/core/spin_error.rb +17 -0
 - data/examples/core/spin_error_backtrace.rb +30 -0
 - data/examples/core/spin_uncaught_error.rb +15 -0
 - data/examples/core/supervisor.rb +8 -8
 - data/examples/core/supervisor_with_cancel_scope.rb +7 -7
 - data/examples/core/supervisor_with_error.rb +8 -8
 - data/examples/core/supervisor_with_manual_move_on.rb +6 -7
 - data/examples/core/suspend.rb +13 -0
 - data/examples/core/thread.rb +1 -1
 - data/examples/core/thread_cancel.rb +9 -11
 - data/examples/core/thread_pool.rb +18 -14
 - data/examples/core/throttle.rb +7 -7
 - data/examples/core/timeout.rb +3 -3
 - data/examples/fs/read.rb +7 -9
 - data/examples/http/config.ru +7 -3
 - data/examples/http/cuba.ru +22 -0
 - data/examples/http/happy_eyeballs.rb +6 -4
 - data/examples/http/http_client.rb +1 -1
 - data/examples/http/http_get.rb +1 -1
 - data/examples/http/http_parse_experiment.rb +21 -16
 - data/examples/http/http_proxy.rb +28 -26
 - data/examples/http/http_server.rb +10 -10
 - data/examples/http/http_server_forked.rb +6 -5
 - data/examples/http/http_server_throttled.rb +3 -3
 - data/examples/http/http_ws_server.rb +11 -11
 - data/examples/http/https_raw_client.rb +1 -1
 - data/examples/http/https_server.rb +8 -8
 - data/examples/http/https_wss_server.rb +13 -11
 - data/examples/http/rack_server.rb +2 -2
 - data/examples/http/rack_server_https.rb +4 -4
 - data/examples/http/rack_server_https_forked.rb +5 -5
 - data/examples/http/websocket_secure_server.rb +6 -6
 - data/examples/http/websocket_server.rb +5 -5
 - data/examples/interfaces/pg_client.rb +4 -4
 - data/examples/interfaces/pg_pool.rb +13 -6
 - data/examples/interfaces/pg_transaction.rb +5 -4
 - data/examples/interfaces/redis_channels.rb +15 -11
 - data/examples/interfaces/redis_client.rb +2 -2
 - data/examples/interfaces/redis_pubsub.rb +2 -1
 - data/examples/interfaces/redis_pubsub_perf.rb +13 -9
 - data/examples/io/backticks.rb +11 -0
 - data/examples/io/cat.rb +4 -5
 - data/examples/io/echo_client.rb +9 -4
 - data/examples/io/echo_client_from_stdin.rb +20 -0
 - data/examples/io/echo_pipe.rb +7 -8
 - data/examples/io/echo_server.rb +8 -6
 - data/examples/io/echo_server_with_timeout.rb +13 -10
 - data/examples/io/echo_stdin.rb +3 -3
 - data/examples/io/httparty.rb +2 -2
 - data/examples/io/httparty_multi.rb +8 -4
 - data/examples/io/httparty_threaded.rb +6 -2
 - data/examples/io/io_read.rb +2 -2
 - data/examples/io/irb.rb +16 -4
 - data/examples/io/net-http.rb +3 -3
 - data/examples/io/open.rb +17 -0
 - data/examples/io/system.rb +3 -3
 - data/examples/io/tcpserver.rb +15 -0
 - data/examples/io/tcpsocket.rb +6 -5
 - data/examples/performance/multi_snooze.rb +29 -0
 - data/examples/performance/{perf_snooze.rb → snooze.rb} +7 -5
 - data/examples/performance/snooze_raw.rb +39 -0
 - data/ext/gyro/async.c +165 -0
 - data/ext/gyro/child.c +167 -0
 - data/ext/{ev → gyro}/extconf.rb +4 -3
 - data/ext/gyro/gyro.c +316 -0
 - data/ext/{ev/ev.h → gyro/gyro.h} +12 -7
 - data/ext/gyro/gyro_ext.c +23 -0
 - data/ext/{ev → gyro}/io.c +65 -57
 - data/ext/{ev → gyro}/libev.h +0 -0
 - data/ext/gyro/signal.c +117 -0
 - data/ext/{ev → gyro}/socket.c +61 -6
 - data/ext/gyro/timer.c +199 -0
 - data/ext/libev/Changes +35 -0
 - data/ext/libev/README +2 -1
 - data/ext/libev/ev.c +213 -151
 - data/ext/libev/ev.h +95 -88
 - data/ext/libev/ev_epoll.c +26 -15
 - data/ext/libev/ev_kqueue.c +11 -5
 - data/ext/libev/ev_linuxaio.c +642 -0
 - data/ext/libev/ev_poll.c +13 -8
 - data/ext/libev/ev_port.c +5 -2
 - data/ext/libev/ev_vars.h +14 -3
 - data/ext/libev/ev_wrap.h +16 -0
 - data/lib/ev_ext.bundle +0 -0
 - data/lib/polyphony.rb +46 -50
 - data/lib/polyphony/auto_run.rb +12 -0
 - data/lib/polyphony/core/cancel_scope.rb +11 -7
 - data/lib/polyphony/core/channel.rb +16 -9
 - data/lib/polyphony/core/coprocess.rb +101 -51
 - data/lib/polyphony/core/exceptions.rb +14 -12
 - data/lib/polyphony/core/resource_pool.rb +21 -8
 - data/lib/polyphony/core/supervisor.rb +10 -5
 - data/lib/polyphony/core/sync.rb +7 -6
 - data/lib/polyphony/core/thread.rb +4 -4
 - data/lib/polyphony/core/thread_pool.rb +4 -4
 - data/lib/polyphony/core/throttler.rb +6 -4
 - data/lib/polyphony/extensions/core.rb +253 -0
 - data/lib/polyphony/extensions/io.rb +28 -16
 - data/lib/polyphony/extensions/openssl.rb +2 -1
 - data/lib/polyphony/extensions/socket.rb +47 -52
 - data/lib/polyphony/http.rb +4 -3
 - data/lib/polyphony/http/agent.rb +68 -57
 - data/lib/polyphony/http/server.rb +5 -5
 - data/lib/polyphony/http/server/http1.rb +268 -0
 - data/lib/polyphony/http/server/http2.rb +62 -0
 - data/lib/polyphony/http/server/http2_stream.rb +104 -0
 - data/lib/polyphony/http/server/rack.rb +64 -0
 - data/lib/polyphony/http/server/request.rb +119 -0
 - data/lib/polyphony/net.rb +26 -15
 - data/lib/polyphony/postgres.rb +17 -13
 - data/lib/polyphony/redis.rb +16 -15
 - data/lib/polyphony/version.rb +1 -1
 - data/lib/polyphony/websocket.rb +11 -4
 - data/polyphony.gemspec +13 -9
 - data/test/eg.rb +27 -0
 - data/test/helper.rb +25 -0
 - data/test/run.rb +5 -0
 - data/test/test_async.rb +33 -0
 - data/test/test_coprocess.rb +239 -77
 - data/test/test_core.rb +95 -61
 - data/test/test_gyro.rb +148 -0
 - data/test/test_http_server.rb +313 -0
 - data/test/test_io.rb +79 -27
 - data/test/test_kernel.rb +22 -12
 - data/test/test_signal.rb +36 -0
 - data/test/test_timer.rb +24 -0
 - metadata +89 -33
 - data/examples/core/nested_async.rb +0 -17
 - data/examples/core/next_tick.rb +0 -12
 - data/examples/core/sleep_spawn.rb +0 -19
 - data/examples/core/spawn.rb +0 -14
 - data/examples/core/spawn_error.rb +0 -28
 - data/examples/performance/perf_multi_snooze.rb +0 -21
 - data/ext/ev/async.c +0 -168
 - data/ext/ev/child.c +0 -169
 - data/ext/ev/ev_ext.c +0 -23
 - data/ext/ev/ev_module.c +0 -242
 - data/ext/ev/signal.c +0 -119
 - data/ext/ev/timer.c +0 -197
 - data/lib/polyphony/core/fiber_pool.rb +0 -98
 - data/lib/polyphony/extensions/kernel.rb +0 -169
 - data/lib/polyphony/http/http1_adapter.rb +0 -254
 - data/lib/polyphony/http/http2_adapter.rb +0 -157
 - data/lib/polyphony/http/rack.rb +0 -25
 - data/lib/polyphony/http/request.rb +0 -66
 - data/test/test_ev.rb +0 -110
 
| 
         @@ -0,0 +1,32 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'bundler/setup'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            COUNT = 10_000
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            class ::Fiber
         
     | 
| 
      
 9 
     | 
    
         
            +
              attr_accessor :tag
         
     | 
| 
      
 10 
     | 
    
         
            +
            end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            COUNTS = Hash.new { |h, k| h[k] = 0 }
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            def t(tag)
         
     | 
| 
      
 15 
     | 
    
         
            +
              Fiber.current.tag = tag.to_s
         
     | 
| 
      
 16 
     | 
    
         
            +
              COUNT.times do
         
     | 
| 
      
 17 
     | 
    
         
            +
                COUNTS[tag] += 1
         
     | 
| 
      
 18 
     | 
    
         
            +
                snooze
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
              puts "#{tag} done"
         
     | 
| 
      
 21 
     | 
    
         
            +
            rescue StandardError => e
         
     | 
| 
      
 22 
     | 
    
         
            +
              puts e
         
     | 
| 
      
 23 
     | 
    
         
            +
            end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            GC.disable
         
     | 
| 
      
 26 
     | 
    
         
            +
            cp1 = spin { t(:a) }
         
     | 
| 
      
 27 
     | 
    
         
            +
            cp2 = spin { t(:b) }
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            sleep 0.01 while cp1.alive? || cp2.alive?
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            puts 'counts:'
         
     | 
| 
      
 32 
     | 
    
         
            +
            p COUNTS
         
     | 
| 
         @@ -1,18 +1,17 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            spin do
         
     | 
| 
       7 
     | 
    
         
            -
              puts "going to sleep..."
         
     | 
| 
       8 
7 
     | 
    
         
             
              cancel_after(1) do
         
     | 
| 
       9 
     | 
    
         
            -
                 
     | 
| 
      
 8 
     | 
    
         
            +
                spin do
         
     | 
| 
      
 9 
     | 
    
         
            +
                  puts 'going to sleep...'
         
     | 
| 
       10 
10 
     | 
    
         
             
                  sleep(2)
         
     | 
| 
       11 
     | 
    
         
            -
                 
     | 
| 
      
 11 
     | 
    
         
            +
                ensure
         
     | 
| 
      
 12 
     | 
    
         
            +
                  puts 'woke up'
         
     | 
| 
      
 13 
     | 
    
         
            +
                end.await
         
     | 
| 
       12 
14 
     | 
    
         
             
              end
         
     | 
| 
       13 
15 
     | 
    
         
             
            rescue Polyphony::Cancel => e
         
     | 
| 
       14 
16 
     | 
    
         
             
              puts "got error: #{e}"
         
     | 
| 
       15 
     | 
    
         
            -
            ensure
         
     | 
| 
       16 
     | 
    
         
            -
              puts "woke up"
         
     | 
| 
       17 
17 
     | 
    
         
             
            end
         
     | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
         @@ -0,0 +1,17 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'bundler/setup'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'polyphony/extensions/backtrace'
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            def error(t)
         
     | 
| 
      
 8 
     | 
    
         
            +
              raise "hello #{t}"
         
     | 
| 
      
 9 
     | 
    
         
            +
            end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            def spin_with_error
         
     | 
| 
      
 12 
     | 
    
         
            +
              spin { error(2) }
         
     | 
| 
      
 13 
     | 
    
         
            +
            end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            spin_with_error
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            puts 'done coprocing'
         
     | 
| 
         @@ -0,0 +1,30 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'bundler/setup'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            def error(t)
         
     | 
| 
      
 7 
     | 
    
         
            +
              raise "hello #{t}"
         
     | 
| 
      
 8 
     | 
    
         
            +
            end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            def deferred_error(t)
         
     | 
| 
      
 11 
     | 
    
         
            +
              snooze
         
     | 
| 
      
 12 
     | 
    
         
            +
              de2(t)
         
     | 
| 
      
 13 
     | 
    
         
            +
            end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            def de2(t)
         
     | 
| 
      
 16 
     | 
    
         
            +
              snooze
         
     | 
| 
      
 17 
     | 
    
         
            +
              error(t)
         
     | 
| 
      
 18 
     | 
    
         
            +
            end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
            def spin_with_error
         
     | 
| 
      
 21 
     | 
    
         
            +
              spin { error(4) }
         
     | 
| 
      
 22 
     | 
    
         
            +
            end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
            spin do
         
     | 
| 
      
 25 
     | 
    
         
            +
              spin do
         
     | 
| 
      
 26 
     | 
    
         
            +
                spin do
         
     | 
| 
      
 27 
     | 
    
         
            +
                  deferred_error(3)
         
     | 
| 
      
 28 
     | 
    
         
            +
                end.await
         
     | 
| 
      
 29 
     | 
    
         
            +
              end.await
         
     | 
| 
      
 30 
     | 
    
         
            +
            end.await
         
     | 
    
        data/examples/core/supervisor.rb
    CHANGED
    
    | 
         @@ -1,9 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
      
 6 
     | 
    
         
            +
            def my_sleep(t)
         
     | 
| 
       7 
7 
     | 
    
         
             
              puts "#{t} start"
         
     | 
| 
       8 
8 
     | 
    
         
             
              sleep(t)
         
     | 
| 
       9 
9 
     | 
    
         
             
              puts "#{t} done"
         
     | 
| 
         @@ -11,11 +11,11 @@ end 
     | 
|
| 
       11 
11 
     | 
    
         | 
| 
       12 
12 
     | 
    
         
             
            puts "#{Time.now} waiting..."
         
     | 
| 
       13 
13 
     | 
    
         
             
            supervise do |s|
         
     | 
| 
       14 
     | 
    
         
            -
              s. 
     | 
| 
       15 
     | 
    
         
            -
              s. 
     | 
| 
       16 
     | 
    
         
            -
              s. 
     | 
| 
       17 
     | 
    
         
            -
              s.spin  
     | 
| 
       18 
     | 
    
         
            -
                puts "fiber count: #{Polyphony:: 
     | 
| 
       19 
     | 
    
         
            -
               
     | 
| 
      
 14 
     | 
    
         
            +
              s.spin { my_sleep(1) }
         
     | 
| 
      
 15 
     | 
    
         
            +
              s.spin { my_sleep(2) }
         
     | 
| 
      
 16 
     | 
    
         
            +
              s.spin { my_sleep(3) }
         
     | 
| 
      
 17 
     | 
    
         
            +
              s.spin do
         
     | 
| 
      
 18 
     | 
    
         
            +
                puts "fiber count: #{Polyphony::Coprocess.list.size}"
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
       20 
20 
     | 
    
         
             
            end
         
     | 
| 
       21 
21 
     | 
    
         
             
            puts "#{Time.now} done waiting"
         
     | 
| 
         @@ -1,9 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
      
 6 
     | 
    
         
            +
            def my_sleep(t)
         
     | 
| 
       7 
7 
     | 
    
         
             
              puts "start: #{t}"
         
     | 
| 
       8 
8 
     | 
    
         
             
              r = sleep(t)
         
     | 
| 
       9 
9 
     | 
    
         
             
              puts "my_sleep result #{r.inspect}"
         
     | 
| 
         @@ -13,11 +13,11 @@ end 
     | 
|
| 
       13 
13 
     | 
    
         
             
            puts "#{Time.now} going to sleep..."
         
     | 
| 
       14 
14 
     | 
    
         
             
            move_on_after(0.5) do
         
     | 
| 
       15 
15 
     | 
    
         
             
              supervise do |s|
         
     | 
| 
       16 
     | 
    
         
            -
                puts  
     | 
| 
       17 
     | 
    
         
            -
                s. 
     | 
| 
       18 
     | 
    
         
            -
                s. 
     | 
| 
       19 
     | 
    
         
            -
                s. 
     | 
| 
      
 16 
     | 
    
         
            +
                puts 'supervise block'
         
     | 
| 
      
 17 
     | 
    
         
            +
                s.spin my_sleep(1)
         
     | 
| 
      
 18 
     | 
    
         
            +
                s.spin my_sleep(2)
         
     | 
| 
      
 19 
     | 
    
         
            +
                s.spin my_sleep(3)
         
     | 
| 
       20 
20 
     | 
    
         
             
              end
         
     | 
| 
       21 
     | 
    
         
            -
              puts  
     | 
| 
      
 21 
     | 
    
         
            +
              puts 'supervisor done'
         
     | 
| 
       22 
22 
     | 
    
         
             
            end
         
     | 
| 
       23 
23 
     | 
    
         
             
            puts "#{Time.now} woke up"
         
     | 
| 
         @@ -1,22 +1,22 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
      
 6 
     | 
    
         
            +
            def my_sleep(t)
         
     | 
| 
       7 
7 
     | 
    
         
             
              sleep(t)
         
     | 
| 
       8 
     | 
    
         
            -
              raise  
     | 
| 
      
 8 
     | 
    
         
            +
              raise 'foo'
         
     | 
| 
       9 
9 
     | 
    
         
             
            end
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         
             
            spin do
         
     | 
| 
       12 
12 
     | 
    
         
             
              puts "#{Time.now} going to sleep..."
         
     | 
| 
       13 
13 
     | 
    
         
             
              supervise do |s|
         
     | 
| 
       14 
     | 
    
         
            -
                s. 
     | 
| 
       15 
     | 
    
         
            -
                s. 
     | 
| 
       16 
     | 
    
         
            -
                s. 
     | 
| 
      
 14 
     | 
    
         
            +
                s.spin { my_sleep(1) }
         
     | 
| 
      
 15 
     | 
    
         
            +
                s.spin { my_sleep(2) }
         
     | 
| 
      
 16 
     | 
    
         
            +
                s.spin { my_sleep(3) }
         
     | 
| 
       17 
17 
     | 
    
         
             
              end
         
     | 
| 
       18 
     | 
    
         
            -
            rescue => e
         
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
      
 18 
     | 
    
         
            +
            # rescue StandardError => e
         
     | 
| 
      
 19 
     | 
    
         
            +
            #   puts "exception from supervisor: #{e}"
         
     | 
| 
       20 
20 
     | 
    
         
             
            ensure
         
     | 
| 
       21 
21 
     | 
    
         
             
              puts "#{Time.now} woke up"
         
     | 
| 
       22 
22 
     | 
    
         
             
            end
         
     | 
| 
         @@ -1,9 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
      
 6 
     | 
    
         
            +
            def my_sleep(t)
         
     | 
| 
       7 
7 
     | 
    
         
             
              puts "start: #{t}"
         
     | 
| 
       8 
8 
     | 
    
         
             
              sleep(t)
         
     | 
| 
       9 
9 
     | 
    
         
             
              puts "done: #{t}"
         
     | 
| 
         @@ -11,14 +11,13 @@ end 
     | 
|
| 
       11 
11 
     | 
    
         | 
| 
       12 
12 
     | 
    
         
             
            puts "#{Time.now} going to sleep..."
         
     | 
| 
       13 
13 
     | 
    
         
             
            result = supervise do |s|
         
     | 
| 
       14 
     | 
    
         
            -
              fiber = Fiber.current
         
     | 
| 
       15 
14 
     | 
    
         
             
              spin do
         
     | 
| 
       16 
15 
     | 
    
         
             
                sleep(0.5)
         
     | 
| 
       17 
     | 
    
         
            -
                puts  
     | 
| 
      
 16 
     | 
    
         
            +
                puts 'stopping supervisor...'
         
     | 
| 
       18 
17 
     | 
    
         
             
                s.stop!
         
     | 
| 
       19 
18 
     | 
    
         
             
              end
         
     | 
| 
       20 
     | 
    
         
            -
              s. 
     | 
| 
       21 
     | 
    
         
            -
              s. 
     | 
| 
       22 
     | 
    
         
            -
              s. 
     | 
| 
      
 19 
     | 
    
         
            +
              s.spin { my_sleep(1) }
         
     | 
| 
      
 20 
     | 
    
         
            +
              s.spin { my_sleep(2) }
         
     | 
| 
      
 21 
     | 
    
         
            +
              s.spin { my_sleep(3) }
         
     | 
| 
       23 
22 
     | 
    
         
             
            end
         
     | 
| 
       24 
23 
     | 
    
         
             
            puts "#{Time.now} woke up with #{result.inspect}"
         
     | 
    
        data/examples/core/thread.rb
    CHANGED
    
    
| 
         @@ -1,27 +1,25 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            @op_count = 0
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
       8 
8 
     | 
    
         
             
            def lengthy_op
         
     | 
| 
       9 
     | 
    
         
            -
               
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
               
     | 
| 
       13 
     | 
    
         
            -
               
     | 
| 
      
 9 
     | 
    
         
            +
              100.times do
         
     | 
| 
      
 10 
     | 
    
         
            +
                sleep 0.01
         
     | 
| 
      
 11 
     | 
    
         
            +
                @op_count += 1
         
     | 
| 
      
 12 
     | 
    
         
            +
              end
         
     | 
| 
      
 13 
     | 
    
         
            +
              @op_count
         
     | 
| 
       14 
14 
     | 
    
         
             
            end
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
       16 
16 
     | 
    
         
             
            spin do
         
     | 
| 
       17 
     | 
    
         
            -
               
     | 
| 
       18 
     | 
    
         
            -
              cancel_after(0.01) do
         
     | 
| 
      
 17 
     | 
    
         
            +
              cancel_after(0.1) do
         
     | 
| 
       19 
18 
     | 
    
         
             
                data = Polyphony::Thread.spin { lengthy_op }.await
         
     | 
| 
       20 
     | 
    
         
            -
                puts " 
     | 
| 
      
 19 
     | 
    
         
            +
                puts "slept #{data} times"
         
     | 
| 
       21 
20 
     | 
    
         
             
              end
         
     | 
| 
       22 
21 
     | 
    
         
             
            rescue Exception => e
         
     | 
| 
       23 
22 
     | 
    
         
             
              puts "error: #{e}"
         
     | 
| 
       24 
23 
     | 
    
         
             
            ensure
         
     | 
| 
       25 
     | 
    
         
            -
               
     | 
| 
      
 24 
     | 
    
         
            +
              puts "slept #{@op_count} times"
         
     | 
| 
       26 
25 
     | 
    
         
             
            end
         
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
         @@ -1,16 +1,16 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            def lengthy_op
         
     | 
| 
       7 
     | 
    
         
            -
              data = IO.read( 
     | 
| 
      
 7 
     | 
    
         
            +
              data = IO.read(__FILE__)
         
     | 
| 
       8 
8 
     | 
    
         
             
              data.clear
         
     | 
| 
       9 
9 
     | 
    
         
             
              # Socket.getaddrinfo('debian.org', 80)
         
     | 
| 
       10 
     | 
    
         
            -
              #Digest::SHA256.digest(IO.read('doc/Promise.html'))
         
     | 
| 
      
 10 
     | 
    
         
            +
              # Digest::SHA256.digest(IO.read('doc/Promise.html'))
         
     | 
| 
       11 
11 
     | 
    
         
             
            end
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
            X =  
     | 
| 
      
 13 
     | 
    
         
            +
            X = 100
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
       15 
15 
     | 
    
         
             
            def compare_performance
         
     | 
| 
       16 
16 
     | 
    
         
             
              t0 = Time.now
         
     | 
| 
         @@ -26,18 +26,20 @@ def compare_performance 
     | 
|
| 
       26 
26 
     | 
    
         
             
                    Polyphony::ThreadPool.process { lengthy_op }
         
     | 
| 
       27 
27 
     | 
    
         
             
                  end
         
     | 
| 
       28 
28 
     | 
    
         
             
                  async_perf = X / (Time.now - t0)
         
     | 
| 
       29 
     | 
    
         
            -
                  puts  
     | 
| 
       30 
     | 
    
         
            -
                     
     | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
      
 29 
     | 
    
         
            +
                  puts format(
         
     | 
| 
      
 30 
     | 
    
         
            +
                    'seq thread pool performance: %g (X %0.2f)',
         
     | 
| 
      
 31 
     | 
    
         
            +
                    async_perf,
         
     | 
| 
      
 32 
     | 
    
         
            +
                    async_perf / native_perf
         
     | 
| 
      
 33 
     | 
    
         
            +
                  )
         
     | 
| 
       32 
34 
     | 
    
         
             
                end
         
     | 
| 
       33 
35 
     | 
    
         | 
| 
       34 
36 
     | 
    
         
             
                acc = 0
         
     | 
| 
       35 
37 
     | 
    
         
             
                count = 0
         
     | 
| 
       36 
     | 
    
         
            -
                10.times do | 
     | 
| 
      
 38 
     | 
    
         
            +
                10.times do |_i|
         
     | 
| 
       37 
39 
     | 
    
         
             
                  t0 = Time.now
         
     | 
| 
       38 
40 
     | 
    
         
             
                  supervise do |s|
         
     | 
| 
       39 
41 
     | 
    
         
             
                    X.times do
         
     | 
| 
       40 
     | 
    
         
            -
                      s. 
     | 
| 
      
 42 
     | 
    
         
            +
                      s.spin { Polyphony::ThreadPool.process { lengthy_op } }
         
     | 
| 
       41 
43 
     | 
    
         
             
                    end
         
     | 
| 
       42 
44 
     | 
    
         
             
                  end
         
     | 
| 
       43 
45 
     | 
    
         
             
                  thread_pool_perf = X / (Time.now - t0)
         
     | 
| 
         @@ -45,13 +47,15 @@ def compare_performance 
     | 
|
| 
       45 
47 
     | 
    
         
             
                  count += 1
         
     | 
| 
       46 
48 
     | 
    
         
             
                end
         
     | 
| 
       47 
49 
     | 
    
         
             
                avg_perf = acc / count
         
     | 
| 
       48 
     | 
    
         
            -
                puts  
     | 
| 
       49 
     | 
    
         
            -
                   
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
      
 50 
     | 
    
         
            +
                puts format(
         
     | 
| 
      
 51 
     | 
    
         
            +
                  'avg thread pool performance: %g (X %0.2f)',
         
     | 
| 
      
 52 
     | 
    
         
            +
                  avg_perf,
         
     | 
| 
      
 53 
     | 
    
         
            +
                  avg_perf / native_perf
         
     | 
| 
      
 54 
     | 
    
         
            +
                )
         
     | 
| 
      
 55 
     | 
    
         
            +
              rescue Exception => e
         
     | 
| 
       52 
56 
     | 
    
         
             
                p e
         
     | 
| 
       53 
57 
     | 
    
         
             
                puts e.backtrace.join("\n")
         
     | 
| 
       54 
58 
     | 
    
         
             
              end
         
     | 
| 
       55 
59 
     | 
    
         
             
            end
         
     | 
| 
       56 
60 
     | 
    
         | 
| 
       57 
     | 
    
         
            -
            spin { compare_performance }
         
     | 
| 
      
 61 
     | 
    
         
            +
            spin { compare_performance }
         
     | 
    
        data/examples/core/throttle.rb
    CHANGED
    
    | 
         @@ -1,16 +1,16 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
            spin  
     | 
| 
      
 6 
     | 
    
         
            +
            spin do
         
     | 
| 
       7 
7 
     | 
    
         
             
              throttled_loop(3) { STDOUT << '.' }
         
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
      
 8 
     | 
    
         
            +
            end
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
     | 
    
         
            -
            spin  
     | 
| 
      
 10 
     | 
    
         
            +
            spin do
         
     | 
| 
       11 
11 
     | 
    
         
             
              throttled_loop(rate: 2) { STDOUT << '?' }
         
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
      
 12 
     | 
    
         
            +
            end
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
     | 
    
         
            -
            spin  
     | 
| 
      
 14 
     | 
    
         
            +
            spin do
         
     | 
| 
       15 
15 
     | 
    
         
             
              throttled_loop(interval: 1) { STDOUT << '*' }
         
     | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
      
 16 
     | 
    
         
            +
            end
         
     | 
    
        data/examples/core/timeout.rb
    CHANGED
    
    
    
        data/examples/fs/read.rb
    CHANGED
    
    | 
         @@ -1,23 +1,21 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require 'bundler/setup'
         
     | 
| 
       4 
     | 
    
         
            -
            require 'polyphony'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'polyphony/auto_run'
         
     | 
| 
       5 
5 
     | 
    
         
             
            require 'polyphony/fs'
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
            PATH = File.expand_path('../../../../docs/dev-journal.md', __dir__)
         
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
7 
     | 
    
         
             
            def raw_read_file(x)
         
     | 
| 
       10 
8 
     | 
    
         
             
              t0 = Time.now
         
     | 
| 
       11 
     | 
    
         
            -
              x.times { IO.orig_read( 
     | 
| 
      
 9 
     | 
    
         
            +
              x.times { IO.orig_read(__FILE__) }
         
     | 
| 
       12 
10 
     | 
    
         
             
              puts "raw_read_file: #{Time.now - t0}"
         
     | 
| 
       13 
11 
     | 
    
         
             
            end
         
     | 
| 
       14 
12 
     | 
    
         | 
| 
       15 
13 
     | 
    
         
             
            def threaded_read_file(x, y)
         
     | 
| 
       16 
14 
     | 
    
         
             
              t0 = Time.now
         
     | 
| 
       17 
15 
     | 
    
         
             
              threads = []
         
     | 
| 
       18 
     | 
    
         
            -
              y.times  
     | 
| 
      
 16 
     | 
    
         
            +
              y.times do
         
     | 
| 
       19 
17 
     | 
    
         
             
                threads << Thread.new { x.times { IO.orig_read(PATH) } }
         
     | 
| 
       20 
     | 
    
         
            -
               
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
       21 
19 
     | 
    
         
             
              threads.each(&:join)
         
     | 
| 
       22 
20 
     | 
    
         
             
              puts "threaded_read_file: #{Time.now - t0}"
         
     | 
| 
       23 
21 
     | 
    
         
             
            end
         
     | 
| 
         @@ -25,9 +23,9 @@ end 
     | 
|
| 
       25 
23 
     | 
    
         
             
            def thread_pool_read_file(x, y)
         
     | 
| 
       26 
24 
     | 
    
         
             
              t0 = Time.now
         
     | 
| 
       27 
25 
     | 
    
         
             
              supervise do |s|
         
     | 
| 
       28 
     | 
    
         
            -
                y.times  
     | 
| 
      
 26 
     | 
    
         
            +
                y.times do
         
     | 
| 
       29 
27 
     | 
    
         
             
                  s.spin { x.times { IO.read(PATH) } }
         
     | 
| 
       30 
     | 
    
         
            -
                 
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
       31 
29 
     | 
    
         
             
              end
         
     | 
| 
       32 
30 
     | 
    
         
             
              puts "thread_pool_read_file: #{Time.now - t0}"
         
     | 
| 
       33 
31 
     | 
    
         
             
            end
         
     | 
| 
         @@ -37,4 +35,4 @@ X = ARGV[1] ? ARGV[1].to_i : 100 
     | 
|
| 
       37 
35 
     | 
    
         | 
| 
       38 
36 
     | 
    
         
             
            raw_read_file(X * Y)
         
     | 
| 
       39 
37 
     | 
    
         
             
            threaded_read_file(X, Y)
         
     | 
| 
       40 
     | 
    
         
            -
            thread_pool_read_file(X, Y)
         
     | 
| 
      
 38 
     | 
    
         
            +
            thread_pool_read_file(X, Y)
         
     |