ECLockManager.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef ECLockManager_INCLUDED
  18. #define ECLockManager_INCLUDED
  19. #include <kopano/zcdefs.h>
  20. #include <kopano/kcodes.h>
  21. #include <map>
  22. #include <memory>
  23. #include <pthread.h>
  24. #include <kopano/lockhelper.hpp>
  25. namespace KC {
  26. class ECLockManager;
  27. class ECObjectLockImpl;
  28. typedef std::shared_ptr<ECLockManager> ECLockManagerPtr;
  29. ///////////////
  30. // ECObjectLock
  31. ///////////////
  32. class ECObjectLock _kc_final {
  33. public:
  34. ECObjectLock(void) = default;
  35. ECObjectLock(ECLockManagerPtr ptrLockManager, unsigned int ulObjId, ECSESSIONID sessionId);
  36. ECObjectLock(const ECObjectLock &other);
  37. ECObjectLock(ECObjectLock &&);
  38. ECObjectLock& operator=(const ECObjectLock &other);
  39. void swap(ECObjectLock &other);
  40. ECRESULT Unlock();
  41. private:
  42. std::shared_ptr<ECObjectLockImpl> m_ptrImpl;
  43. };
  44. ///////////////////////
  45. // ECObjectLock inlines
  46. ///////////////////////
  47. inline ECObjectLock::ECObjectLock(const ECObjectLock &other): m_ptrImpl(other.m_ptrImpl) {}
  48. inline ECObjectLock::ECObjectLock(ECObjectLock &&other) : m_ptrImpl(std::move(other.m_ptrImpl)) {}
  49. inline ECObjectLock& ECObjectLock::operator=(const ECObjectLock &other) {
  50. if (&other != this) {
  51. ECObjectLock tmp(other);
  52. swap(tmp);
  53. }
  54. return *this;
  55. }
  56. inline void ECObjectLock::swap(ECObjectLock &other) {
  57. m_ptrImpl.swap(other.m_ptrImpl);
  58. }
  59. ////////////////
  60. // ECLockManager
  61. ////////////////
  62. class ECLockManager _kc_final : public std::enable_shared_from_this<ECLockManager> {
  63. public:
  64. static ECLockManagerPtr Create();
  65. ECRESULT LockObject(unsigned int ulObjId, ECSESSIONID sessionId, ECObjectLock *lpOjbectLock);
  66. ECRESULT UnlockObject(unsigned int ulObjId, ECSESSIONID sessionId);
  67. bool IsLocked(unsigned int ulObjId, ECSESSIONID *lpSessionId);
  68. private:
  69. ECLockManager(void) = default;
  70. // Map object ids to session IDs.
  71. typedef std::map<unsigned int, ECSESSIONID> LockMap;
  72. KC::shared_mutex m_hRwLock;
  73. LockMap m_mapLocks;
  74. };
  75. } /* namespace */
  76. #endif // ndef ECLockManager_INCLUDED