Česky
Kamil Dudka

Share Library (C++)

File detail

Name:DownloadMutexLock.h [Download]
Location: sharelib > src > sharelib
Size:3.1 KB
Last modification:2007-08-27 01:16

Source code

#ifndef MUTEX_LOCK_H
#define MUTEX_LOCK_H
 
/**
 * @file MutexLock.h
 * @brief Platform-independent mutex representation
 * @author Kamil Dudka, xdudka00@gmail.com
 * @date 2007-05-15
 * @ingroup backend
 */
 
#if !defined _LINUX && !defined _WIN32
#  error "Platform was not specified. Add macro _LINUX or _WIN32 to compiler options."
#endif
 
#include "sharelib.h"
 
#ifdef _WIN32
#    include <windows.h>
#    include <limits.h>
#endif // _WIN32
 
#ifdef _LINUX
#    include <unistd.h>
#    include <sys/types.h>
#    include <sys/time.h>
#    include <sys/ipc.h>
#    include <sys/sem.h>
#    include <sys/stat.h>
#    include <errno.h>
#endif //_LINUX
 
namespace Share {
 
  /**
    * @brief Platform-independent mutex representation
    * @ingroup backend
    * @note Implementation of this class was taken from @b MSDTk.
    */
  class Mutex {
    public:
      /**
       * @brief Create mutex
       * @param name Name of mutex to create.
       * @note Parameter name is not used on Linux for now.
       * @throw ShareException Library-specific exception derived from std::bad_alloc
       */
      Mutex (RelocPtr<char> name) throw (ShareException);
 
      /**
       * @brief Destroy mutex
       */
      ~Mutex() throw ();
 
      /**
       * @brief Mutex per-lock specific data.
       * @note This is not used on Linux for now.
       * @ingroup backend
       */
      struct LockData {
#ifdef _WIN32
        HANDLE m_hSemaphore;
#endif
      };
 
      /**
       * Process will be susspended if mutex is already locked.
       * @brief Lock mutex.
       * @param data Pointer to structure containing per-lock specific data.
       * @throw ShareException Library-specific exception derived from std::bad_alloc
       */
      void lock(LockData *data) throw (ShareException);
 
      /**
       * @brief Unlock mutex.
       * @param data Pointer to structure containing per-lock specific data.
       * @throw ShareException Library-specific exception derived from std::bad_alloc
       */
      void unlock(LockData *data) throw (ShareException);
 
    protected:
#ifdef _WIN32
      RelocPtr<char> name_;
#endif // _WIN32
 
#ifdef _LINUX
      int m_iSemaphore;
#endif // _LINUX
 
    private:
      Mutex (const Mutex&);                     ///< Avoid object copying
      void operator=(const Mutex&);             ///< Avoid object copying
  };
 
  /**
   * (Resource Acquisition Is Initialization)
   * @brief Mutex lock RAII object
   * @ingroup backend
   */
  class MutexLock {
    public:
      /**
       * @brief Lock mutex
       * @param mutex Pointer to mutex to lock.
       */
      MutexLock (Mutex *mutex):
        mutex_ (mutex)
      {
        mutex_-> lock (&lockData);
      }
 
      /**
       * @brief Unlock mutex.
       */
      ~MutexLock()
      {
        mutex_-> unlock (&lockData);
      }
 
    private:
      MutexLock (const MutexLock&);
      void operator= (const MutexLock&);
 
    private:
      Mutex *mutex_;
      Mutex::LockData lockData;
  };
 
}
 
#endif // MUTEX_LOCK_H