// Common, group WinWrap
// see CW Mutex
// Copyright Alexander Liss

#ifndef __MUTEX_H__
#define __MUTEX_H__


#include "kernel.h"

// acquire a lock through a wait function, or by setting bInitialOwner

class Mutex : public Kernel 
{
public:

    // creates a mutex object or opens existing one
Mutex( BOOL bInitialOwner = FALSE, LPCTSTR lpName = NULL, LPSECURITY_ATTRIBUTES lpMutexAttributes = NULL);
    // opens an existing named mutex
Mutex( LPCTSTR lpName, BOOL bInheritHandle = FALSE, DWORD dwDesiredAccess = MUTEX_ALL_ACCESS);

    // release a lock on a mutex
BOOL release();
BOOL creator(){return attributes & 1;}

private:
	DWORD attributes;
};


class FunctionMutex
{
FunctionMutex(const FunctionMutex&);
FunctionMutex& operator=(const FunctionMutex&);
public:

FunctionMutex(Mutex& c):mutex(&c){if(mutex)wait(*mutex);}
~FunctionMutex(){if(mutex)mutex->release();}

private:
	Mutex * mutex;
};


#endif