Semaphore_WIN32.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // Semaphore_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Semaphore_WIN32.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Semaphore
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Semaphore_WIN32.h"
  16. namespace Poco {
  17. SemaphoreImpl::SemaphoreImpl(int n, int max)
  18. {
  19. poco_assert (n >= 0 && max > 0 && n <= max);
  20. _sema = CreateSemaphoreW(NULL, n, max, NULL);
  21. if (!_sema)
  22. {
  23. throw SystemException("cannot create semaphore");
  24. }
  25. }
  26. SemaphoreImpl::~SemaphoreImpl()
  27. {
  28. CloseHandle(_sema);
  29. }
  30. void SemaphoreImpl::waitImpl()
  31. {
  32. switch (WaitForSingleObject(_sema, INFINITE))
  33. {
  34. case WAIT_OBJECT_0:
  35. return;
  36. default:
  37. throw SystemException("wait for semaphore failed");
  38. }
  39. }
  40. bool SemaphoreImpl::waitImpl(long milliseconds)
  41. {
  42. switch (WaitForSingleObject(_sema, milliseconds + 1))
  43. {
  44. case WAIT_TIMEOUT:
  45. return false;
  46. case WAIT_OBJECT_0:
  47. return true;
  48. default:
  49. throw SystemException("wait for semaphore failed");
  50. }
  51. }
  52. } // namespace Poco