Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.8 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * jwa 09FEB92 creation
  7. * pcy21Apr93: OS2 FE merge
  8. * pcy08Apr94: Trim size, use static iterators, dead code removal
  9. * srt21Jun96: Added named shared event type semaphores
  10. * mwh27Jan97: add this pointer to semaphore name in default ctor, needed
  11. * to help with semaphores being used in a DLL and a client
  12. * to a DLL. The DLL and the client each get their own copy
  13. * of SemKey, but the memory addresses will be unique
  14. */
  15. #include "cdefine.h"
  16. #if (C_OS & C_NT)
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #endif
  20. #include "err.h"
  21. #include "apcsemnt.h"
  22. INT SemKey = 0;
  23. //-------------------------------------------------------------------------
  24. // The semaphore is created as unnamed and shared
  25. //
  26. ApcSemaphore::ApcSemaphore(VOID) :
  27. Semaphore()
  28. {
  29. SetObjectStatus(ErrNO_ERROR);
  30. // unnamed semaphore, shared and
  31. TCHAR cBuffer[128];
  32. // add use of this pointer in the name - the memory address will be
  33. // unique throughout this process, including any DLL's that are mapped
  34. // in. This ocurred because if a DLL uses ApcSemaphore, and a client
  35. // of the DLL also uses ApcSemaphore they each have their own copy of
  36. // SemKey, meaning it is possible, and likely that when the DLL makes
  37. // use of this CTOR, and the client makes use of this CTOR that they
  38. // will map to the same name, therefore you will get two ApcSemaphore
  39. // objects that both reference the same event. By adding the this
  40. // pointer each event should be unique
  41. _stprintf(cBuffer,_TEXT("%ld%ld_%d_%ld"),GetCurrentProcessId(),GetCurrentThreadId(),
  42. SemKey,this);
  43. SemKey++;
  44. SemHand=CreateEvent((LPSECURITY_ATTRIBUTES)NULL,
  45. TRUE, // Manual Reset
  46. FALSE, // Initial State
  47. cBuffer);
  48. if(!SemHand){
  49. SetObjectStatus(ErrSEM_CREATE_FAILED);
  50. }
  51. }
  52. //-------------------------------------------------------------------------
  53. //
  54. ApcSemaphore::ApcSemaphore(TCHAR * cBuffer) //named shared event
  55. {
  56. SemHand=CreateEvent((LPSECURITY_ATTRIBUTES)NULL,
  57. FALSE, // Manual Reset
  58. FALSE, // Initial State
  59. cBuffer);
  60. if(!SemHand){
  61. SetObjectStatus(ErrSEM_CREATE_FAILED);
  62. }
  63. }
  64. //-------------------------------------------------------------------------
  65. //
  66. ApcSemaphore::~ApcSemaphore()
  67. {
  68. CloseHandle(SemHand);
  69. }
  70. //-------------------------------------------------------------------------
  71. //
  72. INT ApcSemaphore::Post(VOID)
  73. {
  74. INT err = ErrNO_ERROR;
  75. if(!SetEvent(SemHand))
  76. err= ErrSEM_GENERAL_ERROR;
  77. return err;
  78. }
  79. //-------------------------------------------------------------------------
  80. //
  81. INT ApcSemaphore::Clear(VOID)
  82. {
  83. INT err = ErrNO_ERROR;
  84. if(!ResetEvent(SemHand))
  85. err = ErrSEM_GENERAL_ERROR;
  86. return err;
  87. }
  88. //-------------------------------------------------------------------------
  89. //
  90. INT ApcSemaphore::IsPosted(VOID)
  91. {
  92. // Could also call TimedWait(0)
  93. //
  94. INT ret = ErrSEM_BLOCK_NG;
  95. DWORD waitresult = WaitForSingleObject(SemHand,(DWORD)0);
  96. if(waitresult == WAIT_TIMEOUT)
  97. ret = ErrNO_ERROR;
  98. return ret;
  99. }
  100. //-------------------------------------------------------------------------
  101. // Wait for sem to be posted
  102. //
  103. // ulTimeout: 0 - Don't wait at all
  104. // <0 - Wait forever (same as Wait())
  105. // >0 - Wait for ulTimeout milliseconds
  106. //
  107. INT ApcSemaphore::TimedWait(LONG ulTimeout)
  108. {
  109. INT err;
  110. DWORD time_out = (ulTimeout < 0) ? INFINITE : ulTimeout;
  111. DWORD waitresult=WaitForSingleObject(SemHand,time_out);
  112. if(waitresult == 0){
  113. err = ErrNO_ERROR;
  114. }
  115. else if (waitresult == WAIT_TIMEOUT) {
  116. err = ErrSEM_TIMED_OUT;
  117. }
  118. else {
  119. err = ErrSEM_GENERAL_ERROR;
  120. }
  121. return err;
  122. }