RWLock_Android.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // RWLock_Android.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/RWLock_Android.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: RWLock
  9. //
  10. // Definition of the RWLockImpl class for Android Threads.
  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_Android_INCLUDED
  18. #define Foundation_RWLock_Android_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 reader/writer lock");
  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 reader/writer lock");
  54. }
  55. inline void RWLockImpl::writeLockImpl()
  56. {
  57. if (pthread_mutex_lock(&_mutex))
  58. throw SystemException("cannot lock reader/writer lock");
  59. }
  60. inline bool RWLockImpl::tryWriteLockImpl()
  61. {
  62. int rc = pthread_mutex_trylock(&_mutex);
  63. if (rc == 0)
  64. return true;
  65. else if (rc == EBUSY)
  66. return false;
  67. else
  68. throw SystemException("cannot lock reader/writer lock");
  69. }
  70. inline void RWLockImpl::unlockImpl()
  71. {
  72. if (pthread_mutex_unlock(&_mutex))
  73. throw SystemException("cannot unlock reader/writer lock");
  74. }
  75. } // namespace Poco
  76. #endif // Foundation_RWLock_Android_INCLUDED