mutex.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifdef _WIN32
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #define _CRT_NONSTDC_NO_WARNINGS
  4. #include <windows.h>
  5. #include <io.h>
  6. #include "../Win32/w32def.h"
  7. #endif // _WIN32
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #ifdef __linux__
  13. #include <sys/file.h>
  14. #include <sys/ipc.h>
  15. #include <sys/sem.h>
  16. #include <linux/limits.h>
  17. #include <sys/shm.h>
  18. #endif // __linux__
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include "defines.h"
  23. #include "mutex.h"
  24. /////////////////////////////////////////////////////////////////////////////
  25. #define _IS_VALID_SHMID(id) ((id) >= 0)
  26. /////////////////////////////////////////////////////////////////////////////
  27. CGlobalMutex::CGlobalMutex(void) : m_nShmID(_INVALID_ID), m_nSigRefCount(0), m_pMutex(NULL)
  28. {
  29. memset(&m_mutexAttr, 0, sizeof(m_mutexAttr));
  30. ::pthread_mutexattr_init(&m_mutexAttr);
  31. ::pthread_mutexattr_setpshared(&m_mutexAttr, PTHREAD_PROCESS_SHARED);
  32. ::pthread_mutexattr_settype(&m_mutexAttr, PTHREAD_MUTEX_RECURSIVE);
  33. ::pthread_mutexattr_setrobust(&m_mutexAttr, PTHREAD_MUTEX_ROBUST);
  34. }
  35. CGlobalMutex::~CGlobalMutex(void)
  36. {
  37. Release();
  38. }
  39. int CGlobalMutex::Create(const uuid_t &ruuid, const char *pszDir)
  40. {
  41. if(!pszDir)
  42. pszDir = "";
  43. size_t nLenReq = strlen(pszDir) + _UUID_STRING_LEN + 10;
  44. if(nLenReq > PATH_MAX)
  45. {
  46. TRACE("CGlobalMutex::Create: directory name too long\n");
  47. errno = EINVAL;
  48. return -1;
  49. }
  50. int nRet = -1;
  51. char szUuid[_UUID_STRING_LEN + 1];
  52. char szShm[PATH_MAX];
  53. _uuid_unparse(&ruuid, szUuid, sizeof(szUuid));
  54. sprintf(szShm, "%s/%s.gfa.mtx", pszDir, szUuid);
  55. int nFdShm;
  56. if((nFdShm = ::open(szShm, O_RDWR | O_CREAT | O_TRUNC, _NUMBER_OF_THE_BEAST)) >= 0)
  57. {
  58. key_t shmKey;
  59. if((shmKey = ::ftok(szShm, 'R')) >= 0)
  60. {
  61. if((m_nShmID = ::shmget(shmKey, sizeof(pthread_mutex_t), IPC_CREAT | _NUMBER_OF_THE_BEAST)) >= 0)
  62. {
  63. struct shmid_ds sds;
  64. memset(&sds, 0, sizeof(sds));
  65. if(!::shmctl(m_nShmID, IPC_STAT, (struct shmid_ds*)&sds))
  66. {
  67. m_pMutex = (pthread_mutex_t*)::shmat(m_nShmID, NULL, 0);
  68. if(!sds.shm_atime)
  69. {
  70. if(!(nRet = ::pthread_mutex_init(m_pMutex, &m_mutexAttr)))
  71. {
  72. TRACE("CGlobalMutex::Create: Created new Mutex\n");
  73. nRet = 1;
  74. }
  75. else
  76. {
  77. TRACE("CGlobalMutex::Create failed! Code: %d\n", nRet);
  78. nRet = -1;
  79. }
  80. }
  81. else
  82. {
  83. TRACE("CGlobalMutex::Create: Opened existing Mutex\n");
  84. nRet = 0;
  85. }
  86. }
  87. else
  88. {
  89. TRACE("CGlobalMutex::Create: IPC_STAT failed - errno: %d!\n", errno);
  90. }
  91. }
  92. else
  93. {
  94. TRACE("CGlobalMutex::Create: Failed to create SHM on Key: 0x%08X - errno: %d!\n", shmKey, errno);
  95. }
  96. }
  97. else
  98. {
  99. TRACE("CGlobalMutex::Create: Failed to create shmKey - errno: %d!\n", errno);
  100. }
  101. close(nFdShm);
  102. }
  103. else
  104. {
  105. TRACE("CGlobalMutex::Create: Failed to open '%s' - errno: %d!\n", szShm, errno);
  106. }
  107. return nRet;
  108. }
  109. long CGlobalMutex::Release(void)
  110. {
  111. struct shmid_ds sds;
  112. sds.shm_nattch = (shmatt_t)-1;
  113. if(_IS_VALID_SHMID(m_nShmID) && m_pMutex)
  114. {
  115. if(!::shmctl(m_nShmID, IPC_STAT, (struct shmid_ds*)&sds))
  116. {
  117. if((long)sds.shm_nattch > 1)
  118. {
  119. ::pthread_mutexattr_destroy(&m_mutexAttr);
  120. ::shmdt(m_pMutex);
  121. ::shmctl(m_nShmID, IPC_STAT, (struct shmid_ds*)&sds);
  122. TRACE("CGlobalMutex::Release: Still attached: %ld!\n", (long)(sds.shm_nattch));
  123. }
  124. else if((long)sds.shm_nattch == 1)
  125. {
  126. ::pthread_mutex_destroy(m_pMutex);
  127. ::pthread_mutexattr_destroy(&m_mutexAttr);
  128. ::shmdt(m_pMutex);
  129. ::shmctl(m_nShmID, IPC_RMID, NULL);
  130. sds.shm_nattch = 0;
  131. TRACE("CGlobalMutex::Release: Mutex removed!\n");
  132. }
  133. }
  134. else
  135. {
  136. sds.shm_nattch = (shmatt_t)-1;
  137. TRACE("CGlobalMutex::Release: IPC_STAT failed!\n");
  138. }
  139. m_pMutex = NULL;
  140. m_nShmID = _INVALID_ID;
  141. }
  142. return (long)sds.shm_nattch;
  143. }
  144. bool CGlobalMutex::Lock(void)
  145. {
  146. if(m_pMutex)
  147. {
  148. int nRet = ::pthread_mutex_lock(m_pMutex);
  149. if(nRet == EOWNERDEAD)
  150. {
  151. TRACE("CGlobalMutex::Lock: Former owner process has died - gained ownership!\n");
  152. ::pthread_mutex_consistent(m_pMutex);
  153. nRet = 0;
  154. }
  155. // TRACE("CGlobalMutex::Lock: Locked: %d.\n", m_nShmID);
  156. return !nRet;
  157. }
  158. else
  159. {
  160. TRACE("CGlobalMutex::Lock: Invalid Mutex: %p!\n", m_pMutex);
  161. errno = EINVAL;
  162. return false;
  163. }
  164. }
  165. bool CGlobalMutex::TryLock(void)
  166. {
  167. if(m_pMutex)
  168. {
  169. int nRet = ::pthread_mutex_trylock(m_pMutex);
  170. if(nRet == EOWNERDEAD)
  171. {
  172. TRACE("CGlobalMutex::TryLock: Former owner process has died - gained ownership!\n");
  173. ::pthread_mutex_consistent(m_pMutex);
  174. nRet = 0;
  175. }
  176. return !nRet;
  177. }
  178. else
  179. {
  180. TRACE("CGlobalMutex::TryLock: Invalid Mutex: %p!\n", m_pMutex);
  181. errno = EINVAL;
  182. return false;
  183. }
  184. }
  185. bool CGlobalMutex::Unlock(void)
  186. {
  187. if(m_pMutex)
  188. {
  189. // TRACE("CGlobalMutex::Unlock: Unlocked: %d.\n", m_nShmID);
  190. return !::pthread_mutex_unlock(m_pMutex);
  191. }
  192. else
  193. {
  194. TRACE("CGlobalMutex::Unlock: Invalid Mutex: %p!\n", m_pMutex);
  195. errno = EINVAL;
  196. return false;
  197. }
  198. }
  199. bool CGlobalMutex::LockAndSigBlock(sigset_t *pss)
  200. {
  201. if(m_pMutex && pss)
  202. {
  203. if(m_nSigRefCount++ == 0)
  204. {
  205. ::sigprocmask(SIG_BLOCK, pss, NULL);
  206. // TRACE("Block: %lu\n", ::pthread_self());
  207. }
  208. return Lock();
  209. }
  210. else
  211. {
  212. TRACE("CGlobalMutex::LockAndSigBlock: Invalid Mutex or sigset: %p!\n", pss);
  213. errno = EINVAL;
  214. }
  215. return false;
  216. }
  217. bool CGlobalMutex::UnlockAndSigUnblock(sigset_t *pss)
  218. {
  219. if(m_pMutex && pss)
  220. {
  221. bool ret = Unlock();
  222. if(--m_nSigRefCount == 0)
  223. {
  224. ::sigprocmask(SIG_UNBLOCK, pss, NULL);
  225. // TRACE("Unblock: %lu\n", ::pthread_self());
  226. }
  227. return ret;
  228. }
  229. else
  230. {
  231. TRACE("CGlobalMutex::UnlockAndSigBlock: Invalid Mutex or sigset: %p!\n", pss);
  232. errno = EINVAL;
  233. }
  234. return false;
  235. }