Ruby: include/ruby/internal/intern/thread.h Source File

Ruby 3.4.6p54 (2025-09-16 revision dbd83256b1cec76c69756ecb8758b9e1079833de)

thread.h
Go to the documentation of this file.
1#ifndef RBIMPL_INTERN_THREAD_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_INTERN_THREAD_H
23#include "ruby/internal/attr/nonnull.h"
24#include "ruby/internal/cast.h"
25#include "ruby/internal/config.h"
26#include "ruby/internal/dllexport.h"
27#include "ruby/internal/value.h"
28
29 RBIMPL_SYMBOL_EXPORT_BEGIN()
30
31struct timeval;
32
33/* thread.c */
34
41void rb_thread_schedule(void);
42
51int rb_thread_wait_fd(int fd);
52
61int rb_thread_fd_writable(int fd);
62
73void rb_thread_fd_close(int fd);
74
86int rb_thread_alone(void);
87
95void rb_thread_sleep(int sec);
96
102void rb_thread_sleep_forever(void);
103
110void rb_thread_sleep_deadly(void);
111
123 VALUE rb_thread_stop(void);
124
136 VALUE rb_thread_wakeup(VALUE thread);
137
147 VALUE rb_thread_wakeup_alive(VALUE thread);
148
158 VALUE rb_thread_run(VALUE thread);
159
177 VALUE rb_thread_kill(VALUE thread);
178
179 RBIMPL_ATTR_NONNULL((1))
190 VALUE rb_thread_create(VALUE (*f)(void *g), void *g);
191
199void rb_thread_wait_for(struct timeval time);
200
208 VALUE rb_thread_current(void);
209
219 VALUE rb_thread_main(void);
220
233 VALUE rb_thread_local_aref(VALUE thread, ID key);
234
249 VALUE rb_thread_local_aset(VALUE thread, ID key, VALUE val);
250
256void rb_thread_atfork(void);
257
262void rb_thread_atfork_before_exec(void);
263
275 VALUE rb_exec_recursive(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h);
276
286 VALUE rb_exec_recursive_paired(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h);
287
302 VALUE rb_exec_recursive_outer(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h);
303
321 VALUE rb_exec_recursive_paired_outer(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h);
322
336typedef void rb_unblock_function_t(void *);
337
348typedef VALUE rb_blocking_function_t(void *);
349
360void rb_thread_check_ints(void);
361
375int rb_thread_interrupted(VALUE thval);
376
382#define RUBY_UBF_IO RBIMPL_CAST((rb_unblock_function_t *)-1)
383
389#define RUBY_UBF_PROCESS RBIMPL_CAST((rb_unblock_function_t *)-1)
390
391/* thread_sync.c */
392
398 VALUE rb_mutex_new(void);
399
407 VALUE rb_mutex_locked_p(VALUE mutex);
408
419 VALUE rb_mutex_trylock(VALUE mutex);
420
429 VALUE rb_mutex_lock(VALUE mutex);
430
440 VALUE rb_mutex_unlock(VALUE mutex);
441
478 VALUE rb_mutex_sleep(VALUE self, VALUE timeout);
479
488 VALUE rb_mutex_synchronize(VALUE mutex, VALUE (*func)(VALUE arg), VALUE arg);
489
490 RBIMPL_SYMBOL_EXPORT_END()
491
492#endif /* RBIMPL_INTERN_THREAD_H */
dllexport.h
Tweaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
rb_thread_interrupted
int rb_thread_interrupted(VALUE thval)
Checks if the thread's execution was recently interrupted.
Definition thread.c:1451
rb_thread_local_aref
VALUE rb_thread_local_aref(VALUE thread, ID key)
This badly named function reads from a Fiber local storage.
Definition thread.c:3573
rb_mutex_new
VALUE rb_mutex_new(void)
Creates a mutex.
Definition thread_sync.c:191
rb_thread_fd_writable
int rb_thread_fd_writable(int fd)
Identical to rb_thread_wait_fd(), except it blocks the current thread until the given file descriptor...
Definition io.c:1603
rb_thread_kill
VALUE rb_thread_kill(VALUE thread)
Terminates the given thread.
Definition thread.c:2766
rb_thread_main
VALUE rb_thread_main(void)
Obtains the "main" thread.
Definition thread.c:3005
rb_exec_recursive
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
Definition thread.c:5332
rb_thread_sleep_forever
void rb_thread_sleep_forever(void)
Blocks indefinitely.
Definition thread.c:1381
rb_thread_fd_close
void rb_thread_fd_close(int fd)
Notifies a closing of a file descriptor to other threads.
Definition thread.c:2707
rb_thread_wait_for
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
Definition thread.c:1413
rb_mutex_trylock
VALUE rb_mutex_trylock(VALUE mutex)
Attempts to lock the mutex, without waiting for other threads to unlock it.
Definition thread_sync.c:252
rb_mutex_locked_p
VALUE rb_mutex_locked_p(VALUE mutex)
Queries if there are any threads that holds the lock.
Definition thread_sync.c:203
rb_mutex_synchronize
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.
Definition thread_sync.c:640
rb_thread_stop
VALUE rb_thread_stop(void)
Stops the current thread.
Definition thread.c:2917
rb_mutex_sleep
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.
Definition thread_sync.c:583
rb_exec_recursive_paired
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,...
Definition thread.c:5343
rb_unblock_function_t
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition thread.h:336
rb_thread_atfork_before_exec
void rb_thread_atfork_before_exec(void)
:FIXME: situation of this function is unclear.
Definition thread.c:4807
rb_thread_create
VALUE rb_thread_create(VALUE(*f)(void *g), void *g)
Creates a Ruby thread that is backended by a C function.
Definition thread.c:987
rb_thread_check_ints
void rb_thread_check_ints(void)
Checks for interrupts.
Definition thread.c:1434
rb_thread_run
VALUE rb_thread_run(VALUE thread)
This is a rb_thread_wakeup() + rb_thread_schedule() combo.
Definition thread.c:2908
rb_thread_wakeup
VALUE rb_thread_wakeup(VALUE thread)
Marks a given thread as eligible for scheduling.
Definition thread.c:2861
rb_mutex_unlock
VALUE rb_mutex_unlock(VALUE mutex)
Releases the mutex.
Definition thread_sync.c:505
rb_exec_recursive_paired_outer
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...
Definition thread.c:5373
rb_thread_sleep_deadly
void rb_thread_sleep_deadly(void)
Identical to rb_thread_sleep_forever(), except the thread calling this function is considered "dead" ...
Definition thread.c:1388
rb_thread_atfork
void rb_thread_atfork(void)
A pthread_atfork(3posix)-like API.
Definition thread.c:4802
rb_thread_current
VALUE rb_thread_current(void)
Obtains the "current" thread.
Definition thread.c:2984
rb_thread_alone
int rb_thread_alone(void)
Checks if the thread this function is running is the only thread that is currently alive.
Definition thread.c:3846
rb_thread_wait_fd
int rb_thread_wait_fd(int fd)
Blocks the current thread until the given file descriptor is ready to be read.
Definition io.c:1597
rb_thread_local_aset
VALUE rb_thread_local_aset(VALUE thread, ID key, VALUE val)
This badly named function writes to a Fiber local storage.
Definition thread.c:3721
rb_thread_schedule
void rb_thread_schedule(void)
Tries to switch to another thread.
Definition thread.c:1482
rb_exec_recursive_outer
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.
Definition thread.c:5355
rb_thread_wakeup_alive
VALUE rb_thread_wakeup_alive(VALUE thread)
Identical to rb_thread_wakeup(), except it doesn't raise on an already killed thread.
Definition thread.c:2870
rb_mutex_lock
VALUE rb_mutex_lock(VALUE mutex)
Attempts to lock the mutex.
Definition thread_sync.c:432
rb_thread_sleep
void rb_thread_sleep(int sec)
Blocks for the given period of time.
Definition thread.c:1457
nonnull.h
Defines RBIMPL_ATTR_NONNULL.
RBIMPL_ATTR_NONNULL
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
timeval
Definition missing.h:63
value.h
Defines VALUE and ID.
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40