-=cut
-sub add_timer ($$) {
- my ($secs, $coderef) = @_;
-
- my $fire_time = now() + $secs;
-
- my $timer = [$fire_time, $coderef];
-
- if (!@Timers || $fire_time >= $Timers[-1][0]) {
- push @Timers, $timer;
- return $timer;
- }
-
- # Now, where do we insert? (NOTE: this appears slow, algorithm-wise,
- # but it was compared against calendar queues, heaps, naive push/sort,
- # and a bunch of other versions, and found to be fastest with a large
- # variety of datasets.)
- for (my $i = 0; $i < @Timers; $i++) {
- if ($Timers[$i][0] > $fire_time) {
- splice(@Timers, $i, 0, $timer);
- return $timer;
- }
- }
-
- die "Shouldn't get here.";
-}
-
-# keeping this around in case we support other FD types for now,
-# epoll_create1(EPOLL_CLOEXEC) requires Linux 2.6.27+...
-sub set_cloexec ($) {
- my ($fd) = @_;
-
- $_io = IO::Handle->new_from_fd($fd, 'r+') or return;
- defined(my $fl = fcntl($_io, F_GETFD, 0)) or return;
- fcntl($_io, F_SETFD, $fl | FD_CLOEXEC);
-}
-
-sub _InitPoller
-{
- return if $DoneInit;
- $DoneInit = 1;
-
- if (PublicInbox::Syscall::epoll_defined()) {
- $Epoll = epoll_create();
- set_cloexec($Epoll) if (defined($Epoll) && $Epoll >= 0);
- } else {
- my $cls;
- for (qw(DSKQXS DSPoll)) {
- $cls = "PublicInbox::$_";
- last if eval "require $cls";
- }
- $cls->import(qw(epoll_ctl epoll_wait));
- $Epoll = $cls->new;
- }
- *EventLoop = *EpollEventLoop;
-}
-
-=head2 C<< CLASS->EventLoop() >>
-
-Start processing IO events. In most daemon programs this never exits. See
-C<PostLoopCallback> below for how to exit the loop.
-
-=cut
-sub FirstTimeEventLoop {
- my $class = shift;
-
- _InitPoller();