iv_thread: Fix memory leak in iv_thread_create() error path.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_fd_pump: Alternative Solaris build fix.

Instead of building with -DBSD_COMP to obtain a definition of
FIONREAD, realise that we don't use FIONREAD at all if we don't
have splice(2), so just define it to zero in that case.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_getaddrinfo: Fix build warning on OpenBSD 5.1.

By including netinet/in.h before arpa/inet.h.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Allow __pthread_create to satisfy pthreads dependency.

Tru64 uses mangled symbol names in its libpthread, and has some
preprocessor magic in its pthread.h to do the same mangling, and
so, testing for pthreads symbols without including pthread.h
doesn't work.

Since AC_SEARCH_LIBS() doesn't allow specifying a header file to be
included for tests (and on Tru64, we can't include pthread.h without
additionally specifying -pthread), we'll check for __pthread_create
if pthread_create does not exist.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Define _BSD and _POSIX_PII_SOCKET on Tru64.

On Tru64, we need -D_BSD to get a definition for 'union wait', and
-D_POSIX_PII_SOCKET so that sys/socket.h defines 'socklen_t'.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Check for -pthread.

On Tru64, we need to build with -pthread to be allowed to include
pthread.h:

	pthread.h:339:4: error: #error "Please compile the module including pthread.h with -pthread"

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
dev_poll: Always call DP_POLL with struct dvpoll::dp_nfds >= 1.

Tru64 supports /dev/poll, but returns EINVAL if DP_POLL is
called with dp_nfds == 0, as would be the case when no file
descriptors are registered.

Do what epoll and kqueue do, which is to make sure that we
always pass in at least one return event structure.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
Include sys/socket.h in all files that use socklen_t.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_wait: Use 'union wait' on Tru64.

On Tru64, the second ('status') argument to wait4() is not an int *,
but a union wait * (which contains the actual exit status code).

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Make pthreads library check work on HP-UX.

On HP-UX, libpthread uses mangled symbol names like on Tru64, but
what's worse, the HP-UX C library provides stub versions of almost
all libpthread functions that return errors instead of doing what
they are supposed to -- and there's no way to detect this at compile
time.

However, there are a number of symbols in libpthread that the C
library doesn't define stubs for, and so we can run AC_SEARCH_LIBS
for one of those functions in libpthread, and if that returns
success, force libpthread to be linked in.  pthread_trace_notify_np
is one such symbol, and we will arbitrarily pick it for this purpose.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Define _XOPEN_SOURCE to 500 on HP-UX.

On HP-UX, we need -D_XOPEN_SOURCE=500 so that Berkeley socket API
functions use 'socklen_t's for sockaddr struct lengths instead of
ints.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Detect __sync_lock_test_and_set().

If pthreads spinlocks aren't available, we use hand-coded spinlocks
based on __sync_lock_test_and_set(), but not all platforms have that
latter function available (HP-UX without a sufficiently new version
of gcc is one such platform).  On such platforms, we'll have to stub
out our spinlocks entirely -- this isn't generally safe, but there's
nothing better we can do in that case (apart from refusing to build
at all).

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Add checks for CLOCK_{MONOTONIC,REALTIME}.

On HP-UX, CLOCK_{MONOTONIC,REALTIME} are enum values, and not
defines, so we can't check for them using #ifdef, like we were doing
in iv_timer.  Detect these constants at configure time, and define
appropriate HAVE_* symbols in config.h for iv_timer to use.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_getaddrinfo: Don't assume existence of struct sockaddr_in6 on HP-UX.

On HP-UX, we need to define _XOPEN_SOURCE to 500 to make the
Berkeley socket API use socklen_t instead of int for sockaddr struct
lengths, but a side effect of this is that netinet/in6.h stops defining
struct sockaddr_in6.  There's not much we can do about that, though, so
just concede defeat and stop referencing struct sockaddr_in6 on HP-UX.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
Include arpa/inet.h in sources that use hton[sl]().

As this is necessary on HP-UX when _XOPEN_SOURCE is defined to 500.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
lib/test/server: Don't open too many sockets on HP-UX.

As the default resource limits are set rather low.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
lib/test/timer_rat: Don't register too many timers on HP-UX.

As the default value causes the assembler/linker to run out of memory.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_thread: Print pthread_create() return code in case of failure.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_wait: Avoid using PTHREAD_MUTEX_INITIALIZER.

As it is broken on HP-UX.  Initialize the global mutex from
iv_wait's module constructor function instead.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_work: Fix deadlock scenario introduced by fairness changes.

A recent change to iv_work_thread_got_event, iv_work's pool
thread work handling loop, added some logic to make sure we return
from the function every now and then, so that we don't starve other
fds/tasks/timers registered in this thread.

The idea was that even if more work is pending, we will just return
from the function, and rely on other logic in iv_work to call us again
if there is still more work to do.

When a new work item is submitted via iv_work_submit_pool(), the
logic is as follows:

	if (there are pool threads idle)
		kick an idle thread
	else if (we are not at the maximum number of pool threads yet)
		start a new pool thread

However, if we're already at the maximum number of pool threads, and
none of those threads are idle when more work arrives, we will neither
kick a thread nor start a new one.  And with the fairness changes,
pool threads no longer check whether there is work left to do before
going to sleep again, which can lead to a deadlock scenario where
there is work pending but all work threads idle.

Fix this by checking at iv_work_thread_got_event() exit time whether
there is work left to do, and if there is, kick ourselves to make sure
we get called again after all other pending fds/tasks/timers have been
processed.

Reported-by: Gergely Nagy <algernon@balabit.hu>
Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>