Ruby 3.2.2p53 (2023-03-30 revision e51014f9c05aa65cbf203442d37fef7c12390015)
|
#include "ruby/internal/attr/nonnull.h"
#include "ruby/internal/intern/thread.h"
#include "ruby/internal/dllexport.h"
Go to the source code of this file.
Macros | |
#define | RUBY_CALL_WO_GVL_FLAG_SKIP_CHECK_INTS_AFTER 0x01 |
#define | RUBY_CALL_WO_GVL_FLAG_SKIP_CHECK_INTS_ |
#define | RUBY_INTERNAL_THREAD_EVENT_STARTED 1 << 0 /** thread started */ |
#define | RUBY_INTERNAL_THREAD_EVENT_READY 1 << 1 /** acquiring GVL */ |
#define | RUBY_INTERNAL_THREAD_EVENT_RESUMED 1 << 2 /** acquired GVL */ |
#define | RUBY_INTERNAL_THREAD_EVENT_SUSPENDED 1 << 3 /** released GVL */ |
#define | RUBY_INTERNAL_THREAD_EVENT_EXITED 1 << 4 /** thread terminated */ |
#define | RUBY_INTERNAL_THREAD_EVENT_MASK 0xff /** All Thread events */ |
Flags for rb_nogvl() | |
#define | RB_NOGVL_INTR_FAIL (0x1) |
Passing this flag to rb_nogvl() prevents it from checking interrupts. | |
#define | RB_NOGVL_UBF_ASYNC_SAFE (0x2) |
Passing this flag to rb_nogvl() indicates that the passed UBF is async-signal-safe. | |
Typedefs | |
typedef void | rb_internal_thread_event_data_t |
typedef void(* | rb_internal_thread_event_callback) (rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *user_data) |
typedef struct rb_internal_thread_event_hook | rb_internal_thread_event_hook_t |
Functions | |
void * | rb_thread_call_with_gvl (void *(*func)(void *), void *data1) |
(Re-)acquires the GVL. | |
void * | rb_thread_call_without_gvl (void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2) |
Allows the passed function to run in parallel with other Ruby threads. | |
void * | rb_thread_call_without_gvl2 (void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2) |
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc. | |
void * | rb_nogvl (void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags) |
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behaviour. | |
rb_internal_thread_event_hook_t * | rb_internal_thread_add_event_hook (rb_internal_thread_event_callback func, rb_event_flag_t events, void *data) |
Registers a thread event hook function. | |
bool | rb_internal_thread_remove_event_hook (rb_internal_thread_event_hook_t *hook) |
Unregister the passed hook. | |
Definition in file thread.h.
#define RB_NOGVL_INTR_FAIL (0x1) |
Passing this flag to rb_nogvl() prevents it from checking interrupts.
Interrupts can impact your program negatively. For instance consider following callback function:
Here, if it gets interrupted at (a) or (b), read(2)
is cancelled and this function leaks memory (which is not a good thing of course, but...). But if it gets interrupted at (c), where read(2)
is already done, interruption is way more catastrophic because what was read gets lost. To reroute this kind of problem you should set this flag. And check interrupts elsewhere at your own risk.
#define RB_NOGVL_UBF_ASYNC_SAFE (0x2) |
Passing this flag to rb_nogvl() indicates that the passed UBF is async-signal-safe.
An UBF could be async safe, and that makes things simpler. However async unsafe UBFs are just okay. If unsure, you can safely leave it unspecified.
#define RUBY_CALL_WO_GVL_FLAG_SKIP_CHECK_INTS_ |
#define RUBY_CALL_WO_GVL_FLAG_SKIP_CHECK_INTS_AFTER 0x01 |
#define RUBY_INTERNAL_THREAD_EVENT_EXITED 1 << 4 /** thread terminated */ |
#define RUBY_INTERNAL_THREAD_EVENT_MASK 0xff /** All Thread events */ |
#define RUBY_INTERNAL_THREAD_EVENT_READY 1 << 1 /** acquiring GVL */ |
#define RUBY_INTERNAL_THREAD_EVENT_RESUMED 1 << 2 /** acquired GVL */ |
#define RUBY_INTERNAL_THREAD_EVENT_STARTED 1 << 0 /** thread started */ |
#define RUBY_INTERNAL_THREAD_EVENT_SUSPENDED 1 << 3 /** released GVL */ |
typedef void(* rb_internal_thread_event_callback) (rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *user_data) |
typedef struct rb_internal_thread_event_hook rb_internal_thread_event_hook_t |
rb_internal_thread_event_hook_t * rb_internal_thread_add_event_hook | ( | rb_internal_thread_event_callback | func, |
rb_event_flag_t | events, | ||
void * | data | ||
) |
Registers a thread event hook function.
[in] | func | A callback. |
[in] | events | A set of events that func should run. |
[in] | data | Passed as-is to func . |
bool rb_internal_thread_remove_event_hook | ( | rb_internal_thread_event_hook_t * | hook | ) |
Unregister the passed hook.
[in] | hook. | The hook to unregister. |
void * rb_nogvl | ( | void *(*)(void *) | func, |
void * | data1, | ||
rb_unblock_function_t * | ubf, | ||
void * | data2, | ||
int | flags | ||
) |
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behaviour.
[in] | func | A function to call without GVL. |
[in,out] | data1 | Passed as-is to func . |
[in] | ubf | An UBF to cancel func . |
[in,out] | data2 | Passed as-is to ubf . |
[in] | flags | Flags. |
func
returned, or 0 in case func
did not return. Definition at line 1523 of file thread.c.
Referenced by rb_thread_call_without_gvl2().
void * rb_thread_call_with_gvl | ( | void *(*)(void *) | func, |
void * | data1 | ||
) |
(Re-)acquires the GVL.
This manoeuvre makes it possible for an out-of-GVL routine to one-shot call a ruby method.
What this function does:
[in] | func | What to call with GVL. |
[in,out] | data1 | Passed as-is to func . |
func
. func
must not return a Ruby object. If it did such return value would escape from GC's scope; would not be marked. func
does not raise, by properly rescuing everything using e.g. rb_protect(). Definition at line 1756 of file thread.c.
Referenced by rb_thread_call_with_gvl().
void * rb_thread_call_without_gvl | ( | void *(*)(void *) | func, |
void * | data1, | ||
rb_unblock_function_t * | ubf, | ||
void * | data2 | ||
) |
Allows the passed function to run in parallel with other Ruby threads.
What this function does:
In case other threads interfaced with this thread using rb_thread_kill() etc., the passed UBF is additionally called. See rb_unblock_function_t for details.
Unlike rb_thread_call_without_gvl2() this function also reacts to signals etc.
[in] | func | A function to call without GVL. |
[in,out] | data1 | Passed as-is to func . |
[in] | ubf | An UBF to cancel func . |
[in,out] | data2 | Passed as-is to ubf . |
func
returned, or 0 in case ubf
cancelled func
. func
, it might be faster to just call func
with blocking everything else. Be sure to benchmark your code to see if it is actually worth releasing the GVL. void * rb_thread_call_without_gvl2 | ( | void *(*)(void *) | func, |
void * | data1, | ||
rb_unblock_function_t * | ubf, | ||
void * | data2 | ||
) |
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc.
As described in RB_NOGVL_INTR_FAIL, interrupts can hurt you. In case this function detects an interrupt, it returns immediately. You can record progress of your callback and check it after returning from this function.
What this function does:
[in] | func | A function to call without GVL. |
[in,out] | data1 | Passed as-is to func . |
[in] | ubf | An UBF to cancel func . |
[in,out] | data2 | Passed as-is to ubf . |
func
returned, or 0 in case func
did not return.