Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

SWMRSync.h

Go to the documentation of this file.
00001 // Copyright NVIDIA Corporation 2002-2004
00002 // TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
00003 // *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
00004 // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
00005 // AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
00006 // BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
00007 // WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00008 // BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
00009 // ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
00010 // BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 
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 // Single write, multiple read synchronization interface.
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   // Enables locking. This should be called once at initialization time of the nvsg library.
00065   // Sell also nvsg::nvsgInitialize
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   /* This mutex guards access to the other objects
00133    * managed by this data structure and also indicates 
00134    * whether any writer threads are writing. */
00135   HANDLE m_hMutexNoWriter;
00136 
00137   /* This manual-reset event is signaled when
00138    * no reader threads are reading. */
00139   HANDLE m_hEventNoReaders;
00140 
00141   /* This semaphore is used simply as a counter that is
00142    * accessible between multiple processes. It is NOT
00143    * used for thread synchronization.
00144    * The count is the number of reader threads reading. */
00145   HANDLE m_hSemNumReaders;
00146 
00147   /* Helper method to create a name for the semaphores and 
00148    * mutexes, so we can use them over process boundaries */
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; // thread synchronization object
00164   mutable int m_sharedLockCount; // accessed only when inside critical section
00165 #endif
00166 
00167   static bool m_lockingEnabled; // set via enableLocking; false by default
00168 };
00169 
00170 }
00171 

Generated on Tue Mar 1 13:19:20 2005 for NVSGSDK by NVIDIA