Mutex.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef MYMUTEX_H
  17. #define MYMUTEX_H
  18. #ifdef WIN32
  19. #include <WinSock2.h>
  20. #include <windows.h>
  21. #else
  22. #include <pthread.h>
  23. #include "../common/unix.h"
  24. #endif
  25. #include "../common/types.h"
  26. #include <string>
  27. #include <map>
  28. #define MUTEX_ATTRIBUTE_FAST 1
  29. #define MUTEX_ATTRIBUTE_RECURSIVE 2
  30. #define MUTEX_ATTRIBUTE_ERRORCHK 3
  31. #define MUTEX_TIMEOUT_MILLISECONDS 10000
  32. class CriticalSection {
  33. public:
  34. CriticalSection(int attribute = MUTEX_ATTRIBUTE_FAST);
  35. ~CriticalSection();
  36. void lock();
  37. void unlock();
  38. bool trylock();
  39. private:
  40. #ifdef WIN32
  41. CRITICAL_SECTION CSMutex;
  42. #else
  43. pthread_mutex_t CSMutex;
  44. pthread_mutexattr_t type_attribute;
  45. #endif
  46. };
  47. class Mutex {
  48. public:
  49. Mutex();
  50. ~Mutex();
  51. void lock();
  52. void unlock();
  53. bool trylock();
  54. void readlock(const char* function = 0, int32 line = 0);
  55. void releasereadlock(const char* function = 0, int32 line = 0);
  56. bool tryreadlock(const char* function = 0);
  57. void writelock(const char* function = 0, int32 line = 0);
  58. void releasewritelock(const char* function = 0, int32 line = 0);
  59. bool trywritelock(const char* function = 0);
  60. void waitReaders(const char* function = 0, int32 line = 0);
  61. void SetName(string in_name);
  62. private:
  63. CriticalSection CSRead;
  64. CriticalSection CSWrite;
  65. CriticalSection* CSLock;
  66. #ifdef DEBUG //Used for debugging only
  67. CriticalSection CSStack;
  68. map<string, int32> stack;
  69. #endif
  70. int readers;
  71. bool writing;
  72. volatile bool mlocked;
  73. string name;
  74. };
  75. class LockMutex {
  76. public:
  77. LockMutex(Mutex* in_mut, bool iLock = true);
  78. ~LockMutex();
  79. void unlock();
  80. void lock();
  81. private:
  82. bool locked;
  83. Mutex* mut;
  84. };
  85. #endif