NamedMutex_WIN32.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // NamedMutex_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/NamedMutex_WIN32.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: NamedMutex
  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/NamedMutex_WIN32.h"
  16. #include "Poco/Exception.h"
  17. namespace Poco {
  18. NamedMutexImpl::NamedMutexImpl(const std::string& name):
  19. _name(name)
  20. {
  21. _mutex = CreateMutexA(NULL, FALSE, _name.c_str());
  22. if (!_mutex)
  23. throw SystemException("cannot create named mutex", _name);
  24. }
  25. NamedMutexImpl::~NamedMutexImpl()
  26. {
  27. CloseHandle(_mutex);
  28. }
  29. void NamedMutexImpl::lockImpl()
  30. {
  31. switch (WaitForSingleObject(_mutex, INFINITE))
  32. {
  33. case WAIT_OBJECT_0:
  34. return;
  35. case WAIT_ABANDONED:
  36. throw SystemException("cannot lock named mutex (abadoned)", _name);
  37. default:
  38. throw SystemException("cannot lock named mutex", _name);
  39. }
  40. }
  41. bool NamedMutexImpl::tryLockImpl()
  42. {
  43. switch (WaitForSingleObject(_mutex, 0))
  44. {
  45. case WAIT_OBJECT_0:
  46. return true;
  47. case WAIT_TIMEOUT:
  48. return false;
  49. case WAIT_ABANDONED:
  50. throw SystemException("cannot lock named mutex (abadoned)", _name);
  51. default:
  52. throw SystemException("cannot lock named mutex", _name);
  53. }
  54. }
  55. void NamedMutexImpl::unlockImpl()
  56. {
  57. ReleaseMutex(_mutex);
  58. }
  59. } // namespace Poco