Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual C++ 10.0

SIP Stack (1 of 3)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Aug 2012CPOL3 min read 30K   1.7K   4  
SIP Stack Implementation on the basis of RFC SIP 3261 Specification
#ifndef WindowsMutex_h
	#define WindowsMutex_h

	#include <windows.h>
	#include <string>

	namespace CoreFW
	{
		class __declspec( dllexport ) FWMutex
		{
		private:
			mutable HANDLE m_hMutex ;
			unsigned long m_ulErrorCode ;

			FWMutex( const FWMutex &MutexObject ) {}
			void operator=( FWMutex &MutexObject ) {}

		public:
			FWMutex()
			{
				m_hMutex = CreateMutex( NULL,                       // no security attributes
										FALSE,                      // initially not owned
										NULL ) ;					// name of mutex

				m_ulErrorCode = GetLastError() ;

				if( m_ulErrorCode == ERROR_INVALID_HANDLE )
					m_hMutex = NULL ;
			}

			FWMutex( const std::string &strMutexName )
			{
				if( !strMutexName.empty() )
				{
					m_hMutex = CreateMutex( NULL,                       // no security attributes
											FALSE,                      // initially not owned
											strMutexName.c_str() ) ;	// name of mutex
				}
				else
				{
					m_hMutex = CreateMutex( NULL,                       // no security attributes
											FALSE,                      // initially not owned
										    NULL ) ;					// name of mutex
				}

				m_ulErrorCode = GetLastError() ;

				if( m_ulErrorCode == ERROR_INVALID_HANDLE )
					m_hMutex = NULL ;
			}

			virtual ~FWMutex()
			{
				Unlock() ;
				CloseHandle( m_hMutex ) ;
			}

			inline unsigned long GetMutexCreationErrorCode() const
			{
				return m_ulErrorCode ;
			}

			inline bool IsMutexAlreadyExist() const
			{
				return ( m_ulErrorCode == ERROR_ALREADY_EXISTS ) ;
			}

			inline bool IsMutexCreated() const
			{
				return ( m_hMutex != NULL ) ;
			}

			bool Lock() const
			{
				if( m_hMutex != NULL )
				{
					if( WaitForSingleObject( m_hMutex, INFINITE ) == WAIT_OBJECT_0 )
						return true ;
				}

				return false ;
			}

			bool Unlock() const
			{
				if( m_hMutex != NULL )
				{
					if( ReleaseMutex( m_hMutex ) == TRUE )
						return true ;
				}

				return false ;
			}
		} ;

	} // End namespace

#endif // WindowsMutex_h

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Hatim Haidry

VC++, Technical Architect

India

haidryhatim@gmail.com

Comments and Discussions