|
Ruby 4.0.7p0 (2026-09-15 revision 229531a6cfbf07e3caef30dbac24a2a3f3fed482)
|
Public APIs related to rb_cThread. More...
#include "ruby/internal/attr/nonnull.h"#include "ruby/internal/cast.h"#include "ruby/internal/config.h"#include "ruby/internal/dllexport.h"#include "ruby/internal/value.h"Go to the source code of this file.
Macros | |
| #define | RUBY_UBF_IO RBIMPL_CAST((rb_unblock_function_t *)-1) |
| A special UBF for blocking IO operations. | |
| #define | RUBY_UBF_PROCESS RBIMPL_CAST((rb_unblock_function_t *)-1) |
| A special UBF for blocking process operations. | |
Typedefs | |
| typedef void | rb_unblock_function_t(void *) |
| This is the type of UBFs. | |
Functions | |
| void | rb_thread_schedule (void) |
| Tries to switch to another thread. | |
| int | rb_thread_wait_fd (int fd) |
| Blocks the current thread until the given file descriptor is ready to be read. | |
| int | rb_thread_fd_writable (int fd) |
| Identical to rb_thread_wait_fd(), except it blocks the current thread until the given file descriptor is ready to be written. | |
| void | rb_thread_fd_close (int fd) |
| This funciton is now a no-op. | |
| int | rb_thread_alone (void) |
| Checks if the thread this function is running is the only thread that is currently alive. | |
| void | rb_thread_sleep (int sec) |
| Blocks for the given period of time. | |
| void | rb_thread_sleep_forever (void) |
| Blocks indefinitely. | |
| void | rb_thread_sleep_deadly (void) |
| Identical to rb_thread_sleep_forever(), except the thread calling this function is considered "dead" when our deadlock checker is triggered. | |
| VALUE | rb_thread_stop (void) |
| Stops the current thread. | |
| VALUE | rb_thread_wakeup (VALUE thread) |
| Marks a given thread as eligible for scheduling. | |
| VALUE | rb_thread_wakeup_alive (VALUE thread) |
| Identical to rb_thread_wakeup(), except it doesn't raise on an already killed thread. | |
| VALUE | rb_thread_run (VALUE thread) |
| This is a rb_thread_wakeup() + rb_thread_schedule() combo. | |
| VALUE | rb_thread_kill (VALUE thread) |
| Terminates the given thread. | |
| VALUE | rb_thread_create (VALUE(*f)(void *g), void *g) |
| Creates a Ruby thread that is backended by a C function. | |
| void | rb_thread_wait_for (struct timeval time) |
| Identical to rb_thread_sleep(), except it takes struct timeval instead. | |
| VALUE | rb_thread_current (void) |
| Obtains the "current" thread. | |
| VALUE | rb_thread_main (void) |
| Obtains the "main" thread. | |
| VALUE | rb_thread_local_aref (VALUE thread, ID key) |
| This badly named function reads from a Fiber local storage. | |
| VALUE | rb_thread_local_aset (VALUE thread, ID key, VALUE val) |
| This badly named function writes to a Fiber local storage. | |
| void | rb_thread_atfork (void) |
| A pthread_atfork(3posix)-like API. | |
| void | rb_thread_atfork_before_exec (void) |
| :FIXME: situation of this function is unclear. | |
| VALUE | rb_exec_recursive (VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h) |
| "Recursion" API entry point. | |
| VALUE | rb_exec_recursive_paired (VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h) |
| Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g, p } instead of just g. | |
| VALUE | rb_exec_recursive_outer (VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h) |
| Identical to rb_exec_recursive(), except it calls f for outermost recursion only. | |
| VALUE | rb_exec_recursive_paired_outer (VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h) |
| Identical to rb_exec_recursive_outer(), except it checks for the recursion on the ordered pair of { g, p } instead of just g. | |
| void | rb_thread_check_ints (void) |
| Checks for interrupts. | |
| int | rb_thread_interrupted (VALUE thval) |
| Checks if the thread's execution was recently interrupted. | |
| VALUE | rb_mutex_new (void) |
| Creates a mutex. | |
| VALUE | rb_mutex_locked_p (VALUE mutex) |
| Queries if there are any threads that holds the lock. | |
| VALUE | rb_mutex_trylock (VALUE mutex) |
| Attempts to lock the mutex, without waiting for other threads to unlock it. | |
| VALUE | rb_mutex_lock (VALUE mutex) |
| Attempts to lock the mutex. | |
| VALUE | rb_mutex_unlock (VALUE mutex) |
| Releases the mutex. | |
| VALUE | rb_mutex_sleep (VALUE self, VALUE timeout) |
| Releases the lock held in the mutex and waits for the period of time; reacquires the lock on wakeup. | |
| VALUE | rb_mutex_synchronize (VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg) |
| Obtains the lock, runs the passed function, and releases the lock when it completes. | |
Public APIs related to rb_cThread.
Definition in file thread.h.
| #define RUBY_UBF_IO RBIMPL_CAST((rb_unblock_function_t *)-1) |
| #define RUBY_UBF_PROCESS RBIMPL_CAST((rb_unblock_function_t *)-1) |
| typedef void rb_unblock_function_t(void *) |
This is the type of UBFs.
An UBF is a function that unblocks a blocking region. For instance when a thread is blocking due to pselect(3posix), it is highly expected that pthread_kill(3posix) can interrupt the system call and the thread could revive. Or when a thread is blocking due to waitpid(3posix), it is highly expected that killing the waited process should suffice. An UBF is a function that does such things. Designing your own UBF needs deep understanding of why your blocking region blocks, how threads work in ruby, and a matter of luck. It often is the case you simply cannot cancel something that had already begun.
"Recursion" API entry point.
This basically calls the given function with the given arguments, but additionally with recursion flag. The flag is set to 1 if the execution have already experienced the passed g parameter before.
| [in] | f | The function that possibly recurs. |
| [in,out] | g | Passed as-is to f. |
| [in,out] | h | Passed as-is to f. |
Definition at line 5607 of file thread.c.
Referenced by rb_error_frozen_object(), rb_exec_recursive(), and rb_io_puts().
Identical to rb_exec_recursive(), except it calls f for outermost recursion only.
Inner recursions yield calls to rb_throw_obj().
| [in] | f | The function that possibly recurs. |
| [in,out] | g | Passed as-is to f. |
| [in,out] | h | Passed as-is to f. |
Definition at line 5630 of file thread.c.
Referenced by rb_exec_recursive_outer().
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g, p } instead of just g.
| [in] | f | The function that possibly recurs. |
| [in,out] | g | Passed as-is to f. |
| [in] | p | Paired object for recursion detection. |
| [in,out] | h | Passed as-is to f. |
Definition at line 5618 of file thread.c.
Referenced by rb_exec_recursive_paired(), and rb_num_coerce_bit().
| VALUE rb_exec_recursive_paired_outer | ( | VALUE(* | f )(VALUE g, VALUE h, int r), |
| VALUE | g, | ||
| VALUE | p, | ||
| VALUE | h ) |
Identical to rb_exec_recursive_outer(), except it checks for the recursion on the ordered pair of { g, p } instead of just g.
It can also be seen as a routine identical to rb_exec_recursive_paired(), except it calls f for outermost recursion only. Inner recursions yield calls to rb_throw_obj().
| [in] | f | The function that possibly recurs. |
| [in,out] | g | Passed as-is to f. |
| [in] | p | Paired object for recursion detection. |
| [in,out] | h | Passed as-is to f. |
Definition at line 5648 of file thread.c.
Referenced by rb_exec_recursive_paired_outer().
Attempts to lock the mutex.
It waits until the mutex gets available.
| [out] | mutex | The mutex to lock. |
| rb_eThreadError | Recursive deadlock situation. |
Definition at line 450 of file thread_sync.c.
Referenced by rb_mutex_lock().
Queries if there are any threads that holds the lock.
| [in] | mutex | The mutex in question. |
| RUBY_Qtrue | The mutex is locked by someone. |
| RUBY_Qfalse | The mutex is not locked by anyone. |
Definition at line 190 of file thread_sync.c.
Referenced by rb_mutex_locked_p().
| VALUE rb_mutex_new | ( | void | ) |
Creates a mutex.
Definition at line 184 of file thread_sync.c.
Referenced by rb_mutex_new().
Releases the lock held in the mutex and waits for the period of time; reacquires the lock on wakeup.
| [out] | self | The target mutex. |
| [in] | timeout | Duration, in seconds, in rb_cNumeric. |
| rb_eArgError | timeout is negative. |
| rb_eRangeError | timeout is out of range of time_t. |
| rb_eThreadError | The mutex is not owned by the current thread. |
Definition at line 643 of file thread_sync.c.
Referenced by rb_mutex_sleep().
Obtains the lock, runs the passed function, and releases the lock when it completes.
| [out] | mutex | The mutex to lock. |
| [in] | func | What to do during the mutex is locked. |
| [in,out] | arg | Passed as-is to func. |
Definition at line 649 of file thread_sync.c.
Referenced by rb_autoload_load(), and rb_mutex_synchronize().
Attempts to lock the mutex, without waiting for other threads to unlock it.
Failure in locking the mutex can be detected by the return value.
| [out] | mutex | The mutex to lock. |
| RUBY_Qtrue | Successfully locked by the current thread. |
| RUBY_Qfalse | Otherwise. |
Definition at line 260 of file thread_sync.c.
Referenced by rb_mutex_trylock().
Releases the mutex.
| [out] | mutex | The mutex to unlock. |
| rb_eThreadError | The mutex is not owned by the current thread. |
Definition at line 533 of file thread_sync.c.
Referenced by rb_mutex_unlock().
| int rb_thread_alone | ( | void | ) |
Checks if the thread this function is running is the only thread that is currently alive.
| 1 | Yes it is. |
| 0 | No it isn't. |
Definition at line 4079 of file thread.c.
Referenced by rb_thread_alone(), and rb_thread_stop().
| void rb_thread_atfork | ( | void | ) |
A pthread_atfork(3posix)-like API.
Ruby expects its child processes to call this function at the very beginning of their processes. If you plan to fork a process don't forget to call it.
Definition at line 5077 of file thread.c.
Referenced by rb_thread_atfork().
| void rb_thread_atfork_before_exec | ( | void | ) |
:FIXME: situation of this function is unclear.
It seems nobody uses it. Maybe a good idea to KonMari.
Definition at line 5082 of file thread.c.
Referenced by rb_thread_atfork_before_exec().
| void rb_thread_check_ints | ( | void | ) |
Checks for interrupts.
In ruby, signals are masked by default. You can call this function at will to check if there are pending signals. In case there are, they would be handled in this function.
If your extension library has a function that takes a long time, consider calling it periodically.
Definition at line 1466 of file thread.c.
Referenced by rb_io_maybe_wait(), rb_io_wait_readable(), rb_io_wait_writable(), and rb_thread_check_ints().
Creates a Ruby thread that is backended by a C function.
| [in] | f | The function to run on a thread. |
| [in,out] | g | Passed through to f. |
| rb_eThreadError | Could not create a ruby thread. |
| rb_eSystemCallError | Situations like EPERM. |
| VALUE rb_thread_current | ( | void | ) |
Obtains the "current" thread.
Definition at line 3217 of file thread.c.
Referenced by rb_f_kill(), and rb_thread_current().
| void rb_thread_fd_close | ( | int | fd | ) |
This funciton is now a no-op.
It was previously used to interrupt threads that were using the given file descriptor and wait for them to finish.
| [in] | fd | A file descriptor. |
Definition at line 2935 of file thread.c.
Referenced by rb_thread_fd_close().
| int rb_thread_fd_writable | ( | int | fd | ) |
Identical to rb_thread_wait_fd(), except it blocks the current thread until the given file descriptor is ready to be written.
| [in] | fd | A file descriptor. |
| rb_eIOError | Closed stream. |
| rb_eSystemCallError | Situations like EBADF. |
Definition at line 1635 of file io.c.
Referenced by rb_thread_fd_writable().
| int rb_thread_interrupted | ( | VALUE | thval | ) |
Checks if the thread's execution was recently interrupted.
If called from that thread, this function can be used to detect spurious wake-ups.
| [in] | thval | Thread in question. |
| 0 | The thread was not interrupted. |
| otherwise | The thread was interrupted recently. |
Definition at line 1483 of file thread.c.
Referenced by rb_thread_interrupted().
Terminates the given thread.
Unlike a stopped thread, a killed thread could never be revived. This function does return, when passed e.g. an already killed thread. But if the passed thread is the only one, or a special thread called "main", then it also terminates the entire process.
| [out] | thread | The thread to terminate. |
| rb_eFatal | The passed thread is the running thread. |
| rb_eSystemExit | The passed thread is the last thread. |
Definition at line 2999 of file thread.c.
Referenced by rb_thread_kill().
This badly named function reads from a Fiber local storage.
When this function was born there was no such thing like a Fiber. The world was innocent. But now... This is a Fiber local storage. Sorry.
| [in] | thread | Thread that the target Fiber is running. |
| [in] | key | The name of the Fiber local storage to read. |
| RUBY_Qnil | No such storage. |
| otherwise | The value stored at key. |
Definition at line 3806 of file thread.c.
Referenced by rb_thread_local_aref().
This badly named function writes to a Fiber local storage.
When this function was born there was no such thing like a Fiber. The world was innocent. But now... This is a Fiber local storage. Sorry.
| [in] | thread | Thread that the target Fiber is running. |
| [in] | key | The name of the Fiber local storage to write. |
| [in] | val | The new value of the storage. |
| rb_eFrozenError | thread is frozen. |
Definition at line 3954 of file thread.c.
Referenced by rb_detach_process(), and rb_thread_local_aset().
| VALUE rb_thread_main | ( | void | ) |
Obtains the "main" thread.
There are threads called main. Historically the (only) main thread was the one which runs when the process boots. Now that we have Ractor, there are more than one main threads.
Definition at line 3238 of file thread.c.
Referenced by rb_thread_main().
This is a rb_thread_wakeup() + rb_thread_schedule() combo.
| [out] | thread | Thread in question to wake up. |
| rb_eThreadError | Stop flogging a dead horse. |
Definition at line 3141 of file thread.c.
Referenced by rb_thread_run().
| void rb_thread_schedule | ( | void | ) |
Tries to switch to another thread.
This function blocks until the current thread re-acquires the GVL.
| rb_eInterrupt | Operation interrupted. |
Definition at line 1514 of file thread.c.
Referenced by rb_thread_run(), and rb_thread_schedule().
| void rb_thread_sleep | ( | int | sec | ) |
Blocks for the given period of time.
| [in] | sec | Duration in seconds. |
| rb_eInterrupt | Interrupted. |
Definition at line 1489 of file thread.c.
Referenced by rb_thread_sleep().
| void rb_thread_sleep_deadly | ( | void | ) |
Identical to rb_thread_sleep_forever(), except the thread calling this function is considered "dead" when our deadlock checker is triggered.
| rb_eInterrupt | Interrupted. |
Definition at line 1419 of file thread.c.
Referenced by rb_thread_sleep_deadly(), and rb_thread_stop().
| void rb_thread_sleep_forever | ( | void | ) |
Blocks indefinitely.
| rb_eInterrupt | Interrupted. |
Definition at line 1412 of file thread.c.
Referenced by rb_thread_fd_select(), and rb_thread_sleep_forever().
| VALUE rb_thread_stop | ( | void | ) |
Stops the current thread.
This is not the end of the thread's lifecycle. A stopped thread can later be woken up.
| rb_eThreadError | Stopping this thread would deadlock. |
| RUBY_Qnil | Always. |
Definition at line 3150 of file thread.c.
Referenced by rb_thread_stop().
| int rb_thread_wait_fd | ( | int | fd | ) |
Blocks the current thread until the given file descriptor is ready to be read.
| [in] | fd | A file descriptor. |
| rb_eIOError | Closed stream. |
| rb_eSystemCallError | Situations like EBADF. |
Definition at line 1629 of file io.c.
Referenced by rb_thread_wait_fd().
| void rb_thread_wait_for | ( | struct timeval | time | ) |
Identical to rb_thread_sleep(), except it takes struct timeval instead.
| [in] | time | Duration. |
| rb_eInterrupt | Interrupted. |
Definition at line 1445 of file thread.c.
Referenced by rb_thread_fd_select(), rb_thread_sleep(), and rb_thread_wait_for().
Marks a given thread as eligible for scheduling.
| [out] | thread | Thread in question to wake up. |
| rb_eThreadError | Stop flogging a dead horse. |
Definition at line 3094 of file thread.c.
Referenced by rb_thread_run(), and rb_thread_wakeup().
Identical to rb_thread_wakeup(), except it doesn't raise on an already killed thread.
| [out] | thread | A thread to wake up. |
| RUBY_Qnil | thread is already killed. |
| otherwise | thread is alive. |
Definition at line 3103 of file thread.c.
Referenced by rb_thread_wakeup(), and rb_thread_wakeup_alive().