RWLock_VX.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // RWLock_VX.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/RWLock_VX.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: RWLock
  9. //
  10. // Definition of the RWLockImpl class for POSIX Threads (VxWorks).
  11. //
  12. // Copyright (c) 2004-2011, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_RWLock_VX_INCLUDED
  18. #define Foundation_RWLock_VX_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #include <pthread.h>
  22. #include <errno.h>
  23. namespace Poco {
  24. class Foundation_API RWLockImpl
  25. {
  26. protected:
  27. RWLockImpl();
  28. ~RWLockImpl();
  29. void readLockImpl();
  30. bool tryReadLockImpl();
  31. void writeLockImpl();
  32. bool tryWriteLockImpl();
  33. void unlockImpl();
  34. private:
  35. pthread_mutex_t _mutex;
  36. };
  37. //
  38. // inlines
  39. //
  40. inline void RWLockImpl::readLockImpl()
  41. {
  42. if (pthread_mutex_lock(&_mutex))
  43. throw SystemException("cannot lock mutex");
  44. }
  45. inline bool RWLockImpl::tryReadLockImpl()
  46. {
  47. int rc = pthread_mutex_trylock(&_mutex);
  48. if (rc == 0)
  49. return true;
  50. else if (rc == EBUSY)
  51. return false;
  52. else
  53. throw SystemException("cannot lock mutex");
  54. }
  55. inline void RWLockImpl::writeLockImpl()
  56. {
  57. readLockImpl();
  58. }
  59. inline bool RWLockImpl::tryWriteLockImpl()
  60. {
  61. return tryReadLockImpl();
  62. }
  63. inline void RWLockImpl::unlockImpl()
  64. {
  65. if (pthread_mutex_unlock(&_mutex))
  66. throw SystemException("cannot unlock mutex");
  67. }
  68. } // namespace Poco
  69. #endif // Foundation_RWLock_VX_INCLUDED