Click here to Skip to main content
15,896,727 members
Articles / Desktop Programming / MFC

The Diffraction Grating Calculator

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
30 Oct 2010GPL38 min read 41.5K   678   6  
An MFC Windows program related to Concave Diffraction Gratings using VTK libraries.
// MSMutex.cpp: implementation for the CMSMutex class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MSMutex.h"
#include <stdlib.h>
#include <time.h>

// STL includes here

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
CMSMutex::CMSMutex()
{
   m_szTheMutexName = _T("AnonymousMutex");
   srand( (unsigned)time( NULL ) );
   CString szRand; szRand.Format(_T("_%6d"),rand());
   m_szTheMutexName += szRand;
   // create and ensure the anonymous mutex, with a random part in the name 
   // (one never knows..., I don't trust windows OS)
   m_hWindowsMutex = ::CreateMutex( NULL, FALSE, m_szTheMutexName.GetBuffer(MAX_PATH) );
   m_szTheMutexName.ReleaseBuffer();
   if( !m_hWindowsMutex )
   {
      NULL;
   }
}

CMSMutex::CMSMutex(const TCHAR* namedMutex)
{
   m_szTheMutexName = namedMutex;
   // create and ensure the named mutex
   m_hWindowsMutex = ::CreateMutex( NULL, FALSE, m_szTheMutexName.GetBuffer(MAX_PATH) );
   m_szTheMutexName.ReleaseBuffer();
   if( !m_hWindowsMutex )
   {
      NULL;
   }
}
 
CMSMutex::~CMSMutex()
{
   // first of all: unlock 
   UnLock();
   // cleanup resources
   ::CloseHandle( m_hWindowsMutex );
   m_hWindowsMutex = NULL;
}

//////////////////////////////////////////////////////////////////////
// Interface
//////////////////////////////////////////////////////////////////////

// LOCK interface

// acquire the resource
int CMSMutex::Acquire(void)
{
   if (Lock())
      return LOCK_ACQUIRED;
   else
      return LOCK_NOT_ACQUIRED;
}

int CMSMutex::Acquire(unsigned long nTimeOut)
{
   if (Lock(nTimeOut))
      return LOCK_ACQUIRED;
   else
      return LOCK_NOT_ACQUIRED;
}

// release the resource
int CMSMutex::Release(void)
{
   if (UnLock(true)) // recursively unlock the mutex
      return LOCK_RELEASED;
   else
      return LOCK_NOT_RELEASED;
}

///////////////////////////////////////////////////////////////////////////////
// Implementation
///////////////////////////////////////////////////////////////////////////////

bool CMSMutex::Lock( void )
{
   // call infinite lock
   return(  Lock( INFINITE ) );
}

bool CMSMutex::Lock( unsigned long nTimeOut /* in milliseconds */ )
{
   // try to get mutex-ownership
   DWORD dwResult = ::WaitForSingleObject( m_hWindowsMutex, (DWORD) nTimeOut );
   // return result
   return( WAIT_OBJECT_0 == dwResult );
}

bool CMSMutex::UnLock( bool bRecurse /* = true */ )
{
   // try unlock
   if( ::ReleaseMutex( m_hWindowsMutex ) )
   {
      // cleanup all locks
      if( bRecurse )
      {
         while( ::ReleaseMutex( m_hWindowsMutex ) );
      }
      // return success
      return( true );
   }
   // return failure
   return( false );
}

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 GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
Italy Italy
Senior Software Developer in C/C++ and Oracle.
Ex-physicist holding a Ph.D. on x-ray lasers.

Comments and Discussions