Index: src/xplcommon/CondVar.h
===================================================================
--- src/xplcommon/CondVar.h	(revision 34)
+++ src/xplcommon/CondVar.h	(revision 34)
@@ -0,0 +1,118 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_CONDVAR_H
+#define XPLCOMMON_CONDVAR_H
+//------------------------------------------------------------------------------
+
+#if 0 // The public interface of a condition variable
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon {
+
+//------------------------------------------------------------------------------
+
+class Mutex;
+
+//------------------------------------------------------------------------------
+
+/**
+ * Condition variable.
+ */
+class CondVar
+{
+public:
+    /**
+     * Wait for the condition variable.
+     *
+     * @param mutex the mutex to unlock and re-lock.
+     * @param timeout the number of milliseconds to wait at most. If
+     * negative, the function will wait indefinitely.
+     *
+     * @return true, if the condition variable was signalled, false
+     * if a timeout occured
+     */
+    bool wait(Mutex& mutex, int timeout = -1);
+
+    /**
+     * Signal the condition variable to wake up one thread.
+     */
+    void signal();
+
+    /**
+     * Signal all threads waiting on the condition variable.
+     */
+    void broadcast();
+};
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+
+#endif // 0, The public interface of a condition variable
+
+//------------------------------------------------------------------------------
+
+#include "config.h"
+
+//------------------------------------------------------------------------------
+
+#if TARGET_API_WIN32
+
+//------------------------------------------------------------------------------
+
+#include "win32/CondVar.h"
+
+namespace xplcommon { typedef win32::CondVar CondVar; }
+
+//------------------------------------------------------------------------------
+
+#else // TARGET_API_WIN32
+
+//------------------------------------------------------------------------------
+
+#include "posix/CondVar.h"
+
+namespace xplcommon { typedef posix::CondVar CondVar; }
+
+//------------------------------------------------------------------------------
+
+#endif // TARGET_API_WIN32
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_CONDVAR_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/Makefile.am
===================================================================
--- src/xplcommon/Makefile.am	(revision 32)
+++ src/xplcommon/Makefile.am	(revision 34)
@@ -29,4 +29,5 @@
 	Thread.h		\
 	Mutex.h			\
+	CondVar.h		\
 	Failable.h		\
 	Waiter.h		\
Index: src/xplcommon/posix/CondVar.cc
===================================================================
--- src/xplcommon/posix/CondVar.cc	(revision 34)
+++ src/xplcommon/posix/CondVar.cc	(revision 34)
@@ -0,0 +1,61 @@
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "CondVar.h"
+
+#include <sys/time.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::CondVar;
+
+//------------------------------------------------------------------------------
+
+void CondVar::getAbsTime(struct ::timespec& abstime, int timeout)
+{
+    struct timeval tv;
+    gettimeofday(&tv, 0);
+
+    abstime.tv_sec = tv.tv_sec + timeout / 1000;
+    abstime.tv_nsec = (tv.tv_usec + (timeout%1000) * 1000) * 1000;
+    if (abstime.tv_nsec>=1000000000) {
+        ++abstime.tv_sec;
+        abstime.tv_nsec -= 1000000000;
+    }
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/CondVar.h
===================================================================
--- src/xplcommon/posix/CondVar.h	(revision 34)
+++ src/xplcommon/posix/CondVar.h	(revision 34)
@@ -0,0 +1,148 @@
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_CONDVAR_H
+#define XPLCOMMON_POSIX_CONDVAR_H
+//------------------------------------------------------------------------------
+
+#include "Mutex.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A condition variable for POSIX threads.
+ */
+class CondVar
+{
+private:
+    /**
+     * Fill the given timespec with the absolute time corresponding to
+     * the given relative timeout.
+     */
+    static void getAbsTime(struct ::timespec& abstime, int timeout);
+
+    /**
+     * The pthreads structure.
+     */
+    pthread_cond_t cond;
+
+public:
+    /**
+     * Construct the condition variable.
+     */
+    CondVar();
+
+    /**
+     * Destroy the condition variable.
+     */
+    ~CondVar();
+
+    /**
+     * Wait for the condition variable.
+     *
+     * @param mutex the mutex to unlock and re-lock.
+     * @param timeout the number of milliseconds to wait at most. If
+     * negative, the function will wait indefinitely.
+     *
+     * @return true, if the condition variable was signalled, false
+     * if a timeout occured
+     */
+    bool wait(Mutex& mutex, int timeout = -1);
+
+    /**
+     * Signal the condition variable to wake up one thread.
+     */
+    void signal();
+
+    /**
+     * Signal all threads waiting on the condition variable.
+     */
+    void broadcast();
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline CondVar::CondVar()
+{
+    pthread_cond_init(&cond, 0);
+}
+
+//------------------------------------------------------------------------------
+
+inline CondVar::~CondVar()
+{
+    pthread_cond_destroy(&cond);
+}
+
+//------------------------------------------------------------------------------
+
+inline bool CondVar::wait(Mutex& mutex, int timeout)
+{
+    if (timeout<0) {
+        pthread_cond_wait(&cond, &mutex.mutex);
+        return true;
+    } else {
+        struct ::timespec abstime;
+        getAbsTime(abstime, timeout);
+        return pthread_cond_timedwait(&cond, &mutex.mutex, &abstime)==0;
+    }
+}
+
+//------------------------------------------------------------------------------
+
+inline void CondVar::signal()
+{
+    pthread_cond_signal(&cond);
+}
+
+//------------------------------------------------------------------------------
+
+inline void CondVar::broadcast()
+{
+    pthread_cond_broadcast(&cond);
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_CONDVAR_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Makefile.am
===================================================================
--- src/xplcommon/posix/Makefile.am	(revision 32)
+++ src/xplcommon/posix/Makefile.am	(revision 34)
@@ -3,4 +3,5 @@
 libxplcommon_posix_la_SOURCES =	\
 	Thread.cc		\
+	CondVar.cc		\
 	Waitable.cc		\
 	Waiter.cc		\
@@ -22,4 +23,5 @@
 	Thread.h		\
 	Mutex.h			\
+	CondVar.h		\
 	Waitable.h		\
 	Waiter.h		\
Index: src/xplcommon/posix/Mutex.h
===================================================================
--- src/xplcommon/posix/Mutex.h	(revision 32)
+++ src/xplcommon/posix/Mutex.h	(revision 34)
@@ -79,4 +79,6 @@
      */
     void unlock();
+
+    friend class CondVar;
 };
 
Index: src/xplcommon/win32/CondVar.cc
===================================================================
--- src/xplcommon/win32/CondVar.cc	(revision 34)
+++ src/xplcommon/win32/CondVar.cc	(revision 34)
@@ -0,0 +1,88 @@
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "CondVar.h"
+
+//------------------------------------------------------------------------------
+
+using xplcommon::win32::CondVar;
+
+//------------------------------------------------------------------------------
+
+bool CondVar::wait(Mutex& inMutex, int timeout)
+{
+    mutex.lock();
+    ++numWaiters;
+    size_t generation = waitGeneration;
+    mutex.unlock();
+
+    inMutex.unlock();
+
+    bool lastWaiter = false;
+    bool wokenUp = false;
+    bool waitDone = false;
+    while (!waitDone) {
+        int result = WaitForSingleObject(event,
+                                         (timeout<0) ? INFINITE : timeout);
+        if (result==WAIT_OBJECT_0) {
+            mutex.lock();
+            waitDone = numToRelease>0 && generation!=waitGeneration;
+            if (waitDone) {
+                --numToRelease;
+                lastWaiter = numToRelease == 0;
+                wokenUp = true;
+            }
+            mutex.unlock();
+        } else {
+            waitDone = true;
+        }
+    }
+
+    inMutex.lock();
+
+    mutex.lock();
+    --numWaiters;
+    mutex.unlock();
+
+    if (lastWaiter) {
+        ResetEvent(event);
+    }
+
+    return wokenUp;
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/win32/CondVar.h
===================================================================
--- src/xplcommon/win32/CondVar.h	(revision 34)
+++ src/xplcommon/win32/CondVar.h	(revision 34)
@@ -0,0 +1,168 @@
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_WIN32_CONDVAR_H
+#define XPLCOMMON_WIN32_CONDVAR_H
+//------------------------------------------------------------------------------
+
+#include "Mutex.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace win32 {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A condition variable for the Win32 API.
+ *
+ * Note: the CONDITION_VARIABLE structure and the associated functions
+ * are available only starting with the Windows Vista API. However,
+ * for the time being we also want to support Windows XP, hence this
+ * rather fancy implementation.
+ */
+class CondVar
+{
+private:
+    /**
+     * The mutex protecting the internal data structures.
+     */
+    Mutex mutex;
+
+    /**
+     * The number of waiters.
+     */
+    size_t numWaiters;
+
+    /**
+     * The wait generation.
+     */
+    size_t waitGeneration;
+
+    /**
+     * The number of waiters to release.
+     */
+    size_t numToRelease;
+
+    /**
+     * The event to use for waiting.
+     */
+    HANDLE event;
+
+public:
+    /**
+     * Construct the condition variable.
+     */
+    CondVar();
+
+    /**
+     * Destroy the condition variable.
+     */
+    ~CondVar();
+
+    /**
+     * Wait for the condition variable.
+     *
+     * @param mutex the mutex to unlock and re-lock.
+     * @param timeout the number of milliseconds to wait at most. If
+     * negative, the function will wait indefinitely.
+     *
+     * @return true, if the condition variable was signalled, false
+     * if a timeout occured
+     */
+    bool wait(Mutex& inMutex, int timeout = -1);
+
+    /**
+     * Signal the condition variable to wake up one thread.
+     */
+    void signal();
+
+    /**
+     * Signal all threads waiting on the condition variable.
+     */
+    void broadcast();
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline CondVar::CondVar() :
+    numWaiters(0),
+    waitGeneration(0),
+    numToRelease(0)
+{
+    event  = CreateEvent(0, true, false, 0);
+}
+
+//------------------------------------------------------------------------------
+
+inline CondVar::~CondVar()
+{
+    CloseHandle(event);
+}
+
+//------------------------------------------------------------------------------
+
+inline void CondVar::signal()
+{
+    mutex.lock();
+    if (numWaiters>numToRelease) {
+        SetEvent(event);
+        ++numToRelease;
+        ++waitGeneration;
+    }
+    mutex.unlock();
+}
+
+//------------------------------------------------------------------------------
+
+inline void CondVar::broadcast()
+{
+    mutex.lock();
+    if (numWaiters>0) {
+        SetEvent(event);
+        numToRelease = numWaiters;
+        ++waitGeneration;
+    }
+    mutex.unlock();
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::win32 */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_WIN32_CONDVAR_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/win32/Makefile.am
===================================================================
--- src/xplcommon/win32/Makefile.am	(revision 32)
+++ src/xplcommon/win32/Makefile.am	(revision 34)
@@ -3,4 +3,5 @@
 libxplcommon_win32_la_SOURCES =	\
 	Thread.cc		\
+	CondVar.cc		\
 	Event.cc		\
 	Overlapped.cc		\
@@ -17,4 +18,5 @@
 	Thread.h		\
 	Mutex.h			\
+	CondVar.h		\
 	EventFailable.h		\
 	Event.h			\
Index: src/xplcommon/win32/Mutex.h
===================================================================
--- src/xplcommon/win32/Mutex.h	(revision 33)
+++ src/xplcommon/win32/Mutex.h	(revision 34)
@@ -79,4 +79,6 @@
      */
     void unlock();
+
+    friend class CondVar;
 };
 
