| 1 |
#ifndef RBIMPL_INTERN_SELECT_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_SELECT_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs to provide ::rb_fd_select(). |
| 22 |
* @note Functions and structs defined in this header file are not |
| 23 |
* necessarily ruby-specific. They don't need ::VALUE etc. |
| 24 |
*/ |
| 25 |
#include "ruby/internal/config.h" |
| 26 |
|
| 27 |
#ifdef HAVE_SYS_TYPES_H |
| 28 |
# include <sys/types.h> /* for NFDBITS (BSD Net/2) */ |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "ruby/internal/dllexport.h" |
| 32 |
|
| 33 |
/* thread.c */ |
| 34 |
#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT) |
| 35 |
# include "ruby/internal/intern/select/largesize.h" |
| 36 |
#elif defined(_WIN32) |
| 37 |
# include "ruby/internal/intern/select/win32.h" |
| 38 |
# /** Does nothing (defined for compatibility). */ |
| 39 |
# define rb_fd_resize(n, f) ((void)(f)) |
| 40 |
#else |
| 41 |
# include "ruby/internal/intern/select/posix.h" |
| 42 |
# /** Does nothing (defined for compatibility). */ |
| 43 |
# define rb_fd_resize(n, f) ((void)(f)) |
| 44 |
#endif |
| 45 |
|
| 46 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 47 |
|
| 48 |
struct timeval; |
| 49 |
|
| 50 |
/** |
| 51 |
* Waits for multiple file descriptors at once. This is basically a wrapper of |
| 52 |
* system-provided select() with releasing GVL, to allow other Ruby threads run |
| 53 |
* in parallel. |
| 54 |
* |
| 55 |
* @param[in] nfds Max FD in everything passed, plus one. |
| 56 |
* @param[in,out] rfds Set of FDs to wait for reads. |
| 57 |
* @param[in,out] wfds Set of FDs to wait for writes. |
| 58 |
* @param[in,out] efds Set of FDs to wait for OOBs. |
| 59 |
* @param[in,out] timeout Max blocking duration. |
| 60 |
* @retval -1 Failed, errno set. |
| 61 |
* @retval 0 Timeout exceeded. |
| 62 |
* @retval otherwise Total number of file descriptors returned. |
| 63 |
* @post `rfds` contains readable FDs. |
| 64 |
* @post `wfds` contains writable FDs. |
| 65 |
* @post `efds` contains exceptional FDs. |
| 66 |
* @post `timeout` is the time left. |
| 67 |
* @note All pointers are allowed to be null pointers. |
| 68 |
* |
| 69 |
* Although backend threads can run in parallel of this function, touching a |
| 70 |
* file descriptor from multiple threads could be problematic. For instance |
| 71 |
* what happens when a thread closes a file descriptor that is selected by |
| 72 |
* someone else, vastly varies among operating systems. You would better avoid |
| 73 |
* touching an fd from more than one threads. |
| 74 |
* |
| 75 |
* @internal |
| 76 |
* |
| 77 |
* Although any file descriptors are possible here, it makes completely no |
| 78 |
* sense to pass a descriptor that is not `O_NONBLOCK`. If you want to know |
| 79 |
* the reason for this limitation in detail, you might find this thread super |
| 80 |
* interesting: https://lkml.org/lkml/2004/10/6/117 |
| 81 |
*/ |
| 82 |
int rb_thread_fd_select(int nfds, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout); |
| 83 |
|
| 84 |
RBIMPL_SYMBOL_EXPORT_END() |
| 85 |
|
| 86 |
#endif /* RBIMPL_INTERN_SELECT_H */ |