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.

93 lines
2.6 KiB

  1. //#pragma title( "TSync.cpp - Common synchronization classes" )
  2. /*
  3. Copyright (c) 1995-1998, Mission Critical Software, Inc. All rights reserved.
  4. ===============================================================================
  5. Module - TSync.cpp
  6. System - Common
  7. Author - Rich Denham
  8. Created - 1996-11-08
  9. Description - Common synchronization classes
  10. TCriticalSection
  11. TSemaphoreNamed
  12. Updates -
  13. ===============================================================================
  14. */
  15. #include <stdio.h>
  16. #ifdef USE_STDAFX
  17. # include "stdafx.h"
  18. #else
  19. # include <windows.h>
  20. #endif
  21. #include <time.h>
  22. #include "Common.hpp"
  23. #include "Err.hpp"
  24. #include "TSync.hpp"
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // TSemaphoreNamed member functions
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // Create named semaphore
  29. DWORD // ret-OS return code
  30. TSemaphoreNamed::Create(
  31. TCHAR const * sNameT ,// in -semaphore name
  32. DWORD nInitial ,// in -initial count
  33. DWORD nMaximum ,// in -maximum count
  34. BOOL * pbExisted // out-TRUE=previously existed
  35. )
  36. {
  37. DWORD rcOs=0; // OS return code
  38. handle = CreateSemaphore( NULL, nInitial, nMaximum, sNameT );
  39. if ( handle == NULL )
  40. {
  41. rcOs = GetLastError();
  42. }
  43. else if ( pbExisted )
  44. {
  45. rcOs = GetLastError();
  46. switch ( rcOs )
  47. {
  48. case 0:
  49. *pbExisted = FALSE;
  50. break;
  51. case ERROR_ALREADY_EXISTS:
  52. *pbExisted = TRUE;
  53. rcOs = 0;
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. return rcOs;
  60. }
  61. // Open named semaphore
  62. DWORD // ret-OS return code
  63. TSemaphoreNamed::Open(
  64. TCHAR const * sNameT // in -semaphore name
  65. )
  66. {
  67. DWORD rcOs=0; // OS return code
  68. handle = OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE, sNameT );
  69. if ( handle == NULL ) rcOs = GetLastError();
  70. return rcOs;
  71. }
  72. // Release semaphore
  73. DWORD // ret-OS return code
  74. TSemaphoreNamed::Release(
  75. long nRelease // in -number to release
  76. )
  77. {
  78. DWORD rcOs; // OS return code
  79. long nPrevious=0; // previous count
  80. rcOs = ReleaseSemaphore( Handle(), nRelease, &nPrevious )
  81. ? 0 : GetLastError();
  82. return rcOs;
  83. }
  84. // TSync.cpp - end of file