x86UNIXMutex.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/console.h"
  23. #include "platformX86UNIX/platformX86UNIX.h"
  24. #include "platform/platformMutex.h"
  25. #include "platformX86UNIX/x86UNIXMutex.h"
  26. #include <pthread.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. void * Mutex::createMutex()
  32. {
  33. pthread_mutex_t *mutex;
  34. pthread_mutexattr_t attr;
  35. pthread_mutexattr_init(&attr);
  36. pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
  37. mutex = new pthread_mutex_t;
  38. pthread_mutex_init(mutex, &attr);
  39. return((void*)mutex);
  40. }
  41. void Mutex::destroyMutex(void * mutex)
  42. {
  43. pthread_mutex_t *pt_mutex = reinterpret_cast<pthread_mutex_t*>(mutex);
  44. AssertFatal(pt_mutex, "Mutex::destroyMutex: invalid mutex");
  45. pthread_mutex_destroy(pt_mutex);
  46. delete pt_mutex;
  47. }
  48. bool Mutex::lockMutex(void * mutex, bool block)
  49. {
  50. pthread_mutex_t *pt_mutex = reinterpret_cast<pthread_mutex_t*>(mutex);
  51. AssertFatal(pt_mutex, "Mutex::lockMutex: invalid mutex");
  52. if(block)
  53. {
  54. return pthread_mutex_lock(pt_mutex) == 0;
  55. }
  56. else
  57. {
  58. return pthread_mutex_trylock(pt_mutex) == 0;
  59. }
  60. }
  61. void Mutex::unlockMutex(void * mutex)
  62. {
  63. pthread_mutex_t *pt_mutex = reinterpret_cast<pthread_mutex_t*>(mutex);
  64. AssertFatal(pt_mutex, "Mutex::unlockMutex: invalid mutex");
  65. pthread_mutex_unlock(pt_mutex);
  66. }
  67. ProcessMutex::ProcessMutex()
  68. {
  69. mFD = -1;
  70. }
  71. ProcessMutex::~ProcessMutex()
  72. {
  73. release();
  74. }
  75. bool ProcessMutex::acquire(const char *mutexName)
  76. {
  77. if (mFD != -1)
  78. return false;
  79. dSprintf(
  80. mLockFileName, LockFileNameSize,
  81. "/tmp/%s.lock", mutexName);
  82. mFD = open(mLockFileName,
  83. O_CREAT | O_RDWR,
  84. S_IRUSR | S_IRGRP | S_IROTH |
  85. S_IWUSR | S_IWGRP | S_IWOTH);
  86. if (mFD == -1)
  87. {
  88. Con::printf("Couldn't open file: %s", mLockFileName);
  89. return false;
  90. }
  91. struct flock lock;
  92. //lock.l_type = F_RDLCK;
  93. lock.l_type = F_WRLCK;
  94. lock.l_whence = SEEK_SET;
  95. lock.l_start = 0;
  96. lock.l_len = 0;
  97. lock.l_pid = getpid();
  98. if (fcntl(mFD, F_SETLK, &lock) == -1)
  99. {
  100. Con::printf("Couldn't lock file: %s, %s",
  101. mLockFileName, strerror(errno));
  102. if (fcntl(mFD, F_GETLK, &lock) != -1)
  103. Con::printf("Lock owned by pid: %d", lock.l_pid);
  104. Con::printf("Remove the file if lock is stale");
  105. close(mFD);
  106. mFD = -1;
  107. return false;
  108. }
  109. return true;
  110. }
  111. void ProcessMutex::release()
  112. {
  113. if (mFD != -1)
  114. {
  115. close(mFD);
  116. mFD = -1;
  117. unlink(mLockFileName);
  118. }
  119. }
  120. ConsoleFunction(debug_testx86unixmutex, void, 1, 1, "debug_testx86unixmutex()")
  121. {
  122. void* mutex = Mutex::createMutex();
  123. AssertFatal(mutex, "couldn't create mutex");
  124. Con::printf("created mutex");
  125. Mutex::lockMutex(mutex);
  126. Con::printf("locked mutex");
  127. Mutex::unlockMutex(mutex);
  128. Con::printf("unlocked mutex");
  129. Mutex::destroyMutex(mutex);
  130. Con::printf("destroyed mutex");
  131. }