00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include "nvsgcommon.h"
00016
00017 #if defined(_WIN32)
00018 # include "nvsgwindefs.h"
00019 # ifndef NOMINMAX
00020 # define NOMINMAX // avoid problems with Microsoft definitions in windef.h and min/max from stl
00021 # include <windows.h>
00022 # include <winbase.h>
00023 # undef NOMINMAX
00024 # else
00025 # include <windows.h>
00026 # include <winbase.h>
00027 # endif
00028 #endif
00029
00030 #if defined(LINUX)
00031 # include <pthread.h>
00032 #endif
00033
00034 namespace nvutil {
00035
00036 #if !defined(DOXYGEN_IGNORE)
00037
00038 class ISWMRSync
00039 {
00040 public:
00041 NVSG_API virtual bool waitToWrite(size_t milliseconds = 0xFFFFFFFF) const = 0;
00042 NVSG_API virtual void doneWriting() const = 0;
00043 NVSG_API virtual bool waitToRead(size_t milliseconds = 0xFFFFFFFF) const = 0;
00044 NVSG_API virtual void doneReading() const = 0;
00045 };
00046 #endif
00047
00049
00060 class SWMRSync
00061 {
00062 public:
00063 #if !defined(DOXYGEN_IGNORE)
00064
00065
00066 static void enableLocking();
00067 #endif
00068
00070 SWMRSync(void);
00072 virtual ~SWMRSync(void);
00073
00075
00076 NVSG_API bool initialize(const char* name = NULL
00077
00078 );
00079
00081
00091 NVSG_API bool waitToWrite( size_t milliseconds
00092 , size_t& status
00093 ) const;
00094
00096
00099 NVSG_API void doneWriting() const;
00100
00102
00117 NVSG_API bool waitToRead( size_t milliseconds
00118 , size_t& status
00119 ) const;
00120
00122
00126 NVSG_API void doneReading() const;
00127
00128 private:
00129
00130 #if defined(MULTI_PROCESS_AWARE)
00131 #if defined(_WIN32)
00132
00133
00134
00135 HANDLE m_hMutexNoWriter;
00136
00137
00138
00139 HANDLE m_hEventNoReaders;
00140
00141
00142
00143
00144
00145 HANDLE m_hSemNumReaders;
00146
00147
00148
00149 LPCTSTR constructObjName( LPCTSTR lpszPrefix, LPCTSTR lpszSuffix, LPTSTR lpszFullName, size_t cbFullName, bool &bOk) ;
00150 #endif // _WIN32
00151 #else // MULTI_PROCESS_AWARE
00152
00153 void enterCriticalSection() const;
00154 void leaveCriticalSection() const;
00155 void yield() const;
00156
00157 #if defined(_WIN32)
00158 typedef CRITICAL_SECTION SWMRCriticalSection;
00159 #elif defined(LINUX)
00160 typedef pthread_mutex_t SWMRCriticalSection;
00161 #endif
00162
00163 mutable SWMRCriticalSection m_cs;
00164 mutable int m_sharedLockCount;
00165 #endif
00166
00167 static bool m_lockingEnabled;
00168 };
00169
00170 }
00171