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.

87 lines
1.7 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * counter.h
  7. *
  8. * Abstract:
  9. * simple counter class - up/down counter, wait till zero
  10. *
  11. * Revision History:
  12. * Brijesh Krishnaswami (brijeshk) 05/02/2000
  13. * created
  14. *
  15. *****************************************************************************/
  16. #ifndef _COUNTER_H_
  17. #define _COUNTER_H_
  18. #include <windows.h>
  19. #define INLINE_EXPORT_SPEC __declspec( dllexport)
  20. class INLINE_EXPORT_SPEC CCounter
  21. {
  22. private:
  23. HANDLE _hEvent;
  24. LONG _lCount;
  25. public:
  26. CCounter( )
  27. {
  28. _lCount = 0;
  29. _hEvent = NULL;
  30. }
  31. DWORD Init ()
  32. {
  33. _hEvent = CreateEvent ( NULL, TRUE, TRUE, L"SRCounter" );
  34. return _hEvent == NULL ? GetLastError() : ERROR_SUCCESS;
  35. }
  36. ~CCounter( )
  37. {
  38. if ( _hEvent != NULL )
  39. CloseHandle( _hEvent );
  40. }
  41. void Up( )
  42. {
  43. if (InterlockedIncrement (&_lCount) == 1)
  44. {
  45. if (_hEvent != NULL)
  46. ResetEvent ( _hEvent );
  47. }
  48. }
  49. DWORD Down( )
  50. {
  51. if ( InterlockedDecrement(&_lCount) == 0 )
  52. {
  53. if (_hEvent != NULL && FALSE == SetEvent ( _hEvent ))
  54. {
  55. return GetLastError();
  56. }
  57. }
  58. return ERROR_SUCCESS;
  59. }
  60. DWORD WaitForZero( )
  61. {
  62. if (_hEvent != NULL)
  63. {
  64. return WaitForSingleObject( _hEvent, 10 * 60000 ); /* 10 minutes */
  65. }
  66. else
  67. return ERROR_INTERNAL_ERROR;
  68. }
  69. LONG GetCount( )
  70. {
  71. return _lCount;
  72. }
  73. };
  74. #endif