ovs-thread: New module, initially just with pthreads wrapper functions.

The only tricky part here is that I'm throwing in annotations to allow
"sparse" to report unbalanced locking.

Signed-off-by: Ben Pfaff <blp@nicira.com>
ovs-thread: Add per-thread data support.

POSIX defines a portable pthread_key_t API for per-thread data.  GCC and
C11 have two different forms of per-thread data that are generally faster
than the POSIX API, where they are available.  This commit adds a
macro-based wrapper, DEFINE_PER_THREAD_DATA, that takes advantage of these
features where they are available and falls back to the POSIX API
otherwise.

The Clang compiler implements the C11 _Thread_local keyword.

This commit also adds a convenience wrapper for the POSIX API, via the
DEFINE_PER_THREAD_MALLOCED_DATA macro.

Signed-off-by: Ben Pfaff <blp@nicira.com>
ovs-atomic: New library for atomic operations.

This library should prove useful for the threading changes coming up.
The following commit introduces one (very simple) user.

Signed-off-by: Ben Pfaff <blp@nicira.com>
ovs-thread: Add support for convenient once-only initializers.

pthread_once() is portable but it does not allow passing any parameters to
the initialization function, which is often inconvenient, because it means
that the function can only access data declared at file scope.  This commit
introduces an alternative with a more convenient interface.

Signed-off-by: Ben Pfaff <blp@nicira.com>
ovs-thread: Add support for various thread-related assertions.

Signed-off-by: Ben Pfaff <blp@nicira.com>
New function ovs_strerror() as a thread-safe replacement for strerror().

Signed-off-by: Ben Pfaff <blp@nicira.com>
Replace all uses of strerror() by ovs_strerror(), for thread safety.

Signed-off-by: Ben Pfaff <blp@nicira.com>
random: Make thread-safe.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Use random_*() instead of rand(), for thread safety.

None of these test programs are threaded, but has little cost and means
that "grep" doesn't turn up any instances of these thread-unsafe functions
in our tree.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Makefile: Blacklist functions that threaded programs cannot use safely.

Some functions that POSIX says cannot be used safely in multithreaded
programs are not on the initial blacklist:

    - getenv() should be safe in real implementations in the absence of
      changes to the environment.  (putenv() and setenv() are blacklisted.)

    - We only use getopt() before spawning extra threads, and I expect this
      to continue to be true.

Signed-off-by: Ben Pfaff <blp@nicira.com>