Ruby  3.4.0dev (2024-11-22 revision 0989400a925cd201defdca9eb28eb87200b30785)
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
24 #include "ruby/internal/cast.h"
25 #include "ruby/internal/config.h"
27 #include "ruby/internal/value.h"
28 
30 
31 struct timeval;
32 
33 /* thread.c */
34 
41 void rb_thread_schedule(void);
42 
51 int rb_thread_wait_fd(int fd);
52 
61 int rb_thread_fd_writable(int fd);
62 
73 void rb_thread_fd_close(int fd);
74 
86 int rb_thread_alone(void);
87 
95 void rb_thread_sleep(int sec);
96 
102 void rb_thread_sleep_forever(void);
103 
110 void rb_thread_sleep_deadly(void);
111 
123 VALUE rb_thread_stop(void);
124 
137 
148 
158 VALUE rb_thread_run(VALUE thread);
159 
177 VALUE rb_thread_kill(VALUE thread);
178 
190 VALUE rb_thread_create(VALUE (*f)(void *g), void *g);
191 
199 void rb_thread_wait_for(struct timeval time);
200 
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 
256 void rb_thread_atfork(void);
257 
263 
275 VALUE rb_exec_recursive(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h);
276 
287 
303 
322 
336 typedef void rb_unblock_function_t(void *);
337 
348 typedef VALUE rb_blocking_function_t(void *);
349 
360 void rb_thread_check_ints(void);
361 
375 int 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 
408 
420 
429 VALUE rb_mutex_lock(VALUE mutex);
430 
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 
491 
492 #endif /* RBIMPL_INTERN_THREAD_H */
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:65
int rb_thread_interrupted(VALUE thval)
Checks if the thread's execution was recently interrupted.
Definition: thread.c:1449
VALUE rb_thread_local_aref(VALUE thread, ID key)
This badly named function reads from a Fiber local storage.
Definition: thread.c:3571
VALUE rb_mutex_new(void)
Creates a mutex.
Definition: thread_sync.c:191
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:1597
VALUE rb_thread_kill(VALUE thread)
Terminates the given thread.
Definition: thread.c:2764
VALUE rb_thread_main(void)
Obtains the "main" thread.
Definition: thread.c:3003
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
void rb_thread_sleep_forever(void)
Blocks indefinitely.
Definition: thread.c:1379
void rb_thread_fd_close(int fd)
Notifies a closing of a file descriptor to other threads.
Definition: thread.c:2705
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
Definition: thread.c:1411
VALUE rb_mutex_trylock(VALUE mutex)
Attempts to lock the mutex, without waiting for other threads to unlock it.
Definition: thread_sync.c:252
VALUE rb_mutex_locked_p(VALUE mutex)
Queries if there are any threads that holds the lock.
Definition: thread_sync.c:203
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:634
VALUE rb_thread_stop(void)
Stops the current thread.
Definition: thread.c:2915
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:567
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,...
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition: thread.h:336
void rb_thread_atfork_before_exec(void)
:FIXME: situation of this function is unclear.
Definition: thread.c:4805
VALUE rb_thread_create(VALUE(*f)(void *g), void *g)
Creates a Ruby thread that is backended by a C function.
void rb_thread_check_ints(void)
Checks for interrupts.
Definition: thread.c:1432
VALUE rb_thread_run(VALUE thread)
This is a rb_thread_wakeup() + rb_thread_schedule() combo.
Definition: thread.c:2906
VALUE rb_thread_wakeup(VALUE thread)
Marks a given thread as eligible for scheduling.
Definition: thread.c:2859
VALUE rb_mutex_unlock(VALUE mutex)
Releases the mutex.
Definition: thread_sync.c:505
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...
void rb_thread_sleep_deadly(void)
Identical to rb_thread_sleep_forever(), except the thread calling this function is considered "dead" ...
Definition: thread.c:1386
void rb_thread_atfork(void)
A pthread_atfork(3posix)-like API.
Definition: thread.c:4800
VALUE rb_thread_current(void)
Obtains the "current" thread.
Definition: thread.c:2982
int rb_thread_alone(void)
Checks if the thread this function is running is the only thread that is currently alive.
Definition: thread.c:3844
int rb_thread_wait_fd(int fd)
Blocks the current thread until the given file descriptor is ready to be read.
Definition: io.c:1591
VALUE rb_thread_local_aset(VALUE thread, ID key, VALUE val)
This badly named function writes to a Fiber local storage.
Definition: thread.c:3719
void rb_thread_schedule(void)
Tries to switch to another thread.
Definition: thread.c:1480
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_thread_wakeup_alive(VALUE thread)
Identical to rb_thread_wakeup(), except it doesn't raise on an already killed thread.
Definition: thread.c:2868
VALUE rb_mutex_lock(VALUE mutex)
Attempts to lock the mutex.
Definition: thread_sync.c:432
void rb_thread_sleep(int sec)
Blocks for the given period of time.
Definition: thread.c:1455
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
Defines VALUE and ID.
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