| 1 |
#ifndef RBIMPL_INTERN_SELECT_POSIX_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_SELECT_POSIX_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 |
*/ |
| 23 |
#include "ruby/internal/config.h" |
| 24 |
|
| 25 |
#ifdef HAVE_SYS_SELECT_H |
| 26 |
# include <sys/select.h> /* for select(2) (modern POSIX) */ |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef HAVE_UNISTD_H |
| 30 |
# include <unistd.h> /* for select(2) (archaic UNIX) */ |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "ruby/internal/attr/const.h" |
| 34 |
#include "ruby/internal/attr/noalias.h" |
| 35 |
#include "ruby/internal/attr/nonnull.h" |
| 36 |
#include "ruby/internal/attr/pure.h" |
| 37 |
|
| 38 |
/** |
| 39 |
* The data structure which wraps the fd_set bitmap used by `select(2)`. This |
| 40 |
* allows Ruby to use FD sets larger than what has been historically allowed on |
| 41 |
* modern platforms. |
| 42 |
* |
| 43 |
* @internal |
| 44 |
* |
| 45 |
* ... but because this header file is included only when the system is with |
| 46 |
* that "historic restrictions", this is nothing more than an alias of fd_set. |
| 47 |
*/ |
| 48 |
typedef fd_set rb_fdset_t; |
| 49 |
|
| 50 |
/** Clears the given ::rb_fdset_t. */ |
| 51 |
#define rb_fd_zero FD_ZERO |
| 52 |
|
| 53 |
/** Sets the given fd to the ::rb_fdset_t. */ |
| 54 |
#define rb_fd_set FD_SET |
| 55 |
|
| 56 |
/** Unsets the given fd from the ::rb_fdset_t. */ |
| 57 |
#define rb_fd_clr FD_CLR |
| 58 |
|
| 59 |
/** Queries if the given fd is in the ::rb_fdset_t. */ |
| 60 |
#define rb_fd_isset FD_ISSET |
| 61 |
|
| 62 |
/** Initialises the :given :rb_fdset_t. */ |
| 63 |
#define rb_fd_init FD_ZERO |
| 64 |
|
| 65 |
/** Waits for multiple file descriptors at once. */ |
| 66 |
#define rb_fd_select select |
| 67 |
|
| 68 |
/**@cond INTERNAL_MACRO */ |
| 69 |
#define rb_fd_copy rb_fd_copy |
| 70 |
#define rb_fd_dup rb_fd_dup |
| 71 |
#define rb_fd_ptr rb_fd_ptr |
| 72 |
#define rb_fd_max rb_fd_max |
| 73 |
/** @endcond */ |
| 74 |
|
| 75 |
RBIMPL_ATTR_NONNULL(()) |
| 76 |
RBIMPL_ATTR_NOALIAS() |
| 77 |
/** |
| 78 |
* Destructively overwrites an fdset with another. |
| 79 |
* |
| 80 |
* @param[out] dst Target fdset. |
| 81 |
* @param[in] src Source fdset. |
| 82 |
* @param[in] n Unused parameter. |
| 83 |
* @post `dst` is a copy of `src`. |
| 84 |
*/ |
| 85 |
static inline void |
| 86 |
rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int n) |
| 87 |
{ |
| 88 |
*dst = *src; |
| 89 |
} |
| 90 |
|
| 91 |
RBIMPL_ATTR_NONNULL(()) |
| 92 |
RBIMPL_ATTR_NOALIAS() |
| 93 |
/** |
| 94 |
* Destructively overwrites an fdset with another. |
| 95 |
* |
| 96 |
* @param[out] dst Target fdset. |
| 97 |
* @param[in] src Source fdset. |
| 98 |
* @post `dst` is a copy of `src`. |
| 99 |
*/ |
| 100 |
static inline void |
| 101 |
rb_fd_dup(rb_fdset_t *dst, const fd_set *src) |
| 102 |
{ |
| 103 |
*dst = *src; |
| 104 |
} |
| 105 |
|
| 106 |
RBIMPL_ATTR_PURE() |
| 107 |
/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */ |
| 108 |
/** |
| 109 |
* Raw pointer to `fd_set`. |
| 110 |
* |
| 111 |
* @param[in] f Target fdset. |
| 112 |
* @return Underlying fd_set. |
| 113 |
* |
| 114 |
* @internal |
| 115 |
* |
| 116 |
* Extension library must not touch raw pointers. It was a bad idea to let |
| 117 |
* them use it. |
| 118 |
*/ |
| 119 |
static inline fd_set * |
| 120 |
rb_fd_ptr(rb_fdset_t *f) |
| 121 |
{ |
| 122 |
return f; |
| 123 |
} |
| 124 |
|
| 125 |
RBIMPL_ATTR_CONST() |
| 126 |
/** |
| 127 |
* It seems this function has no use. Maybe just remove? |
| 128 |
* |
| 129 |
* @param[in] f A set. |
| 130 |
* @return Number of file descriptors stored. |
| 131 |
*/ |
| 132 |
static inline int |
| 133 |
rb_fd_max(const rb_fdset_t *f) |
| 134 |
{ |
| 135 |
return FD_SETSIZE; |
| 136 |
} |
| 137 |
|
| 138 |
/** @cond INTERNAL_MACRO */ |
| 139 |
/* :FIXME: What are these? They don't exist for sibling implementations. */ |
| 140 |
#define rb_fd_init_copy(d, s) (*(d) = *(s)) |
| 141 |
#define rb_fd_term(f) ((void)(f)) |
| 142 |
/** @endcond */ |
| 143 |
|
| 144 |
#endif /* RBIMPL_INTERN_SELECT_POSIX_H */ |