SimpleMutex.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief \b [Internal] Encapsulates a mutex
  12. ///
  13. #ifndef __SIMPLE_MUTEX_H
  14. #define __SIMPLE_MUTEX_H
  15. #include "RakMemoryOverride.h"
  16. #if defined(_WIN32)
  17. #include "WindowsIncludes.h"
  18. #else
  19. #include <pthread.h>
  20. #include <sys/types.h>
  21. #endif
  22. #include "Export.h"
  23. namespace RakNet
  24. {
  25. /// \brief An easy to use mutex.
  26. ///
  27. /// I wrote this because the version that comes with Windows is too complicated and requires too much code to use.
  28. /// @remark Previously I used this everywhere, and in fact for a year or two RakNet was totally threadsafe. While doing profiling, I saw that this function was incredibly slow compared to the blazing performance of everything else, so switched to single producer / consumer everywhere. Now the user thread of RakNet is not threadsafe, but it's 100X faster than before.
  29. class RAK_DLL_EXPORT SimpleMutex
  30. {
  31. public:
  32. // Constructor
  33. SimpleMutex();
  34. // Destructor
  35. ~SimpleMutex();
  36. // Locks the mutex. Slow!
  37. void Lock(void);
  38. // Unlocks the mutex.
  39. void Unlock(void);
  40. private:
  41. void Init(void);
  42. #ifdef _WIN32
  43. CRITICAL_SECTION criticalSection; /// Docs say this is faster than a mutex for single process access
  44. #else
  45. pthread_mutex_t hMutex;
  46. #endif
  47. // Not threadsafe
  48. // bool isInitialized;
  49. };
  50. } // namespace RakNet
  51. #endif
粤ICP备19079148号