Mutex Manager.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifdef JA2_PRECOMPILED_HEADERS
  2. #include "JA2 SGP ALL.H"
  3. #elif defined( WIZ8_PRECOMPILED_HEADERS )
  4. #include "WIZ8 SGP ALL.H"
  5. #else
  6. #include "Mutex Manager.h"
  7. #include "debug.h"
  8. #endif
  9. //#define __MUTEX_TYPE
  10. #ifdef __MUTEX_TYPE
  11. //
  12. // Use defines to allocate slots in the mutex manager. Put these defines in LOCAL.H
  13. //
  14. HANDLE MutexTable[MAX_MUTEX_HANDLES];
  15. BOOLEAN InitializeMutexManager(void)
  16. {
  17. UINT32 uiIndex;
  18. //
  19. // Register the Mutex Manager debug topic
  20. //
  21. RegisterDebugTopic(TOPIC_MUTEX, "Mutex Manager");
  22. DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "Initializing the Mutex Manager");
  23. //
  24. // Initialize the table of mutex handles to NULL
  25. //
  26. for (uiIndex = 0; uiIndex < MAX_MUTEX_HANDLES; uiIndex++)
  27. {
  28. MutexTable[uiIndex] = NULL;
  29. }
  30. return TRUE;
  31. }
  32. void ShutdownMutexManager(void)
  33. {
  34. UINT32 uiIndex;
  35. DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "Shutting down the Mutex Manager");
  36. //
  37. // Make sure all mutex handles are closed
  38. //
  39. for (uiIndex = 0; uiIndex < MAX_MUTEX_HANDLES; uiIndex++)
  40. {
  41. if (MutexTable[uiIndex] != NULL)
  42. {
  43. CloseHandle(MutexTable[uiIndex]);
  44. MutexTable[uiIndex] = NULL;
  45. }
  46. }
  47. UnRegisterDebugTopic(TOPIC_MUTEX, "Mutex Manager");
  48. }
  49. BOOLEAN InitializeMutex(UINT32 uiMutexIndex, UINT8 *ubMutexName)
  50. {
  51. MutexTable[uiMutexIndex] = CreateMutex(NULL, FALSE, ubMutexName);
  52. if (MutexTable[uiMutexIndex] == NULL)
  53. {
  54. //
  55. // Mutex creation has failed.
  56. //
  57. DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "ERROR : Mutex initialization has failed.");
  58. return FALSE;
  59. }
  60. return TRUE;
  61. }
  62. BOOLEAN DeleteMutex(UINT32 uiMutexIndex)
  63. {
  64. if (MutexTable[uiMutexIndex] == NULL)
  65. {
  66. //
  67. // Hum ?? We just tried to initialize a mutex entry which doesn't have a reserved slot
  68. //
  69. DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "ERROR : Mutex cannot be deleted since it does not exit");
  70. return FALSE;
  71. }
  72. if (CloseHandle(MutexTable[uiMutexIndex]) == FALSE)
  73. {
  74. //
  75. // Hum, the mutex deletion has failed
  76. //
  77. DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "ERROR : Mutex cannot be deleted since it does not exit");
  78. return FALSE;
  79. }
  80. MutexTable[uiMutexIndex] = NULL;
  81. return TRUE;
  82. }
  83. BOOLEAN EnterMutex(UINT32 uiMutexIndex, INT32 nLine, char *szFilename)
  84. {
  85. switch (WaitForSingleObject(MutexTable[uiMutexIndex], INFINITE))
  86. {
  87. case WAIT_OBJECT_0
  88. : return TRUE;
  89. case WAIT_TIMEOUT
  90. : DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "ERROR : Possible infinite loop detected due to enter mutex timeout");
  91. return FALSE;
  92. case WAIT_ABANDONED
  93. : DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "ERROR : Abandoned mutex has been found");
  94. return FALSE;
  95. }
  96. }
  97. BOOLEAN EnterMutexWithTimeout(UINT32 uiMutexIndex, UINT32 uiTimeout, INT32 nLine, char *szFilename)
  98. {
  99. switch (WaitForSingleObject(MutexTable[uiMutexIndex], uiTimeout))
  100. {
  101. case WAIT_OBJECT_0
  102. : return TRUE;
  103. case WAIT_TIMEOUT
  104. : return FALSE;
  105. case WAIT_ABANDONED
  106. : return FALSE;
  107. }
  108. return TRUE;
  109. }
  110. BOOLEAN LeaveMutex(UINT32 uiMutexIndex, INT32 nLine, char *szFilename)
  111. {
  112. if (ReleaseMutex(MutexTable[uiMutexIndex]) == FALSE)
  113. {
  114. DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "ERROR : Failed to leave mutex");
  115. return FALSE;
  116. }
  117. return TRUE;
  118. }
  119. #else
  120. //
  121. // Use defines to allocate slots in the mutex manager. Put these defines in LOCAL.H
  122. //
  123. CRITICAL_SECTION MutexTable[MAX_MUTEX_HANDLES];
  124. BOOLEAN InitializeMutexManager(void)
  125. {
  126. UINT32 uiIndex;
  127. //
  128. // Make sure all mutex handles are opened
  129. //
  130. for (uiIndex = 0; uiIndex < MAX_MUTEX_HANDLES; uiIndex++)
  131. {
  132. InitializeCriticalSection(&MutexTable[uiIndex]);
  133. }
  134. RegisterDebugTopic(TOPIC_MUTEX, "Mutex Manager");
  135. return TRUE;
  136. }
  137. void ShutdownMutexManager(void)
  138. {
  139. UINT32 uiIndex;
  140. DbgMessage(TOPIC_MUTEX, DBG_LEVEL_0, "Shutting down the Mutex Manager");
  141. //
  142. // Make sure all mutex handles are closed
  143. //
  144. for (uiIndex = 0; uiIndex < MAX_MUTEX_HANDLES; uiIndex++)
  145. {
  146. DeleteCriticalSection(&MutexTable[uiIndex]);
  147. }
  148. UnRegisterDebugTopic(TOPIC_MUTEX, "Mutex Manager");
  149. }
  150. BOOLEAN InitializeMutex(UINT32 uiMutexIndex, UINT8 *ubMutexName)
  151. {
  152. //InitializeCriticalSection(&MutexTable[uiMutexIndex]);
  153. return TRUE;
  154. }
  155. BOOLEAN DeleteMutex(UINT32 uiMutexIndex)
  156. {
  157. //DeleteCriticalSection(&MutexTable[uiMutexIndex]);
  158. return TRUE;
  159. }
  160. BOOLEAN EnterMutex(UINT32 uiMutexIndex, INT32 nLine, char *szFilename)
  161. {
  162. EnterCriticalSection(&MutexTable[uiMutexIndex]);
  163. return TRUE;
  164. }
  165. BOOLEAN EnterMutexWithTimeout(UINT32 uiMutexIndex, UINT32 uiTimeout, INT32 nLine, char *szFilename)
  166. {
  167. EnterCriticalSection(&MutexTable[uiMutexIndex]);
  168. return TRUE;
  169. }
  170. BOOLEAN LeaveMutex(UINT32 uiMutexIndex, INT32 nLine, char *szFilename)
  171. {
  172. LeaveCriticalSection(&MutexTable[uiMutexIndex]);
  173. return TRUE;
  174. }
  175. #endif