/
epoll.pm6
230 lines (165 loc) · 5.68 KB
/
epoll.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use NativeCall;
enum (
EPOLL_CTL_ADD => 1,
EPOLL_CTL_DEL => 2,
EPOLL_CTL_MOD => 3
);
enum EPOLL_EVENTS (
EPOLLIN => 0x001,
EPOLLPRI => 0x002,
EPOLLOUT => 0x004,
EPOLLRDNORM => 0x040,
EPOLLRDBAND => 0x080,
EPOLLWRNORM => 0x100,
EPOLLWRBAND => 0x200,
EPOLLMSG => 0x400,
EPOLLERR => 0x008,
EPOLLHUP => 0x010,
EPOLLRDHUP => 0x2000,
EPOLLWAKEUP => 0x20000000,
EPOLLONESHOT => 0x40000000,
EPOLLET => 0x80000000
);
class epoll-event is repr('CStruct')
{
has uint32 $.events;
has int32 $.fd;
has int32 $!pad;
method in { so $!events +& EPOLLIN }
method out { so $!events +& EPOLLOUT }
}
sub sys_close(int32 --> int32) is native is symbol('close') {}
sub calloc(size_t, size_t --> Pointer) is native {}
sub free(Pointer) is native {}
sub epoll_create1(int32 --> int32) is native {}
sub epoll_ctl(int32, int32, int32, epoll-event --> int32) is native {}
sub epoll_wait(int32, Pointer, int32, int32 --> int32) is native {}
class epoll
{
has $.maxevents = 1;
has $!epfd;
has Pointer $!events;
submethod TWEAK
{
$!epfd = epoll_create1(0);
die "Failure creating epoll" if $!epfd == -1;
$!events = calloc($!maxevents, nativesizeof(epoll-event));
die "Out of memory" unless $!events;
}
submethod DESTROY
{
sys_close($!epfd) if $!epfd >= 0;
$!epfd = -1;
free($_) with $!events;
$!events = Pointer;
}
method add(int32 $fd, Bool :$in = False,
Bool :$out = False,
Bool :$priority = False,
Bool :$edge-triggered = False,
Bool :$one-shot = False,
Bool :$mod = False)
{
my int32 $events = EPOLLIN * $in
+| EPOLLPRI * $priority
+| EPOLLOUT * $out
+| EPOLLET * $edge-triggered
+| EPOLLONESHOT * $one-shot;
my $event = epoll-event.new(:$events, :$fd);
if epoll_ctl($!epfd, ($mod ?? EPOLL_CTL_MOD !! EPOLL_CTL_ADD),
$fd, $event) == -1
{
die 'Failed epoll_ctl()';
}
self
}
method remove(int32 $fd)
{
if epoll_ctl($!epfd, EPOLL_CTL_DEL, $fd, epoll-event) == -1
{
die 'Failed in epoll_ctl()';
}
}
method wait(int32 :$timeout = -1)
{
my $count = epoll_wait($!epfd, $!events, $!maxevents, $timeout);
die 'Failed in epoll_wait()' if $count < 0;
do for ^$count -> $i
{
nativecast(epoll-event,
Pointer.new(+$!events + $i*nativesizeof(epoll-event)))
}
}
}
=begin pod
=head1 NAME
epoll - I/O event notification facility
=head1 SYNOPSIS
use epoll;
my $epoll = epoll.new(maxevents => 1); # 1 is default
$epoll.add($file-descriptor, :in, :out, :priority, :edge-triggered);
# timeout in milliseconds, default -1 = block forever
for $epoll.wait(:2000timeout)
{
say "{.fd} is ready for reading" if .in;
say "{.fd} is ready for writing" if .out;
}
# Or use chained calls:
for epoll.new.add(0, :in).wait
{
say "ready to read on {.fd}" if .in;
}
=head1 DESCRIPTION
Simple low level interface around the Linux C<epoll(7)> I/O event
notification facility. It can monitor multiple file descriptors to
see if I/O is possible on any of them. Mainly useful for interfacing
with other NativeCall modules, since Perl itself has a rich I/O
system. If you really want to use this with Perl C<IO::Handle>s, you
can use C<native-descriptor()> to get a suitable descriptor.
=head2 class B<epoll>
=item method B<new>(:$maxevents = 1)
Create a new epoll object. Maxevents is the maximum number of events
that can be returned from a single call to wait.
=item method B<add>(int32 $file-descriptor, ...event flags...)
Flags:
=table
:in | EPOLLIN | ready for read
:out | EPOLLOUT | ready for write
:priority | EPOLLPRI | urgent data available for read
:edge-triggered | EPOLLET | Edge Triggered
:one-shot | EPOLLONESHOT | Disables after 1 event
:mod | EPOLL_CTL_MOD | Modify an existing file descriptor
:mod is equivalent to EPOLL_CTL_MOD to change the events for a file
descriptor already added. It will also re-enable a file descriptor
disabled by :one-shot mode.
For convenience, always returns the object itself, so you can chain
calls.
=item method B<remove>(int32 $file-descriptor)
Remove a file descriptor previously added.
=item method B<wait>(int32 :$timeout = -1)
Wait for 1 or more events to occur on the add()ed file descriptors.
You can specify an optional timeout in milliseconds.
Returns a List of up up to $maxevents B<epoll-event>s.
=head2 class B<epoll-event>
=item method int32 B<fd>()
The file descriptor for the event.
=item method uint32 B<events>()
A bitmask of the events that occurred. You can check them like this:
if $event.events +& EPOLLIN {...}
or use the much easier:
=item method Bool B<in>()
Ready to read
=item method Bool B<out>()
Ready to write
=head2 EXCEPTIONS
Throws Ad-hoc exceptions for any errors.
(Should save errno, and make real Exceptions -- patches welcome!)
=head2 NOTE
epoll is a Linux specific mechanism, and is typically not available on
other architectures.
=head1 LICENSE
Copyright © 2017 United States Government as represented by the
Administrator of the National Aeronautics and Space Administration.
No copyright is claimed in the United States under Title 17,
U.S.Code. All Other Rights Reserved.
=end pod