Event.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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