2
// demonstration file for the epoll() linux specific call, by Micha
10
baseunix, unix, linux;
19
gPipes: array of tfildes;
20
gEvents: array of epoll_event;
21
gCount, gFired, gWrites: integer;
24
function getustime: qword;
28
fpgettimeofday(@tm, nil);
29
result := tm.tv_sec * 1000000 + tm.tv_usec;
32
procedure read_cb(fd, idx: integer);
37
widx := idx + NumActive + 1;
39
if fpread(fd, ch, sizeof(ch)) <> 0 then
42
writeln('false read event: fd=', fd, ' idx=', idx);
46
if widx >= NumPipes then
48
fpwrite(gPipes[widx][1], 'e', 1);
54
procedure run_once(var work: integer; var tr: qword);
60
for i := 0 to NumActive-1 do
62
fpwrite(gPipes[i][1], 'e', 1);
70
res := epoll_wait(epoll_fd, @gEvents[0], NumPipes, 0);
71
for i := 0 to res-1 do
72
read_cb(gPipes[gEvents[i].data.u32][0], gEvents[i].data.u32);
73
until gCount = gFired;
85
SetLength(gEvents, NumPipes);
86
SetLength(gPipes, NumPipes);
87
epoll_fd := epoll_create(NumPipes);
90
writeln('error calling epoll_create');
94
for i := 0 to NumPipes-1 do
96
if fppipe(gPipes[i]) = -1 then
98
writeln('error calling pipe');
101
fpfcntl(gPipes[i][0], F_SETFL, fpfcntl(gPipes[i][0], F_GETFL) or O_NONBLOCK);
103
lEvent.events := EPOLLIN;
104
lEvent.data.u32 := i;
105
if epoll_ctl(epoll_fd, EPOLL_CTL_ADD, gPipes[i][0], @lEvent) < 0 then
107
writeln('error calling epoll_ctl');
112
for i := 0 to NumRuns-1 do
117
writeln(double(tr)/double(work):10:7);