Leaked source code of windows server 2003
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.

153 lines
4.0 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(
  42. cBuffer,
  43. _TEXT("%ld%ld_%d_%ld"),
  44. GetCurrentProcessId(),
  45. GetCurrentThreadId(),
  46. SemKey,
  47. (INT_PTR)this);
  48. SemKey++;
  49. SemHand=CreateEvent((LPSECURITY_ATTRIBUTES)NULL,
  50. TRUE, // Manual Reset
  51. FALSE, // Initial State
  52. cBuffer);
  53. if(!SemHand){
  54. SetObjectStatus(ErrSEM_CREATE_FAILED);
  55. }
  56. }
  57. //-------------------------------------------------------------------------
  58. //
  59. ApcSemaphore::ApcSemaphore(TCHAR * cBuffer) //named shared event
  60. {
  61. SemHand=CreateEvent((LPSECURITY_ATTRIBUTES)NULL,
  62. FALSE, // Manual Reset
  63. FALSE, // Initial State
  64. cBuffer);
  65. if(!SemHand){
  66. SetObjectStatus(ErrSEM_CREATE_FAILED);
  67. }
  68. }
  69. //-------------------------------------------------------------------------
  70. //
  71. ApcSemaphore::~ApcSemaphore()
  72. {
  73. CloseHandle(SemHand);
  74. }
  75. //-------------------------------------------------------------------------
  76. //
  77. INT ApcSemaphore::Post(VOID)
  78. {
  79. INT err = ErrNO_ERROR;
  80. if(!SetEvent(SemHand))
  81. err= ErrSEM_GENERAL_ERROR;
  82. return err;
  83. }
  84. //-------------------------------------------------------------------------
  85. //
  86. INT ApcSemaphore::Clear(VOID)
  87. {
  88. INT err = ErrNO_ERROR;
  89. if(!ResetEvent(SemHand))
  90. err = ErrSEM_GENERAL_ERROR;
  91. return err;
  92. }
  93. //-------------------------------------------------------------------------
  94. //
  95. INT ApcSemaphore::IsPosted(VOID)
  96. {
  97. // Could also call TimedWait(0)
  98. //
  99. INT ret = ErrSEM_BLOCK_NG;
  100. DWORD waitresult = WaitForSingleObject(SemHand,(DWORD)0);
  101. if(waitresult == WAIT_TIMEOUT)
  102. ret = ErrNO_ERROR;
  103. return ret;
  104. }
  105. //-------------------------------------------------------------------------
  106. // Wait for sem to be posted
  107. //
  108. // ulTimeout: 0 - Don't wait at all
  109. // <0 - Wait forever (same as Wait())
  110. // >0 - Wait for ulTimeout milliseconds
  111. //
  112. INT ApcSemaphore::TimedWait(LONG ulTimeout)
  113. {
  114. INT err;
  115. DWORD time_out = (ulTimeout < 0) ? INFINITE : ulTimeout;
  116. DWORD waitresult=WaitForSingleObject(SemHand,time_out);
  117. if(waitresult == 0){
  118. err = ErrNO_ERROR;
  119. }
  120. else if (waitresult == WAIT_TIMEOUT) {
  121. err = ErrSEM_TIMED_OUT;
  122. }
  123. else {
  124. err = ErrSEM_GENERAL_ERROR;
  125. }
  126. return err;
  127. }