Index: src/hu/varadiistvan/scpl/Mutex.h
===================================================================
--- src/hu/varadiistvan/scpl/Mutex.h	(revision 4)
+++ src/hu/varadiistvan/scpl/Mutex.h	(revision 4)
@@ -0,0 +1,105 @@
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of VSCPL, a simple cross-platform utility library
+
+// 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 HU_VARADIISTVAN_SCPL_MUTEX_H
+#define HU_VARADIISTVAN_SCPL__MUTEX_H
+//------------------------------------------------------------------------------
+
+#if 0 // The public interface of Mutex
+
+//------------------------------------------------------------------------------
+
+namespace hu { namespace varadiistvan { namespace scpl {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A mutual exclusion device.
+ */
+class Mutex
+{
+public:
+    /**
+     * Lock the mutex. If the mutex is locked by another thread, the
+     * caller one waits until the other thread releases the mutex.
+     */
+    void lock();
+
+    /**
+     * Try to lock the mutex. If the mutex is locked by another
+     * thread, this function returns false immediately. Otherwise it
+     * locks the mutex and returns true.
+     */
+    bool tryLock();
+
+    /**
+     * Unlock the mutex.
+     */
+    void unlock();
+};
+
+//------------------------------------------------------------------------------
+
+} /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
+
+//------------------------------------------------------------------------------
+
+#endif // 0, The public interface of Mutex
+
+//------------------------------------------------------------------------------
+
+#include "config.h"
+
+//------------------------------------------------------------------------------
+
+#if TARGET_API_POSIX
+
+#include "_posix/Mutex.h"
+
+//------------------------------------------------------------------------------
+
+#elif TARGET_API_WIN32 || TARGET_API_WIN64
+
+#include "_win32/Mutex.h"
+
+//------------------------------------------------------------------------------
+
+#else
+
+#error "Not implemented for this target!"
+
+#endif
+
+//------------------------------------------------------------------------------
+#endif // HU_VARADIISTVAN_SCPL_MUTEX_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/hu/varadiistvan/scpl/_posix/Mutex.h
===================================================================
--- src/hu/varadiistvan/scpl/_posix/Mutex.h	(revision 4)
+++ src/hu/varadiistvan/scpl/_posix/Mutex.h	(revision 4)
@@ -0,0 +1,132 @@
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of VSCPL, a simple cross-platform utility library
+
+// 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 HU_VARADIISTVAN_SCPL_MUTEX_H
+#define HU_VARADIISTVAN_SCPL_MUTEX_H
+//------------------------------------------------------------------------------
+
+#include <pthread.h>
+
+//------------------------------------------------------------------------------
+
+namespace hu { namespace varadiistvan { namespace scpl {
+
+//------------------------------------------------------------------------------
+
+/**
+ * Mutual exclusion device for POSIX threads.
+ */
+class Mutex
+{
+private:
+    /**
+     * The mutex structure.
+     */
+    pthread_mutex_t mutex;
+
+public:
+    /**
+     * Construct the mutex.
+     */
+    Mutex();
+
+    /**
+     * Destroy the mutex.
+     */
+    ~Mutex();
+
+    /**
+     * Lock the mutex. If the mutex is locked by another thread, the
+     * caller one waits until the other thread releases the mutex.
+     */
+    void lock();
+
+    /**
+     * Try to lock the mutex. If the mutex is locked by another
+     * thread, this function returns false immediately. Otherwise it
+     * locks the mutex and returns true.
+     */
+    bool tryLock();
+
+    /**
+     * Unlock the mutex.
+     */
+    void unlock();
+
+    friend class CondVar;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline Mutex::Mutex()
+{
+    pthread_mutex_init(&mutex, 0);
+}
+
+//------------------------------------------------------------------------------
+
+inline Mutex::~Mutex()
+{
+    pthread_mutex_destroy(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+inline void Mutex::lock()
+{
+    pthread_mutex_lock(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+inline bool Mutex::tryLock()
+{
+    return pthread_mutex_trylock(&mutex)==0;
+}
+
+//------------------------------------------------------------------------------
+
+inline void Mutex::unlock()
+{
+    pthread_mutex_unlock(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
+
+//------------------------------------------------------------------------------
+#endif // HU_VARADIISTVAN_SCPL_MUTEX_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/hu/varadiistvan/scpl/_win32/Mutex.h
===================================================================
--- src/hu/varadiistvan/scpl/_win32/Mutex.h	(revision 4)
+++ src/hu/varadiistvan/scpl/_win32/Mutex.h	(revision 4)
@@ -0,0 +1,132 @@
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of VSCPL, a simple cross-platform utility library
+
+// 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 HU_VARADIISTVAN_SCPL_MUTEX_H
+#define HU_VARADIISTVAN_SCPL_MUTEX_H
+//------------------------------------------------------------------------------
+
+#include <windows.h>
+
+//------------------------------------------------------------------------------
+
+namespace hu { namespace varadiistvan { namespace scpl {
+
+//------------------------------------------------------------------------------
+
+/**
+ * Mutual exclusion device implementation for Windows.
+ */
+class Mutex
+{
+private:
+    /**
+     * The handle of the mutex.
+     */
+    CRITICAL_SECTION mutex;
+
+public:
+    /**
+     * Construct the mutex.
+     */
+    Mutex();
+
+    /**
+     * Destroy the mutex.
+     */
+    ~Mutex();
+
+    /**
+     * Lock the mutex. If the mutex is locked by another thread, the
+     * caller one waits until the other thread releases the mutex.
+     */
+    void lock();
+
+    /**
+     * Try to lock the mutex. If the mutex is locked by another
+     * thread, this function returns false immediately. Otherwise it
+     * locks the mutex and returns true.
+     */
+    bool tryLock();
+
+    /**
+     * Unlock the mutex.
+     */
+    void unlock();
+
+    friend class CondVar;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline Mutex::Mutex()
+{
+    InitializeCriticalSection(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+inline Mutex::~Mutex()
+{
+    DeleteCriticalSection(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+inline void Mutex::lock()
+{
+    EnterCriticalSection(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+inline bool Mutex::tryLock()
+{
+    return TryEnterCriticalSection(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+inline void Mutex::unlock()
+{
+    LeaveCriticalSection(&mutex);
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
+
+//------------------------------------------------------------------------------
+#endif // HU_VARADIISTVAN_SCPL_MUTEX_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/hu/varadiistvan/scpl/hgccwrapper
===================================================================
--- src/hu/varadiistvan/scpl/hgccwrapper	(revision 4)
+++ src/hu/varadiistvan/scpl/hgccwrapper	(revision 4)
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+# Create a skeleton of a wrapper header for a certain class
+
+classname=$1
+uppername=`echo ${classname} | tr a-z A-Z`
+
+cat > "${classname}.h" <<EOF
+// Copyright (c) 2013 by István Váradi
+
+// This file is part of VSCPL, a simple cross-platform utility library
+
+// 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 HU_VARADIISTVAN_SCPL_${uppername}_H
+#define HU_VARADIISTVAN_SCPL__${uppername}_H
+//------------------------------------------------------------------------------
+
+#if 0 // The public interface of ${classname}
+
+//------------------------------------------------------------------------------
+
+namespace hu { namespace varadiistvan { namespace scpl {
+
+//------------------------------------------------------------------------------
+
+class ${classname}
+{
+public:
+};
+
+//------------------------------------------------------------------------------
+
+} /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
+
+//------------------------------------------------------------------------------
+
+#endif // 0, The public interface of ${classname}
+
+//------------------------------------------------------------------------------
+
+#include "config.h"
+
+//------------------------------------------------------------------------------
+
+#if TARGET_API_POSIX
+
+#include "_posix/${classname}.h"
+
+//------------------------------------------------------------------------------
+
+#elif TARGET_API_WIN32 || TARGET_API_WIN64
+
+#include "_win32/${classname}.h"
+
+//------------------------------------------------------------------------------
+
+#else
+
+#error "Not implemented for this target!"
+
+#endif
+
+//------------------------------------------------------------------------------
+#endif // HU_VARADIISTVAN_SCPL_${uppername}_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
+EOF
+
+hg add "${classname}.h"
Index: test/Makefile.am
===================================================================
--- test/Makefile.am	(revision 2)
+++ test/Makefile.am	(revision 4)
@@ -1,12 +1,15 @@
 INCLUDES=-I$(top_srcdir)/src -I../src/hu/varadiistvan/scpl
 
-noinst_PROGRAMS=testthread
+noinst_PROGRAMS=testthread testmutex
+
+LDADD=../src/hu/varadiistvan/scpl/libvscpl.la
+if TARGET_API_POSIX
+LDADD+=-lpthread
+endif
+if TARGET_API_WIN32
+AM_LDFLAGS=-static -static-libgcc -static-libstdc++
+endif
 
 testthread_SOURCES=testthread.cc
-testthread_LDADD=../src/hu/varadiistvan/scpl/libvscpl.la
-if TARGET_API_POSIX
-testthread_LDADD+=-lpthread
-endif
-if TARGET_API_WIN32
-testthread_LDFLAGS=-static -static-libgcc -static-libstdc++
-endif
+
+testmutex_SOURCES=testmutex.cc
Index: test/testmutex.cc
===================================================================
--- test/testmutex.cc	(revision 4)
+++ test/testmutex.cc	(revision 4)
@@ -0,0 +1,162 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of VSCPL, a simple cross-platform utility library
+
+// 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 <hu/varadiistvan/scpl/Thread.h>
+#include <hu/varadiistvan/scpl/Mutex.h>
+
+#include <cstdlib>
+#include <cstdio>
+#include <cassert>
+
+#include <signal.h>
+
+#include <sys/time.h>
+
+//------------------------------------------------------------------------------
+
+using hu::varadiistvan::scpl::Thread;
+using hu::varadiistvan::scpl::Mutex;
+
+//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
+
+class Thread1 : public Thread
+{
+private:
+    Mutex& mutex;
+
+public:
+    Thread1(Mutex& mutex);
+
+    virtual void run();
+};
+
+//------------------------------------------------------------------------------
+
+inline Thread1::Thread1(Mutex& mutex) :
+    mutex(mutex)
+{
+}
+
+//------------------------------------------------------------------------------
+
+void Thread1::run()
+{
+    printf("Thread1::run: locking mutex\n");
+    mutex.lock();
+    printf("Thread1::run: locked mutex, sleeping...\n");
+
+    sleep(5000);
+
+    printf("Thread1::run: woke up, releasing mutex\n");
+    mutex.unlock();
+
+    printf("Thread1::run: released mutex, sleeping\n");
+
+    sleep(1000);
+
+    printf("Thread1::run: woke up, locking mutex\n");
+    mutex.lock();
+    printf("Thread1::run: locked mutex, releasing\n");
+    mutex.unlock();
+
+}
+
+//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
+
+class Thread2 : public Thread
+{
+private:
+    Mutex& mutex;
+
+public:
+    Thread2(Mutex& mutex);
+
+    virtual void run();
+};
+
+//------------------------------------------------------------------------------
+
+inline Thread2::Thread2(Mutex& mutex) :
+    mutex(mutex)
+{
+}
+
+//------------------------------------------------------------------------------
+
+void Thread2::run()
+{
+    printf("Thread2::run: sleeping\n");
+    sleep(1000);
+
+    printf("Thread2::run: woke up, trying to lock mutex...\n");
+
+    while(!mutex.tryLock()) {
+        sleep(200);
+    }
+
+    printf("Thread2::run: locked mutex, sleeping...\n");
+    sleep(5000);
+    printf("Thread2::run: woke up, releasing mutex\n");
+    mutex.unlock();
+}
+
+//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
+
+int main()
+{
+    Mutex mutex;
+
+    Thread1 thread1(mutex);
+    Thread2 thread2(mutex);
+
+    printf("Starting threads\n");
+    thread1.start();
+    thread2.start();
+
+
+    printf("Waiting for thread1\n");
+    thread1.join();
+    printf("Waiting for thread2\n");
+    thread2.join();
+    printf("Both threads returned\n");
+
+    return 0;
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
