Mutex_WIN32.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // Mutex_WIN32.h
  3. //
  4. // Library: Foundation
  5. // Package: Threading
  6. // Module: Mutex
  7. //
  8. // Definition of the MutexImpl and FastMutexImpl classes for WIN32.
  9. //
  10. // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_Mutex_WIN32_INCLUDED
  16. #define Foundation_Mutex_WIN32_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Exception.h"
  19. #include "Poco/UnWindows.h"
  20. namespace Poco {
  21. class Foundation_API MutexImpl
  22. {
  23. protected:
  24. MutexImpl();
  25. ~MutexImpl();
  26. void lockImpl();
  27. bool tryLockImpl();
  28. bool tryLockImpl(long milliseconds);
  29. void unlockImpl();
  30. private:
  31. CRITICAL_SECTION _cs;
  32. };
  33. typedef MutexImpl FastMutexImpl;
  34. //
  35. // inlines
  36. //
  37. inline void MutexImpl::lockImpl()
  38. {
  39. try
  40. {
  41. EnterCriticalSection(&_cs);
  42. }
  43. catch (...)
  44. {
  45. throw SystemException("cannot lock mutex");
  46. }
  47. }
  48. inline bool MutexImpl::tryLockImpl()
  49. {
  50. try
  51. {
  52. return TryEnterCriticalSection(&_cs) != 0;
  53. }
  54. catch (...)
  55. {
  56. }
  57. throw SystemException("cannot lock mutex");
  58. }
  59. inline void MutexImpl::unlockImpl()
  60. {
  61. LeaveCriticalSection(&_cs);
  62. }
  63. } // namespace Poco
  64. #endif // Foundation_Mutex_WIN32_INCLUDED