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

                                                                                                  


                                                                                              
                  
                   



 

                          
 





 
                       
 

                                                   
         
                                     
         
                            





 
                                         
 
                                                                                               
                                                                                                                                                   
                                                                         

                                                        
                 
                                                        
                         







                                                                                      
                         

                                                     
                         
                                                                      

                                             


                                                 
                                                     
                     





 

                      




                                                           




 

// Event.cpp

// Interfaces to the cEvent object representing a synchronization primitive that can be waited-for
// Implemented using C++11 condition variable and mutex

#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(unsigned a_TimeoutMSec)
{
	auto 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();
}