Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# Event.h

Go to the documentation of this file.

    00001 /* -*- C++ -*- */
    00002
    00003 /****************************************************************************
    00004 ** Copyright (c) quickfixengine.org  All rights reserved.
    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 "CallStack.h"
    00027 #include <math.h>
    00028
    00029 #ifndef _MSC_VER
    00030 #include <pthread.h>
    00031 #include <cmath>
    00032 #endif
    00033
    00034 namespace FIX
    00035 {
    00037 class Event
    00038 {
    00039 public:
    00040   Event()
    00041   {
    00042 #ifdef _MSC_VER
    00043     m_event = CreateEvent( 0, false, false, 0 );
    00044 #else
    00045     pthread_mutex_init( &m_mutex, 0 );
    00046     pthread_cond_init( &m_event, 0 );
    00047 #endif
    00048   }
    00049
    00050   ~Event()
    00051   {
    00052 #ifdef _MSC_VER
    00053     CloseHandle( m_event );
    00054 #else
    00055     pthread_cond_destroy( &m_event );
    00056     pthread_mutex_destroy( &m_mutex );
    00057 #endif
    00058   }
    00059
    00060   void signal()
    00061   {
    00062 #ifdef _MSC_VER
    00063     SetEvent( m_event );
    00064 #else
    00065     pthread_mutex_lock( &m_mutex );
    00066     pthread_cond_broadcast( &m_event );
    00067     pthread_mutex_unlock( &m_mutex );
    00068 #endif
    00069   }
    00070
    00071   void wait( double s )
    00072   {
    00073 #ifdef _MSC_VER
    00074     WaitForSingleObject( m_event, (long)(s * 1000) );
    00075 #else
    00076     pthread_mutex_lock( &m_mutex );
    00077     timespec time, remainder;
    00078     double intpart;
    00079     time.tv_nsec = (long)(modf(s, &intpart) * 1e9);
    00080     time.tv_sec = (int)intpart;
    00081     pthread_cond_timedwait( &m_event, &m_mutex, &time );
    00082     pthread_mutex_unlock( &m_mutex );
    00083 #endif
    00084   }
    00085
    00086 private:
    00087 #ifdef _MSC_VER
    00088   HANDLE m_event;
    00089 #else
    00090   pthread_cond_t m_event;
    00091   pthread_mutex_t m_mutex;
    00092 #endif
    00093 };
    00094 }
    00095
    00096 #endif