Mutex_WIN32.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // Mutex_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Mutex_WIN32.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Mutex
  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/Mutex_WIN32.h"
  16. #include "Poco/Timestamp.h"
  17. namespace Poco {
  18. MutexImpl::MutexImpl()
  19. {
  20. // the fct has a boolean return value under WInnNt/2000/XP but not on Win98
  21. // the return only checks if the input address of &_cs was valid, so it is safe to omit it
  22. InitializeCriticalSectionAndSpinCount(&_cs, 4000);
  23. }
  24. MutexImpl::~MutexImpl()
  25. {
  26. DeleteCriticalSection(&_cs);
  27. }
  28. bool MutexImpl::tryLockImpl(long milliseconds)
  29. {
  30. const int sleepMillis = 5;
  31. Timestamp now;
  32. Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
  33. do
  34. {
  35. try
  36. {
  37. if (TryEnterCriticalSection(&_cs) == TRUE)
  38. return true;
  39. }
  40. catch (...)
  41. {
  42. throw SystemException("cannot lock mutex");
  43. }
  44. Sleep(sleepMillis);
  45. }
  46. while (!now.isElapsed(diff));
  47. return false;
  48. }
  49. } // namespace Poco