Ruby 4.1.0dev (2026-10-26 revision 29264f878a52d2c36c75bfca6d04adaefeacbb30)
thread.h
1#ifndef INTERNAL_THREAD_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_THREAD_H
11#include "ruby/ruby.h" /* for VALUE */
12#include "ruby/intern.h" /* for rb_blocking_function_t */
13#include "ccan/list/list.h" /* for list in rb_io_close_wait_list */
14
15struct rb_execution_context_struct; /* in vm_core.h */
16struct rb_gc_mark_ctx; /* in internal/gc.h */
17struct rb_thread_struct; /* in vm_core.h */
18struct rb_io;
19
20#define RB_VM_SAVE_MACHINE_CONTEXT(th) \
21 do { \
22 FLUSH_REGISTER_WINDOWS; \
23 setjmp((th)->ec->machine.regs); \
24 SET_MACHINE_STACK_END(&(th)->ec->machine.stack_end); \
25 } while (0)
26
27#define RUBY_FATAL_THREAD_KILLED INT2FIX(0)
28#define RUBY_FATAL_THREAD_TERMINATED INT2FIX(1)
29#define RUBY_FATAL_FIBER_KILLED RB_INT2FIX(2)
30
31VALUE rb_obj_is_mutex(VALUE obj);
32VALUE rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg);
33void rb_thread_execute_interrupts(VALUE th);
34VALUE rb_thread_shield_new(void);
35bool rb_thread_shield_owned(VALUE self);
36VALUE rb_thread_shield_wait(VALUE self);
37VALUE rb_thread_shield_release(VALUE self);
38VALUE rb_thread_shield_destroy(VALUE self);
39int rb_thread_to_be_killed(VALUE thread);
40void rb_thread_acquire_fork_lock(void);
41void rb_thread_release_fork_lock(void);
42void rb_thread_reset_fork_lock(void);
43void rb_mutex_allow_trap(VALUE self, int val);
44VALUE rb_uninterruptible(VALUE (*b_proc)(VALUE), VALUE data);
45VALUE rb_mutex_owned_p(VALUE self);
46VALUE rb_exec_recursive_outer_mid(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h, ID mid);
47void ruby_mn_threads_params(void);
48
49int rb_thread_io_wait(struct rb_thread_struct *th, struct rb_io *io, int events, struct timeval * timeout);
50int rb_thread_wait_for_single_fd(struct rb_thread_struct *th, int fd, int events, struct timeval * timeout);
51
52size_t rb_thread_io_close_interrupt(struct rb_io *);
53void rb_thread_io_close_wait(struct rb_io *);
54
55void rb_ec_check_ints(struct rb_execution_context_struct *ec);
56
57void rb_thread_free_native_thread(void *th_ptr);
58
59bool rb_thread_event_hooks_registered_p(void); /* thread_pthread.c */
60void rb_thread_free_body(void *th); /* vm.c */
61
62RUBY_SYMBOL_EXPORT_BEGIN
63
64void *rb_thread_prevent_fork(void *(*func)(void *), void *data); /* for ext/socket/raddrinfo.c */
65void rb_thread_register_exiting(void); /* for ext/socket/raddrinfo.c */
66
67/* Temporary. This API will be removed (renamed). */
68VALUE rb_thread_io_blocking_region(struct rb_io *io, rb_blocking_function_t *func, void *data1);
69VALUE rb_thread_io_blocking_call(struct rb_io *io, rb_blocking_function_t *func, void *data1, int events);
70
71// Invoke the given function, with the specified argument, in a way that `IO#close` from another execution context can interrupt it.
72VALUE rb_thread_io_blocking_operation(VALUE self, VALUE(*function)(VALUE), VALUE argument);
73
74/* thread.c (export) */
75int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */
76
77RUBY_SYMBOL_EXPORT_END
78
79int rb_threadptr_execute_interrupts(struct rb_thread_struct *th, int blocking_timing);
80bool rb_thread_mn_schedulable(VALUE thread);
81
82bool rb_thread_resolve_unblock_function(rb_unblock_function_t **unblock_function, void **data2, struct rb_thread_struct *thread);
83
84// interrupt exec
85
86typedef VALUE (rb_interrupt_exec_func_t)(void *data);
87
88enum rb_interrupt_exec_flag {
89 rb_interrupt_exec_flag_none = 0x00,
90 rb_interrupt_exec_flag_value_data = 0x01,
91 rb_interrupt_exec_flag_new_thread = 0x02,
92};
93
94// interrupt the target_th and run func.
95struct rb_ractor_struct;
96
97void rb_threadptr_interrupt_exec(struct rb_thread_struct *target_th,
98 rb_interrupt_exec_func_t *func, void *data, enum rb_interrupt_exec_flag flags);
99
100// create a thread in the target_r and run func on the created thread.
101void rb_ractor_interrupt_exec(struct rb_ractor_struct *target_r,
102 rb_interrupt_exec_func_t *func, void *data, enum rb_interrupt_exec_flag flags);
103
104void rb_threadptr_interrupt_exec_task_mark(struct rb_thread_struct *th);
105
106/* Mark the roots of the heap objects a thread owns, excluding ec and fiber. Used by
107 * thread_mark and by the Ractor's local-root marking (rb_ractor_mark_local_roots). */
108void rb_thread_mark_owned_roots(const struct rb_gc_mark_ctx *ctx, struct rb_thread_struct *th);
109
110#endif /* INTERNAL_THREAD_H */
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition thread.h:336
Ruby's IO, metadata and buffers.
Definition io.h:295
VALUE self
The IO's Ruby level counterpart.
Definition io.h:298
VALUE timeout
The timeout associated with this IO when performing blocking operations.
Definition io.h:406
int fd
file descriptor.
Definition io.h:306
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40