// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#ifdef WIN32_THREADS
#include <windows.h>
static CRITICAL_SECTION cs;
#elif POSIX_THREADS
#include <pthread.h>
static pthread_mutex_t mtx;
#else
#error "No threading model specified"
#endif

#include "Lock.h"

void lock_setup()
{
#ifdef WIN32_THREADS
    InitializeCriticalSection(&cs);        
#endif        
#ifdef POSIX_THREADS
    pthread_mutex_init(&mtx, NULL);
#endif
}

void lock_destroy()
{
#ifdef WIN32_THREADS
    DeleteCriticalSection(&cs);        
#endif        
#ifdef POSIX_THREADS
    pthread_mutex_destroy(&mtx);
#endif
}

Lock::Lock()
{
#ifdef WIN32_THREADS
    EnterCriticalSection(&cs);        
#endif        
#ifdef POSIX_THREADS
    pthread_mutex_lock(&mtx);
#endif
}

Lock::~Lock()
{
#ifdef WIN32_THREADS
    LeaveCriticalSection(&cs);        
#endif        
#ifdef POSIX_THREADS
    pthread_mutex_unlock(&mtx);
#endif
}
