Project

General

Profile

Feature #17059 ยป epoll.h

WIP backend - dsh0416 (Delton Ding), 07/29/2020 03:55 PM

 
1
#ifndef RBIMPL_INTERN_SELECT_EPOLL_H                 /*-*-C++-*-vi:se ft=cpp:*/
2
#define RBIMPL_INTERN_SELECT_EPOLL_H
3
/**
4
 * @file
5
 * @author     Ruby developers <ruby-core@ruby-lang.org>
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
#include "ruby/internal/attr/pure.h"
25
#include "ruby/internal/attr/const.h"
26

    
27
typedef struct {
28
    int n;
29
    int *fd; // TODO: a Vector-like sturct for dynamic allocation
30
} rb_fdset_t;
31

    
32
typedef fd_set rb_fdset_t;
33

    
34
#ifdef HAVE_SYS_EPOLL_H
35
# include <sys/epoll.h>        /* for epoll(7) (Linux) */
36
#endif
37

    
38
int epoll_fd = -1;
39

    
40
#define rb_fd_zero   rb_fd_zero
41
#define rb_fd_set    rb_fd_set
42
#define rb_fd_clr    rb_fd_clr
43
#define rb_fd_isset  rb_fd_isset
44
#define rb_fd_init   rb_fd_init
45
#define rb_fd_copy  rb_fd_copy
46
#define rb_fd_dup   rb_fd_dup
47
#define rb_fd_ptr   rb_fd_ptr
48
#define rb_epoll_max_events 8
49
/** @endcond */
50

    
51
static inline void
52
rb_fd_zero(rb_fdset_t *dst)
53
{
54
    dst->n = 0;
55
    dst->fd = NULL;
56
}
57

    
58
static inline void
59
rb_fd_set(int fd, rb_fdset_t *dst)
60
{
61
    int *new_fd;
62
    dst->n++;
63
    new_fd = (int*) malloc(n * sizeof(int));
64
    memcpy(new_fd, dst->fd, (n - 1) * sizeof(int));
65
    new_fd[n-1] = fd;
66
}
67

    
68
static inline void
69
rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int n)
70
{
71
    *dst = *src;
72
}
73

    
74
static inline void
75
rb_fd_dup(rb_fdset_t *dst, const fd_set *src, int n)
76
{
77
    *dst = *src;
78
}
79

    
80
RBIMPL_ATTR_PURE()
81
/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
82
static inline fd_set *
83
rb_fd_ptr(rb_fdset_t *f)
84
{
85
    return f;
86
}
87

    
88
RBIMPL_ATTR_NONNULL(())
89
RBIMPL_ATTR_PURE()
90
static inline int
91
rb_fd_max(const rb_fdset_t *f)
92
{
93
    return f->maxfd;
94
}
95

    
96
static inline void
97
rb_epoll_init()
98
{
99
    if (epoll_fd == -1) {
100
        epoll_fd = epoll_create1(0);
101
        assert(epoll_id != -1);
102
    }
103
}
104

    
105
static int
106
rb_fd_select(int n, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout)
107
{
108
    int epoll_timeout;
109
    rb_epoll_init(); // lazily create epoll FD, since not everybody waits on I/O
110
    epoll_timeout = (int) timeout->tv_sec * 1000 + timeout->tv_usec;
111
    if (epoll_timeout < 0) epoll_timeout = INT_MAX; // Integer Overflow
112

    
113
    // TODO: implement the conversion of epoll_events
114
    // int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
115
    epoll_wait(epoll_fd, x, rb_epoll_max_events, epoll_timeout);
116
}
117

    
118
/* :FIXME: What are these?  They don't exist for shibling implementations. */
119
#define rb_fd_init_copy(d, s) (*(d) = *(s))
120
#define rb_fd_term(f)   ((void)(f))
121

    
122
#endif /* RBIMPL_INTERN_SELECT_EPOLL_H */