Guard against multiple inclusion of mutex.h and pthr.h.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_event: Get rid of iv_tls usage.

As iv_event has become one of the core ivykis modules, integrate its
per-thread state directly into struct iv_state instead of making it
use iv_tls.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
test/avl: Reduce test AVL tree size.

So that the testsuite doesn't take forever on slower machines.

Reported-by: Christian T. Steigies <cts@debian.org>
Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
Turn iv_validate_now() into a no-op.

When support for pthread_[gs]etspecific() was added, 'iv_now' was
changed from being a __thread variable to being a macro dereferencing
the pointer return value of a call to a function in the ivykis core
(commit 5eed214c3e22, "iv_timer: Move per-thread state to struct
iv_state."), with the goal of moving knowledge of how thread-local
state is stored out of the iv.h public header file.

However, this changed the 'call iv_validate_now() / access iv_now'
sequence of operations for accessing the monotonic clock from a
single function call and a thread-local value reference into a
sequence of two function calls, where one function call would be
enough to do the job.

This commit changes iv_validate_now() into an empty inline function,
and adds the clock validation logic to the function that returns the
address of iv_now (__iv_now_location()), so that all code compiled
against a newer ivykis version will only do a single function call to
return the current value of the monotonic clock.

To preserve API compatibility, iv_validate_now() will continue to
exist as an inline function for as long as we want to keep API/ABI
compatibility (likely forever), and the requirement for ivykis users
to call iv_validate_now() before referencing iv_now has not been
dropped by this commit, to ensure ivykis users will still work
properly when built and linked against older ivykis versions.

To preserve ABI compatibility for code compiled against an older
ivykis version and dynamically linked against a newer ivykis version,
the old version of iv_validate_now() will continue to be provided as
a symbol exported from libivykis.so.

To prevent ABI problems for code compiled against a newer ivykis
version and dynamically linked against an older ivykis version, the
new version of __iv_now_location() is given a different name, to
prevent code compiled against newer ivykis versions which cause
references to 'iv_now' to validate it if it's out of date from
ending up with a version of __iv_now_location() that doesn't perform
this validation.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_fd: Let ->poll() return whether a timeout could have occurred.

After having polled for new events, poll methods can sometimes deduce
that the specified timeout cannot possibly have expired yet, and this
information can be used to avoid checking for expired timers on return
(which involves a potentially costly call to clock_gettime() or
gettimeofday() that we would gladly avoid).

This commit makes iv_fd_poll_method::poll() return an int, where zero
means "the specified timeout has definitely not expired yet", and a
nonzero value means "the timeout may or may not have expired yet".

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_fd: Allow timeout handling optimizations in poll methods.

All file descriptor polling methods that ivykis supports have timeout
arguments to their respective polling system calls -- epoll_wait(2),
poll(2) and poll(7d) have a relative milliseconds argument, and
kqueue(2), port_getn(3C) and ppoll(2) have a relative struct timespec
pointer argument.

When calling into the kernel to wait for file descriptor activity to
occur, ivykis determines the timeout to be used for that system call
by finding the registered iv_timer with the soonest expiring timeout,
and passing in that timeout to the poll system call for the polling
method currently in use, so that that system call will return when
there is file descriptor activity or when the first iv_timer to be
expiring has expired.

One opportunity for improvement on this approach has to do with the
fact that on most operating systems, there appears to be a nontrivial
cost associated with setting up and tearing down a kernel timer on
every entry to and exit from a poll system call (which is what happens
if you pass in a sub-infinite timeout to such a system call), and that
some of those operating systems provide a way of setting up a kernel
timer that can be kept running across system calls.

Even on operating systems where setting up and tearing down kernel
timers isn't all that costly, it can be advantageous to set up a
separate kernel timer corresponding to the soonest expiring iv_timer,
as that will give us explicit notification of when that timer expires,
which can let us avoid calling clock_gettime() on every poll system
call return to check whether the current time is greater than or equal
to the soonest expiring iv_timer's timeout yet.

This commit adds two new poll method methods, ->set_poll_timeout()
and ->clear_poll_timeout():

* ->set_poll_timeout(), when implemented, will be called by the ivykis
  core when it wants to configure an explicit timeout, which when it
  expires should cause a subsequent call to ->poll() to return, and
  with a nonzero value, even if that call to ->poll() was done with a
  NULL (infinite) timeout.

* ->clear_poll_timeout() will be called if an explicit timeout was
  previously configured which has now been superseded, for example if
  an iv_timer with a timeout earlier than the currently registered
  explicit timeout is registered before the explicit timeout expires.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
epoll: Implement ->set_poll_timeout() using timerfd_create(2).

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
kqueue: Implement ->set_poll_timeout() using EVFILT_TIMER.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
port: Implement ->set_poll_timeout() using timer_create(3C).

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>