Debian Codegraph @

include/ruby/internal/intern/cont.h

raw
1 #ifndef RBIMPL_INTERN_CONT_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_CONT_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Public APIs related to rb_cFiber.
22 */
23 #include "ruby/internal/dllexport.h"
24 #include "ruby/internal/value.h"
25 #include "ruby/internal/iterator.h"
26
27 RBIMPL_SYMBOL_EXPORT_BEGIN()
28
29 /* cont.c */
30
31 /**
32 * Creates a Fiber instance from a C-backended block.
33 *
34 * @param[in] func A function, to become the fiber's body.
35 * @param[in] callback_obj Passed as-is to `func`.
36 * @return An allocated new instance of rb_cFiber, which is ready to be
37 * "resume"d.
38 */
39 VALUE rb_fiber_new(rb_block_call_func_t func, VALUE callback_obj);
40
41 /**
42 * Creates a Fiber instance from a C-backended block with the specified
43 * storage.
44 *
45 * If the given storage is Qundef or Qtrue, this function is equivalent to
46 * rb_fiber_new() which inherits storage from the current fiber.
47 *
48 * Specifying Qtrue is experimental and may be changed in the future.
49 *
50 * If the given storage is Qnil, this function will lazy initialize the
51 * internal storage which starts of empty (without any inheritance).
52 *
53 * Otherwise, the given storage is used as the internal storage.
54 *
55 * @param[in] func A function, to become the fiber's body.
56 * @param[in] callback_obj Passed as-is to `func`.
57 * @param[in] storage The way to set up the storage for the fiber.
58 * @return An allocated new instance of rb_cFiber, which is ready to be
59 * "resume"d.
60 */
61 VALUE rb_fiber_new_storage(rb_block_call_func_t func, VALUE callback_obj, VALUE storage);
62
63 /**
64 * Queries the fiber which is calling this function. Any ruby execution
65 * context has its fiber, either explicitly or implicitly.
66 *
67 * @return The current fiber.
68 */
69 VALUE rb_fiber_current(void);
70
71 /**
72 * Queries the liveness of the passed fiber. "Alive" in this context means
73 * that the fiber can still be resumed. Once it reaches is its end of
74 * execution, this function returns ::RUBY_Qfalse.
75 *
76 * @param[in] fiber A target fiber.
77 * @retval RUBY_Qtrue It is.
78 * @retval RUBY_Qfalse It isn't.
79 */
80 VALUE rb_fiber_alive_p(VALUE fiber);
81
82 /**
83 * Queries if an object is a fiber.
84 *
85 * @param[in] obj Arbitrary ruby object.
86 * @retval RUBY_Qtrue It is.
87 * @retval RUBY_Qfalse It isn't.
88 */
89 VALUE rb_obj_is_fiber(VALUE obj);
90
91 /**
92 * Resumes the execution of the passed fiber, either from the point at which
93 * the last rb_fiber_yield() was called if any, or at the beginning of the
94 * fiber body if it is the first call to this function.
95 *
96 * Other arguments are passed into the fiber's body, either as return values of
97 * rb_fiber_yield() in case it switches to there, or as the block parameter of
98 * the fiber body if it switches to the beginning of the fiber.
99 *
100 * The return value of this function is either the value passed to previous
101 * rb_fiber_yield() call, or the ultimate evaluated value of the entire fiber
102 * body if the execution reaches the end of it.
103 *
104 * When an exception happens inside of a fiber it propagates to this function.
105 *
106 * ```ruby
107 * f = Fiber.new do |i|
108 * puts "<x> =>> #{i}"
109 * puts "<y> <-- #{i + 1}"
110 * j = Fiber.yield(i + 1)
111 * puts "<z> =>> #{j}"
112 * puts "<w> <-- #{j + 1}"
113 * next j + 1
114 * end
115 *
116 * puts "[a] <-- 1"
117 * p = f.resume(1)
118 * puts "[b] =>> #{p}"
119 * puts "[c] <-- #{p + 1}"
120 * q = f.resume(p + 1)
121 * puts "[d] =>> #{q}"
122 * ```
123 *
124 * Above program executes in `[a] <x> <y> [b] [c] <z> <w> [d]`.
125 *
126 * @param[out] fiber The fiber to resume.
127 * @param[in] argc Number of objects of `argv`.
128 * @param[in] argv Passed (somehow) to `fiber`.
129 * @exception rb_eFiberError `fib` is terminated etc.
130 * @exception rb_eException Any exceptions happen in `fiber`.
131 * @return (See above)
132 * @note This function _does_ return.
133 *
134 * @internal
135 *
136 * @shyouhei expected this function to raise ::rb_eFrozenError for frozen
137 * fibers but it doesn't in practice. Intentional or ...?
138 */
139 VALUE rb_fiber_resume(VALUE fiber, int argc, const VALUE *argv);
140
141 /**
142 * Identical to rb_fiber_resume(), except you can specify how to handle the
143 * last element of the given array.
144 *
145 * @param[out] fiber The fiber to resume.
146 * @param[in] argc Number of objects of `argv`.
147 * @param[in] argv Passed (somehow) to `fiber`.
148 * @param[in] kw_splat Handling of keyword parameters:
149 * - RB_NO_KEYWORDS `argv`'s last is not a keyword argument.
150 * - RB_PASS_KEYWORDS `argv`'s last is a keyword argument.
151 * - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block.
152 * @exception rb_eFiberError `fiber` is terminated etc.
153 * @exception rb_eException Any exceptions happen in `fiber`.
154 * @return Either what was yielded or the last value of the fiber body.
155 */
156 VALUE rb_fiber_resume_kw(VALUE fiber, int argc, const VALUE *argv, int kw_splat);
157
158 /**
159 * Yields the control back to the point where the current fiber was resumed.
160 * The passed objects would be the return value of rb_fiber_resume(). This
161 * fiber then suspends its execution until next time it is resumed.
162 *
163 * This function can also raise arbitrary exceptions injected from outside of
164 * the fiber using rb_fiber_raise().
165 *
166 * ```ruby
167 * exc = Class.new Exception
168 *
169 * f = Fiber.new do
170 * Fiber.yield
171 * rescue exc => e
172 * puts e.message
173 * end
174 *
175 * f.resume
176 * f.raise exc, "Hi!"
177 * ```
178 *
179 * @param[in] argc Number of objects of `argv`.
180 * @param[in] argv Passed to rb_fiber_resume().
181 * @exception rb_eException (See above)
182 * @return (See rb_fiber_resume() for details)
183 */
184 VALUE rb_fiber_yield(int argc, const VALUE *argv);
185
186 /**
187 * Identical to rb_fiber_yield(), except you can specify how to handle the last
188 * element of the given array.
189 *
190 * @param[in] argc Number of objects of `argv`.
191 * @param[in] argv Passed to rb_fiber_resume().
192 * @param[in] kw_splat Handling of keyword parameters:
193 * - RB_NO_KEYWORDS `argv`'s last is not a keyword argument.
194 * - RB_PASS_KEYWORDS `argv`'s last is a keyword argument.
195 * - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block.
196 * @exception rb_eException What was raised using `Fiber#raise`.
197 * @return (See rb_fiber_resume() for details)
198 */
199 VALUE rb_fiber_yield_kw(int argc, const VALUE *argv, int kw_splat);
200
201 /**
202 * Transfers control to another fiber, resuming it from where it last stopped
203 * or starting it if it was not resumed before. The calling fiber will be
204 * suspended much like in a call to rb_fiber_yield().
205 *
206 * The fiber which receives the transfer call treats it much like a resume
207 * call. Arguments passed to transfer are treated like those passed to resume.
208 *
209 * The two style of control passing to and from fiber (one is rb_fiber_resume()
210 * and rb_fiber_yield(), another is rb_fiber_transfer() to and from fiber)
211 * can't be freely mixed.
212 *
213 * - If the Fiber's lifecycle had started with transfer, it will never be
214 * able to yield or be resumed control passing, only finish or transfer
215 * back. (It still can resume other fibers that are allowed to be
216 * resumed.)
217 *
218 * - If the Fiber's lifecycle had started with resume, it can yield or
219 * transfer to another Fiber, but can receive control back only the way
220 * compatible with the way it was given away: if it had transferred, it
221 * only can be transferred back, and if it had yielded, it only can be
222 * resumed back. After that, it again can transfer or yield.
223 *
224 * If those rules are broken, rb_eFiberError is raised.
225 *
226 * For an individual Fiber design, yield/resume is easier to use (the Fiber
227 * just gives away control, it doesn't need to think about who the control is
228 * given to), while transfer is more flexible for complex cases, allowing to
229 * build arbitrary graphs of Fibers dependent on each other.
230 *
231 * @param[out] fiber Explicit control destination.
232 * @param[in] argc Number of objects of `argv`.
233 * @param[in] argv Passed to rb_fiber_resume().
234 * @exception rb_eFiberError (See above)
235 * @exception rb_eException What was raised using `Fiber#raise`.
236 * @return (See rb_fiber_resume() for details)
237 */
238 VALUE rb_fiber_transfer(VALUE fiber, int argc, const VALUE *argv);
239
240 /**
241 * Identical to rb_fiber_transfer(), except you can specify how to handle the
242 * last element of the given array.
243 *
244 * @param[out] fiber Explicit control destination.
245 * @param[in] argc Number of objects of `argv`.
246 * @param[in] argv Passed to rb_fiber_resume().
247 * @param[in] kw_splat Handling of keyword parameters:
248 * - RB_NO_KEYWORDS `argv`'s last is not a keyword argument.
249 * - RB_PASS_KEYWORDS `argv`'s last is a keyword argument.
250 * - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block.
251 * @exception rb_eFiberError (See above)
252 * @exception rb_eException What was raised using `Fiber#raise`.
253 * @return (See rb_fiber_resume() for details)
254 */
255 VALUE rb_fiber_transfer_kw(VALUE fiber, int argc, const VALUE *argv, int kw_splat);
256
257 /**
258 * Identical to rb_fiber_resume() but instead of resuming normal execution of
259 * the passed fiber, it raises the given exception in it. From inside of the
260 * fiber this would be seen as if rb_fiber_yield() raised.
261 *
262 * This function does return in case the passed fiber gracefully handled the
263 * passed exception. But if it does not, the raised exception propagates out
264 * of the passed fiber; this function then does not return.
265 *
266 * Parameters are passed to rb_make_exception() to create an exception object.
267 * See its document for what are allowed here.
268 *
269 * It is a failure to call this function against a fiber which is resuming,
270 * have never run yet, or has already finished running.
271 *
272 * @param[out] fiber Where exception is raised.
273 * @param[in] argc Passed as-is to rb_make_exception().
274 * @param[in] argv Passed as-is to rb_make_exception().
275 * @exception rb_eFiberError `fiber` is terminated etc.
276 * @return (See rb_fiber_resume() for details)
277 */
278 VALUE rb_fiber_raise(VALUE fiber, int argc, const VALUE *argv);
279
280 RBIMPL_SYMBOL_EXPORT_END()
281
282 #endif /* RBIMPL_INTERN_CONT_H */