interprocess_sync_file.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __INTERPROCESS_SYNC_FILE_H__
  15. #define __INTERPROCESS_SYNC_FILE_H__
  16. #include <util/interprocess_sync.h>
  17. #include <util/threads/sync.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/shared_ptr.hpp>
  20. #include <string>
  21. namespace isc {
  22. namespace util {
  23. /// \brief InterprocessSyncFileError
  24. ///
  25. /// Exception that is thrown if it's not possible to open the
  26. /// lock file.
  27. class InterprocessSyncFileError : public Exception {
  28. public:
  29. InterprocessSyncFileError(const char* file, size_t line,
  30. const char* what) :
  31. isc::Exception(file, line, what) {}
  32. };
  33. /// \brief File-based Interprocess Sync Class
  34. ///
  35. /// This class specifies a concrete implementation for a file-based
  36. /// interprocess synchronization mechanism. Please see the
  37. /// InterprocessSync class documentation for usage.
  38. ///
  39. /// An InterprocessSyncFileError exception may be thrown if there is an
  40. /// issue opening the lock file.
  41. ///
  42. /// Lock files are created typically in the local state directory
  43. /// (var). They are typically named like "<task_name>_lockfile".
  44. /// This implementation opens lock files lazily (only when
  45. /// necessary). It also leaves the lock files lying around as multiple
  46. /// processes may have locks on them.
  47. class InterprocessSyncFile : public InterprocessSync {
  48. public:
  49. /// \brief Constructor
  50. ///
  51. /// Creates a file-based interprocess synchronization object
  52. ///
  53. /// \param name Name of the synchronization task. This has to be
  54. /// identical among the various processes that need to be
  55. /// synchronized for the same task.
  56. InterprocessSyncFile(const std::string& task_name);
  57. /// \brief Destructor
  58. virtual ~InterprocessSyncFile();
  59. protected:
  60. /// \brief Acquire the lock (blocks if something else has acquired a
  61. /// lock on the same task name)
  62. ///
  63. /// \return Returns true if the lock was acquired, false otherwise.
  64. bool lock();
  65. /// \brief Try to acquire a lock (doesn't block)
  66. ///
  67. /// \return Returns true if the lock was acquired, false otherwise.
  68. bool tryLock();
  69. /// \brief Release the lock
  70. ///
  71. /// \return Returns true if the lock was released, false otherwise.
  72. bool unlock();
  73. private:
  74. typedef boost::shared_ptr<isc::util::thread::Mutex> MutexPtr;
  75. typedef boost::shared_ptr<isc::util::thread::Mutex::Locker> LockerPtr;
  76. bool do_lock(int cmd, short l_type);
  77. int fd_; ///< The descriptor for the open file
  78. MutexPtr mutex_; ///< A mutex for mutual exclusion among threads
  79. LockerPtr locker_; ///< A locker on mutex_
  80. };
  81. } // namespace util
  82. } // namespace isc
  83. #endif // __INTERPROCESS_SYNC_FILE_H__