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
@@ -1,11 +0,0 @@
1
- ---
2
- layout: page
3
- title: API Reference
4
- description: Lorem ipsum
5
- has_children: true
6
- alphabetical_order: true
7
- section: true
8
- has_toc: false
9
- nav_order: 5
10
- section_link: /api-reference/exception
11
- ---
@@ -1,57 +0,0 @@
1
- ---
2
- layout: page
3
- title: Gyro::Async
4
- parent: API Reference
5
- permalink: /api-reference/gyro-async/
6
- ---
7
- # Gyro::Async
8
-
9
- `Gyro::Async` encapsulates a libev [async
10
- watcher](http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_async_code_how_to_wake_up_an),
11
- allowing thread-safe synchronisation and signalling. `Gyro::Async` watchers are
12
- used both directly and indirectly in Polyphony to implement
13
- [queues](../gyro-queue/), await fibers and threads, and auxiliary features such
14
- as [thread pools](../polyphony-threadpool/).
15
-
16
- A `Gyro::Async` watcher instance is shared across two or more fibers (across one
17
- or more threads), where one fiber waits to be signalled by calling
18
- `Gyro::Async#await`, and one or more other fibers do the signalling by calling
19
- `Gyro::Async#signal`:
20
-
21
- ```ruby
22
- async = Gyro::Async.new
23
- spin do
24
- sleep 1
25
- async.signal
26
- end
27
-
28
- async.await
29
- ```
30
-
31
- The signalling of async watchers is compressed, which means that multiple
32
- invocations of `Gyro::Async#signal` before the event loop can continue will
33
- result the watcher being signalled just a single time.
34
-
35
- In addition to signalling, the async watcher can also be used to transfer an
36
- arbitrary value to the awaitng fiber. See `#signal` for an example.
37
-
38
- ## Instance methods
39
-
40
- ### #await → object
41
-
42
- Blocks the current thread until the watcher is signalled.
43
-
44
- ### #initialize
45
-
46
- Initializes the watcher instance.
47
-
48
- ### #signal(value = nil) → async
49
-
50
- Signals the watcher, causing the fiber awaiting the watcher to become runnable
51
- and be eventually resumed with the given value.
52
-
53
- ```ruby
54
- async = Gyro::Async.new
55
- spin { async.signal('foo') }
56
- async.await #=> 'foo'
57
- ```
@@ -1,29 +0,0 @@
1
- ---
2
- layout: page
3
- title: Gyro::Child
4
- parent: API Reference
5
- permalink: /api-reference/gyro-child/
6
- ---
7
- # Gyro::Child
8
-
9
- `Gyro::Child` encapsulates a libev [child
10
- watcher](http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_child_code_watch_out_for_pro),
11
- used for waiting for a child process to terminate. A `Gyro::Child` watcher
12
- instance can be used for low-level control of child processes, instead of using
13
- more high-level APIs such `Process.wait` etc.
14
-
15
- ## Instance methods
16
-
17
- ### #await → [pid, exitcode]
18
-
19
- Blocks the current thread until the watcher is signalled. The return value is an
20
- array containing the child's pid and the exit code.
21
-
22
- ```ruby
23
- pid = Polyphony.fork { sleep 1 }
24
- Gyro::Child.new(pid).await #=> [pid, 0]
25
- ```
26
-
27
- ### #initialize(pid)
28
-
29
- Initializes the watcher instance with the given pid
@@ -1,44 +0,0 @@
1
- ---
2
- layout: page
3
- title: Gyro::Queue
4
- parent: API Reference
5
- permalink: /api-reference/gyro-queue/
6
- ---
7
- # Gyro::Queue
8
-
9
- `Gyro::Queue` implements a polyphonic (fiber-aware) queue that can store 0 or
10
- more items of any data types. Adding an item to the queue never blocks.
11
- Retrieving an item from the queue will block if the queue is empty.
12
- `Gyro::Queue` is both fiber-safe and thread-safe. This means multiple fibers
13
- from multiple threads can concurrently interact with the same queue.
14
- `Gyro::Queue` is used pervasively across the Polyphony code base for
15
- synchronisation and fiber control.
16
-
17
- ## Instance methods
18
-
19
- ### #&lt;&lt;(object) → queue<br>#push(object) → queue
20
-
21
- Adds an item to the queue.
22
-
23
- ### #clear → queue
24
-
25
- Removes all items currently in the queue.
26
-
27
- ### #empty? → true or false
28
-
29
- Returns true if the queue is empty. Otherwise returns false.
30
-
31
- ### #initialize
32
-
33
- Initializes an empty queue.
34
-
35
- ### #shift → object<br>#pop → object
36
-
37
- Retrieves an item from the queue. If the queue is empty, `#shift` blocks until
38
- an item is added to the queue or until interrupted. Multiple fibers calling
39
- `#shift` are served in a first-ordered first-served manner.
40
-
41
- ### #shift_each → [*object]<br>#shift_each({ block }) → queue
42
-
43
- Removes and returns all items currently in the queue. If a block is given, it
44
- will be invoked for each item.
@@ -1,51 +0,0 @@
1
- ---
2
- layout: page
3
- title: Gyro::Timer
4
- parent: API Reference
5
- permalink: /api-reference/gyro-timer/
6
- ---
7
- # Gyro::Timer
8
-
9
- `Gyro::Timer` encapsulates a libev [timer
10
- watcher](http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_timer_code_relative_and_opti),
11
- allowing waiting a certain amount of time before proceeding with an operation.
12
- Watchers can be either one-time timers or recurring timers. The Polyphony API
13
- provides various APIs that use timer watchers for timeouts, throttled
14
- operations, and sleeping.
15
-
16
- ## Instance methods
17
-
18
- ### #await → object
19
-
20
- Blocks the current thread until the timer has elapsed. For recurrent timers,
21
- `#await` will block until the next timer period has elapsed, as specified by the
22
- `repeat` argument given to `#initialize`.
23
-
24
- ### #initialize(after, repeat)
25
-
26
- Initializes the watcher instance. The `after` argument gives the time duration
27
- in seconds before the timer has elapsed. The `repeat` argument gives the time
28
- period for recurring timers, or `0` for non-recurring timers.
29
-
30
- ### #stop
31
-
32
- Stops an active recurring timer. Recurring timers stay active (from the point of
33
- view of the event loop) even after the timer period has elapsed. Calling `#stop`
34
- marks the timer as inactive and cleans up associated resources. This should
35
- normally be done inside an `ensure` block:
36
-
37
- ```ruby
38
- def repeat(period)
39
- timer = Gyro::Timer.new(period, period)
40
- loop do
41
- timer.await
42
- yield
43
- end
44
- ensure
45
- timer.stop
46
- end
47
-
48
- repeat(10) { puts Time.now }
49
- ```
50
-
51
- There's no need to call `#stop` for non-recurring timers.
@@ -1,25 +0,0 @@
1
- ---
2
- layout: page
3
- title: Gyro
4
- parent: API Reference
5
- permalink: /api-reference/gyro/
6
- ---
7
- # Gyro
8
-
9
- `Gyro` is the subsystem in charge of the low-level functionality in Polyphony.
10
- It contains all of the different event watcher classes, as well as other
11
- low-level constructs such as `Gyro::Queue`, a fiber-aware queue implementation,
12
- used pervasively across the Polyphony code base.
13
-
14
- While most Polyphony-based applications do not normally need to interact
15
- directly with the `Gyro` classes, more advanced applications and libraries may
16
- use those classes to enhance Polyphony and create custom concurrency patterns.
17
-
18
- ## Classes
19
-
20
- - [`Gyro::Async`](../gyro-async/) - async event watcher
21
- - [`Gyro::Child`](../gyro-child/) - child process event watcher
22
- - [`Gyro::IO`](../gyro-io/) - IO event watcher
23
- - [`Gyro::Queue`](../gyro-queue/) - fiber-aware queue
24
- - [`Gyro::Signal`](../gyro-signal/) - signal event watcher
25
- - [`Gyro::Timer`](../gyro-timer/) - timer event watcher
@@ -1,10 +0,0 @@
1
- ---
2
- layout: page
3
- title: Getting Started
4
- description: Lorem ipsum
5
- has_children: true
6
- section: true
7
- has_toc: false
8
- nav_order: 2
9
- section_link: /getting-started/installing
10
- ---
@@ -1,10 +0,0 @@
1
- ---
2
- layout: page
3
- title: Main Concepts
4
- description: Lorem ipsum
5
- has_children: true
6
- section: true
7
- has_toc: false
8
- nav_order: 3
9
- section_link: /main-concepts/concurrency
10
- ---
@@ -1,10 +0,0 @@
1
- ---
2
- layout: page
3
- title: User Guide
4
- description: Lorem ipsum
5
- has_children: true
6
- section: true
7
- has_toc: false
8
- nav_order: 4
9
- section_link: /user-guide/all-about-timers
10
- ---
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- trap('TERM') do
7
- # do nothing
8
- end
9
-
10
- trap('INT') do
11
- # do nothing
12
- end
13
-
14
- puts "go to sleep"
15
- begin
16
- sleep
17
- ensure
18
- puts "done sleeping"
19
- end
@@ -1,148 +0,0 @@
1
- #include "gyro.h"
2
-
3
- struct Gyro_Async {
4
- struct ev_async ev_async;
5
- struct ev_loop *ev_loop;
6
- int active;
7
- VALUE self;
8
- VALUE fiber;
9
- VALUE value;
10
- VALUE selector;
11
- };
12
-
13
- VALUE cGyro_Async = Qnil;
14
-
15
- static void Gyro_Async_mark(void *ptr) {
16
- struct Gyro_Async *async = ptr;
17
- if (async->fiber != Qnil) {
18
- rb_gc_mark(async->fiber);
19
- }
20
- if (async->value != Qnil) {
21
- rb_gc_mark(async->value);
22
- }
23
- if (async->selector != Qnil) {
24
- rb_gc_mark(async->selector);
25
- }
26
- }
27
-
28
- static void Gyro_Async_free(void *ptr) {
29
- struct Gyro_Async *async = ptr;
30
- if (async->active) {
31
- ev_clear_pending(async->ev_loop, &async->ev_async);
32
- ev_async_stop(async->ev_loop, &async->ev_async);
33
- }
34
- xfree(async);
35
- }
36
-
37
- static size_t Gyro_Async_size(const void *ptr) {
38
- return sizeof(struct Gyro_Async);
39
- }
40
-
41
- static const rb_data_type_t Gyro_Async_type = {
42
- "Gyro_Async",
43
- {Gyro_Async_mark, Gyro_Async_free, Gyro_Async_size,},
44
- 0, 0, 0
45
- };
46
-
47
- static VALUE Gyro_Async_allocate(VALUE klass) {
48
- struct Gyro_Async *async = ALLOC(struct Gyro_Async);
49
- return TypedData_Wrap_Struct(klass, &Gyro_Async_type, async);
50
- }
51
-
52
- inline void Gyro_Async_activate(struct Gyro_Async *async) {
53
- if (async->active) return;
54
-
55
- async->active = 1;
56
- async->fiber = rb_fiber_current();
57
- async->selector = Thread_current_event_selector();
58
- async->ev_loop = Gyro_Selector_ev_loop(async->selector);
59
- Gyro_Selector_add_active_watcher(async->selector, async->self);
60
- ev_async_start(async->ev_loop, &async->ev_async);
61
- }
62
-
63
- inline void Gyro_Async_deactivate(struct Gyro_Async *async) {
64
- if (!async->active) return;
65
-
66
- ev_async_stop(async->ev_loop, &async->ev_async);
67
- Gyro_Selector_remove_active_watcher(async->selector, async->self);
68
- async->active = 0;
69
- async->ev_loop = 0;
70
- async->selector = Qnil;
71
- async->fiber = Qnil;
72
- async->value = Qnil;
73
- }
74
-
75
- void Gyro_Async_callback(struct ev_loop *ev_loop, struct ev_async *ev_async, int revents) {
76
- struct Gyro_Async *async = (struct Gyro_Async*)ev_async;
77
-
78
- Fiber_make_runnable(async->fiber, async->value);
79
- Gyro_Async_deactivate(async);
80
- }
81
-
82
- #define GetGyro_Async(obj, async) \
83
- TypedData_Get_Struct((obj), struct Gyro_Async, &Gyro_Async_type, (async))
84
-
85
- static VALUE Gyro_Async_initialize(VALUE self) {
86
- struct Gyro_Async *async;
87
- GetGyro_Async(self, async);
88
-
89
- async->self = self;
90
- async->fiber = Qnil;
91
- async->value = Qnil;
92
- async->selector = Qnil;
93
- async->active = 0;
94
- async->ev_loop = 0;
95
-
96
- ev_async_init(&async->ev_async, Gyro_Async_callback);
97
-
98
- return Qnil;
99
- }
100
-
101
- static VALUE Gyro_Async_signal(int argc, VALUE *argv, VALUE self) {
102
- struct Gyro_Async *async;
103
- GetGyro_Async(self, async);
104
-
105
- if (!async->active) {
106
- // printf("signal called before await\n");
107
- return Qnil;
108
- }
109
-
110
- async->value = (argc == 1) ? argv[0] : Qnil;
111
- ev_async_send(async->ev_loop, &async->ev_async);
112
-
113
- return Qnil;
114
- }
115
-
116
- VALUE Gyro_Async_await(VALUE self) {
117
- struct Gyro_Async *async;
118
- GetGyro_Async(self, async);
119
-
120
- Gyro_Async_activate(async);
121
- VALUE ret = Gyro_switchpoint();
122
- Gyro_Async_deactivate(async);
123
-
124
- TEST_RESUME_EXCEPTION(ret);
125
- RB_GC_GUARD(ret);
126
- return ret;
127
- }
128
-
129
- VALUE Gyro_Async_await_no_raise(VALUE self) {
130
- struct Gyro_Async *async;
131
- GetGyro_Async(self, async);
132
-
133
- Gyro_Async_activate(async);
134
- VALUE ret = Gyro_switchpoint();
135
- Gyro_Async_deactivate(async);
136
-
137
- RB_GC_GUARD(ret);
138
- return ret;
139
- }
140
-
141
- void Init_Gyro_Async() {
142
- cGyro_Async = rb_define_class_under(mGyro, "Async", rb_cData);
143
- rb_define_alloc_func(cGyro_Async, Gyro_Async_allocate);
144
-
145
- rb_define_method(cGyro_Async, "initialize", Gyro_Async_initialize, 0);
146
- rb_define_method(cGyro_Async, "signal", Gyro_Async_signal, -1);
147
- rb_define_method(cGyro_Async, "await", Gyro_Async_await, 0);
148
- }
@@ -1,127 +0,0 @@
1
- #include "gyro.h"
2
-
3
- struct Gyro_Child {
4
- struct ev_child ev_child;
5
- struct ev_loop *ev_loop;
6
- int active;
7
- int pid;
8
- VALUE self;
9
- VALUE fiber;
10
- VALUE selector;
11
- };
12
-
13
- static VALUE cGyro_Child = Qnil;
14
-
15
- static void Gyro_Child_mark(void *ptr) {
16
- struct Gyro_Child *child = ptr;
17
- if (child->fiber != Qnil) {
18
- rb_gc_mark(child->fiber);
19
- }
20
- if (child->selector != Qnil) {
21
- rb_gc_mark(child->selector);
22
- }
23
- }
24
-
25
- static void Gyro_Child_free(void *ptr) {
26
- struct Gyro_Child *child = ptr;
27
- if (child->active) {
28
- ev_clear_pending(child->ev_loop, &child->ev_child);
29
- ev_child_stop(child->ev_loop, &child->ev_child);
30
- }
31
- xfree(child);
32
- }
33
-
34
- static size_t Gyro_Child_size(const void *ptr) {
35
- return sizeof(struct Gyro_Child);
36
- }
37
-
38
- static const rb_data_type_t Gyro_Child_type = {
39
- "Gyro_Child",
40
- {Gyro_Child_mark, Gyro_Child_free, Gyro_Child_size,},
41
- 0, 0, 0
42
- };
43
-
44
- static VALUE Gyro_Child_allocate(VALUE klass) {
45
- struct Gyro_Child *child = ALLOC(struct Gyro_Child);
46
- return TypedData_Wrap_Struct(klass, &Gyro_Child_type, child);
47
- }
48
-
49
- inline void Gyro_Child_activate(struct Gyro_Child *child) {
50
- if (child->active) return;
51
-
52
- child->active = 1;
53
- child->fiber = rb_fiber_current();
54
- child->selector = Thread_current_event_selector();
55
- child->ev_loop = Gyro_Selector_ev_loop(child->selector);
56
- Gyro_Selector_add_active_watcher(child->selector, child->self);
57
- ev_child_start(child->ev_loop, &child->ev_child);
58
- }
59
-
60
- inline void Gyro_Child_deactivate(struct Gyro_Child *child) {
61
- if (!child->active) return;
62
-
63
- ev_child_stop(child->ev_loop, &child->ev_child);
64
- Gyro_Selector_remove_active_watcher(child->selector, child->self);
65
- child->active = 0;
66
- child->ev_loop = 0;
67
- child->selector = Qnil;
68
- child->fiber = Qnil;
69
- }
70
-
71
- VALUE Gyro_Child_resume_value(struct ev_child *ev_child) {
72
- int exit_status = ev_child->rstatus >> 8; // weird, why should we do this?
73
-
74
- return rb_ary_new_from_args(
75
- 2, INT2NUM(ev_child->rpid), INT2NUM(exit_status)
76
- );
77
- }
78
-
79
- void Gyro_Child_callback(struct ev_loop *ev_loop, struct ev_child *ev_child, int revents) {
80
- struct Gyro_Child *child = (struct Gyro_Child*)ev_child;
81
-
82
- VALUE resume_value = Gyro_Child_resume_value(ev_child);
83
- Fiber_make_runnable(child->fiber, resume_value);
84
-
85
- Gyro_Child_deactivate(child);
86
- }
87
-
88
- #define GetGyro_Child(obj, child) \
89
- TypedData_Get_Struct((obj), struct Gyro_Child, &Gyro_Child_type, (child))
90
-
91
- static VALUE Gyro_Child_initialize(VALUE self, VALUE pid) {
92
- struct Gyro_Child *child;
93
-
94
- GetGyro_Child(self, child);
95
-
96
- child->self = self;
97
- child->fiber = Qnil;
98
- child->selector = Qnil;
99
- child->pid = NUM2INT(pid);
100
- child->active = 0;
101
- child->ev_loop = 0;
102
-
103
- ev_child_init(&child->ev_child, Gyro_Child_callback, child->pid, 0);
104
-
105
- return Qnil;
106
- }
107
-
108
- static VALUE Gyro_Child_await(VALUE self) {
109
- struct Gyro_Child *child;
110
- GetGyro_Child(self, child);
111
-
112
- Gyro_Child_activate(child);
113
- VALUE ret = Gyro_switchpoint();
114
- Gyro_Child_deactivate(child);
115
-
116
- TEST_RESUME_EXCEPTION(ret);
117
- RB_GC_GUARD(ret);
118
- return ret;
119
- }
120
-
121
- void Init_Gyro_Child() {
122
- cGyro_Child = rb_define_class_under(mGyro, "Child", rb_cData);
123
- rb_define_alloc_func(cGyro_Child, Gyro_Child_allocate);
124
-
125
- rb_define_method(cGyro_Child, "initialize", Gyro_Child_initialize, 1);
126
- rb_define_method(cGyro_Child, "await", Gyro_Child_await, 0);
127
- }