mutex.h

Engine/source/platform/threads/mutex.h

More...

Classes:

class
class

Helper for simplifying mutex locking code.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/types.h"
 25#include "platform/platformAssert.h"
 26
 27#ifndef _PLATFORM_THREADS_MUTEX_H_
 28#define _PLATFORM_THREADS_MUTEX_H_
 29
 30// Forward ref used by platform code
 31struct PlatformMutexData;
 32
 33class Mutex
 34{
 35protected:
 36   PlatformMutexData *mData;
 37
 38public:
 39   Mutex();
 40   virtual ~Mutex();
 41
 42   virtual bool lock(bool block = true);
 43   virtual void unlock();
 44
 45   // Old API so that we don't have to change a load of code
 46   static void *createMutex()
 47   {
 48      Mutex *mutex = new Mutex;
 49      return (void *)mutex;
 50   }
 51
 52   static void destroyMutex(void *mutex)
 53   {
 54      Mutex *realMutex = reinterpret_cast<Mutex *>(mutex);
 55      delete realMutex;
 56   }
 57
 58   static bool lockMutex(void *mutex, bool block = true)
 59   {
 60      Mutex *realMutex = reinterpret_cast<Mutex *>(mutex);
 61      return realMutex->lock(block);
 62   }
 63
 64   static void unlockMutex(void *mutex)
 65   {
 66      Mutex *realMutex = reinterpret_cast<Mutex *>(mutex);
 67      realMutex->unlock();
 68   }
 69};
 70
 71/// Helper for simplifying mutex locking code.
 72///
 73/// This class will automatically unlock a mutex that you've
 74/// locked through it, saving you from managing a lot of complex
 75/// exit cases. For instance:
 76///
 77/// @code
 78/// MutexHandle handle;
 79/// handle.lock(myMutex);
 80///
 81/// if(error1)
 82///   return; // Auto-unlocked by handle if we leave here - normally would
 83///           // leave the mutex locked, causing much pain later.
 84///
 85/// handle.unlock();
 86/// @endcode
 87class MutexHandle
 88{
 89private:
 90   void *mMutexPtr;
 91
 92public:
 93   MutexHandle()
 94      : mMutexPtr(NULL)
 95   {
 96   }
 97
 98   ~MutexHandle()
 99   {
100      if(mMutexPtr)
101         unlock();
102   }
103
104   bool lock(void *mutex, bool blocking=false)
105   {
106      AssertFatal(!mMutexPtr, "MutexHandle::lock - shouldn't be locking things twice!");
107
108      bool ret = Mutex::lockMutex(mutex, blocking);
109
110      if(ret)
111      {
112         // We succeeded, do book-keeping.
113         mMutexPtr = mutex;
114      }
115
116      return ret;
117   }
118
119   void unlock()
120   {
121      if(mMutexPtr)
122      {
123         Mutex::unlockMutex(mMutexPtr);
124         mMutexPtr = NULL;
125      }
126   }
127
128};
129
130#endif // _PLATFORM_THREADS_MUTEX_H_
131