interprocess_sync_file.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "exceptions/exceptions.h"
  18. namespace isc {
  19. namespace util {
  20. /// \brief InterprocessSyncFileError
  21. ///
  22. /// Exception that is thrown if it's not possible to open the
  23. /// lock file.
  24. class InterprocessSyncFileError : public Exception {
  25. public:
  26. InterprocessSyncFileError(const char* file, size_t line,
  27. const char* what) :
  28. isc::Exception(file, line, what) {}
  29. };
  30. /// \brief File-based Interprocess Sync Class
  31. ///
  32. /// This class specifies a concrete implementation for a file-based
  33. /// interprocess synchronization mechanism. Please see the
  34. /// InterprocessSync class documentation for usage.
  35. ///
  36. /// Lock files are created typically in the local state directory
  37. /// (var). They are typically named like "<task_name>_lockfile".
  38. /// This implementation opens lock files lazily (only when
  39. /// necessary). It also leaves the lock files lying around as multiple
  40. /// processes may have locks on them.
  41. class InterprocessSyncFile : public InterprocessSync {
  42. public:
  43. /// \brief Constructor
  44. ///
  45. /// Creates a file-based interprocess synchronization object
  46. ///
  47. /// \param name Name of the synchronization task. This has to be
  48. /// identical among the various processes that need to be
  49. /// synchronized for the same task.
  50. InterprocessSyncFile(const std::string& task_name) :
  51. InterprocessSync(task_name), fd_(-1)
  52. {}
  53. /// \brief Destructor
  54. virtual ~InterprocessSyncFile();
  55. protected:
  56. /// \brief Acquire the lock (blocks if something else has acquired a
  57. /// lock on the same task name)
  58. ///
  59. /// \return Returns true if the lock was acquired, false otherwise.
  60. bool lock();
  61. /// \brief Try to acquire a lock (doesn't block)
  62. ///
  63. /// \return Returns true if the lock was acquired, false otherwise.
  64. bool tryLock();
  65. /// \brief Release the lock
  66. ///
  67. /// \return Returns true if the lock was released, false otherwise.
  68. bool unlock();
  69. private:
  70. bool do_lock(int cmd, short l_type);
  71. int fd_; ///< The descriptor for the open file
  72. };
  73. } // namespace util
  74. } // namespace isc
  75. #endif // __INTERPROCESS_SYNC_FILE_H__