/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define STRICT
#include <Windows.h>

#ifdef _MSC_VER
#pragma warning (disable: 4786)
#endif

#include <string>
#include <map>
#include <list>
#include <vector>
#include "WinOnly.h"
#include "WinBe.h"


// createSemaphore
sem_id winBe::createSemaphore ()
{
	CRITICAL_SECTION* cs = new CRITICAL_SECTION;
	InitializeCriticalSection (cs);
	return reinterpret_cast<sem_id>(cs);
	//return reinterpret_cast<sem_id>(::CreateMutex (NULL, TRUE, NULL));
}


// deleteSemaphore
void winBe::deleteSemaphore (sem_id sem)
{
	CRITICAL_SECTION* cs = reinterpret_cast<CRITICAL_SECTION*>(sem);
	DeleteCriticalSection (cs);
	delete cs;
	//BOOL success = ::ReleaseMutex (reinterpret_cast<HANDLE>(hLock));
	//success = ::CloseHandle (reinterpret_cast<HANDLE>(hLock));
}


// lockSemaphore
bool winBe::lockSemaphore (sem_id sem)
{
	EnterCriticalSection (reinterpret_cast<CRITICAL_SECTION*>(sem));
	return true;
	//DWORD result = ::WaitForSingleObject (reinterpret_cast<HANDLE>(hLock), INFINITE);
	//return result == WAIT_FAILED ? false : true;
}


// unlockSemaphore
void winBe::unlockSemaphore (sem_id sem)
{
	LeaveCriticalSection (reinterpret_cast<CRITICAL_SECTION*>(sem));
}
