Event.h
Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /****************************************************************************
00004 ** Copyright (c) 2001-2014
00005 **
00006 ** This file is part of the QuickFIX FIX Engine
00007 **
00008 ** This file may be distributed under the terms of the quickfixengine.org
00009 ** license as defined by quickfixengine.org and appearing in the file
00010 ** LICENSE included in the packaging of this file.
00011 **
00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 **
00015 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00016 **
00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00018 ** not clear to you.
00019 **
00020 ****************************************************************************/
00021 
00022 #ifndef FIX_EVENT_H
00023 #define FIX_EVENT_H
00024 
00025 #include "Utility.h"
00026 #include <math.h>
00027 
00028 #ifndef _MSC_VER
00029 #include <pthread.h>
00030 #include <cmath>
00031 #endif
00032 
00033 namespace FIX
00034 {
00036 class Event
00037 {
00038 public:
00039   Event()
00040   {
00041 #ifdef _MSC_VER
00042     m_event = CreateEvent( 0, false, false, 0 );
00043 #else
00044     pthread_mutex_init( &m_mutex, 0 );
00045     pthread_cond_init( &m_event, 0 );
00046 #endif
00047   }
00048 
00049   ~Event()
00050   {
00051 #ifdef _MSC_VER
00052     CloseHandle( m_event );
00053 #else
00054     pthread_cond_destroy( &m_event );
00055     pthread_mutex_destroy( &m_mutex );
00056 #endif
00057   }
00058 
00059   void signal()
00060   {
00061 #ifdef _MSC_VER
00062     SetEvent( m_event );
00063 #else
00064     pthread_mutex_lock( &m_mutex );
00065     pthread_cond_broadcast( &m_event );
00066     pthread_mutex_unlock( &m_mutex );
00067 #endif
00068   }
00069 
00070   void wait( double s )
00071   {
00072 #ifdef _MSC_VER
00073     WaitForSingleObject( m_event, (long)(s * 1000) );
00074 #else
00075     pthread_mutex_lock( &m_mutex );
00076     timespec time, remainder;
00077     double intpart;
00078     time.tv_nsec = (long)(modf(s, &intpart) * 1e9);
00079     time.tv_sec = (int)intpart;
00080     pthread_cond_timedwait( &m_event, &m_mutex, &time );
00081     pthread_mutex_unlock( &m_mutex );
00082 #endif
00083   }
00084 
00085 private:
00086 #ifdef _MSC_VER
00087   HANDLE m_event;
00088 #else
00089   pthread_cond_t m_event;
00090   pthread_mutex_t m_mutex;
00091 #endif
00092 };
00093 }
00094 
00095 #endif

Generated on Mon Jun 23 2014 23:49:37 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001