/*	XGThreadEvent.cpp
 *
 *		Thread events.  One thread waits on an event, another
 *	thread signals the event to wake the first thread up.
 *
 *	Mod: 6 Feb 99: Changed to make work with X Windows
 */

/**********************************************************************

  XGThreadEvent.cpp is Copyright 1998-1999 Sassafras Software Inc.
  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>
#include <XThreadEvent.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGThreadEvent::XGThreadEvent
 *
 *		Construct me. Defer to the Microsoft Windows routines if
 *	this is on Windows, otherwise, use the other YAAF classes to
 *	make this thing fly
 *
 *		Input:
 *
 *			manual		if 'true', after a thread returns from
 *						Wait(), it must explicitly clear the
 *						event by calling Reset().
 *						if 'false', this will automatically clear
 *						the event on return from Wait().
 *
 *			state		the state of the event when this is
 *						created.
 */

XGThreadEvent::XGThreadEvent (bool manual, bool state)
{
#if (OPT_MACOS == 1) || (OPT_XWIN == 1)
	fState = state;
	fManual = manual;
#endif

#if OPT_WINOS == 1
	fHandle = CreateEvent(NULL, manual, state, NULL);
#endif
}

/*$	XGThreadEvent::~XGThreadEvent
 *
 *		Destruct me. This releases the memory allocated by this
 *	XGThreadEvent. The behavior of this operation is undefined
 *	if a thread is currently waiting on an event.
 */

XGThreadEvent::~XGThreadEvent (void)
{
#if OPT_WINOS == 1
	if (fHandle) CloseHandle(fHandle);
#endif
}

/************************************************************************/
/*																		*/
/*	Event Object														*/
/*																		*/
/************************************************************************/

/*$	XGThreadEvent::Set
 *
 *		Set the state of this event object. If a thread is currently
 *	waiting on this event, that thread is permitted to execute, and the
 *	state of this object is modified according to the parameters passed
 *	when this was first created.
 */

void XGThreadEvent::Set(void)
{
#if (OPT_MACOS == 1) || (OPT_XWIN == 1)
	fSemaphore.EnterCritical();
	fState = true;
	fSemaphore.LeaveCritical();
#endif

#if OPT_WINOS == 1
	if (fHandle)
		SetEvent(fHandle);
#endif
}

/*$	XGThreadEvent::Reset
 *
 *		Clear the state of this event object. Normally this is called
 *	when the 'manual' flag is set, requiring that this event be reset
 *	manually.
 */

void XGThreadEvent::Reset(void)
{
#if (OPT_MACOS == 1) || (OPT_XWIN == 1)
	fSemaphore.EnterCritical();
	fState = false;
	fSemaphore.LeaveCritical();
#endif

#if OPT_WINOS == 1
	if (fHandle)
		ResetEvent(fHandle);
#endif
}

/*$	XGThreadEvent::Test
 *
 *		Tests the state of this event object. This returns 'true' if
 *	the event state is set, and 'false' if it is reset. (Not currently
 *	supported on Windows.)
 */

bool XGThreadEvent::Test (void)
{
#if (OPT_MACOS == 1) || (OPT_XWIN == 1)
	bool res;

	fSemaphore.EnterCritical();
	res = fState;
	fSemaphore.LeaveCritical();
	return res;
#endif

#if OPT_WINOS == 1
	return(true);
#endif
}

/*$	XGThreadEvent::Wait
 *
 *		Wait for this event object to be set, or for the
 *	timeout. This takes time measured in 1/100ths of a second.
 *
 *		This returns 'true' if this returns because of an
 *	event, and 'false' if this times out.
 */

bool XGThreadEvent::Wait (unsigned long timeout)
{
#if (OPT_MACOS == 1) || (OPT_XWIN == 1)
	bool res = false;

	if (timeout) {
		timeout *= 6;		/* convert 100ths to 60ths */
		timeout /= 100;
		timeout += LMGetTicks();
	}
	while (timeout == 0 || timeout > LMGetTicks()) {
		fSemaphore.EnterCritical();
		if (true == (res = fState)) {
			fState = fManual;
			fSemaphore.LeaveCritical();
			break;
		}
		fSemaphore.LeaveCritical();

		YieldToAnyThread();
	}
	return(res);
#endif

#if OPT_WINOS == 1
	if (!fHandle) {
		Sleep(timeout);
	} else {
		DWORD res = WaitForSingleObject(fHandle, timeout);
		if (res != WAIT_TIMEOUT)
			return(true);
	}
	return(false);
#endif
}
