summaryrefslogblamecommitdiffstats
path: root/src/OSSupport/Event.cpp
blob: 7007b5dd2c02f9a689dde6191ff8541abd5b2381 (plain) (tree)
1
2
3
4
5
6
7
8
9
 
            





                                                                                                            
                  
                   



 

                          
 





 
                       
 

                                                   
         
                                     
         
                            





 

                                    
                                                                                                                                
                                                                                                                                                   
                                                                        

                                                        
                 
                                                        
                         







                                                                                      
                         

                                                     
                         
                                                                      

                                             




                                                       





 

                      




                                                           




 

// Event.cpp

// Implements the cEvent object representing an OS-specific synchronization primitive that can be waited-for
// Implemented as an Event on Win and as a 1-semaphore on *nix

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "Event.h"
#include "Errors.h"




cEvent::cEvent(void) :
	m_ShouldWait(true)
{
}





void cEvent::Wait(void)
{
	std::unique_lock<std::mutex> Lock(m_Mutex);
	while (m_ShouldWait)
	{
		m_CondVar.wait(Lock);
	}
	m_ShouldWait = true;
}





bool cEvent::Wait(int a_TimeoutMSec)
{
	std::chrono::system_clock::time_point dst = std::chrono::system_clock::now() + std::chrono::milliseconds(a_TimeoutMSec);
	std::unique_lock<std::mutex> Lock(m_Mutex);  // We assume that this lock is acquired without much delay - we are the only user of the mutex
	while (m_ShouldWait && (std::chrono::system_clock::now() < dst))
	{
		switch (m_CondVar.wait_until(Lock, dst))
		{
			case std::cv_status::no_timeout:
			{
				// The wait was successful, check for spurious wakeup:
				if (!m_ShouldWait)
				{
					m_ShouldWait = true;
					return true;
				}
				// This was a spurious wakeup, wait again:
				continue;
			}
			
			case std::cv_status::timeout:
			{
				// The wait timed out, return failure:
				return false;
			}
		}  // switch (wait_until())
	}  // while (m_ShouldWait && not timeout)

	// The wait timed out in the while() condition:
	return false;
}





void cEvent::Set(void)
{
	{
		std::unique_lock<std::mutex> Lock(m_Mutex);
		m_ShouldWait = false;
	}
	m_CondVar.notify_one();
}