cmFileLock.cxx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileLock.h"
  4. #include <cassert>
  5. #include <utility>
  6. #include "cmFileLockResult.h"
  7. // Common implementation
  8. cmFileLock::cmFileLock(cmFileLock&& other) noexcept
  9. {
  10. this->File = other.File;
  11. #if defined(_WIN32)
  12. other.File = INVALID_HANDLE_VALUE;
  13. #else
  14. other.File = -1;
  15. #endif
  16. this->Filename = std::move(other.Filename);
  17. }
  18. cmFileLock::~cmFileLock()
  19. {
  20. if (!this->Filename.empty()) {
  21. const cmFileLockResult result = this->Release();
  22. static_cast<void>(result);
  23. assert(result.IsOk());
  24. }
  25. }
  26. cmFileLock& cmFileLock::operator=(cmFileLock&& other) noexcept
  27. {
  28. this->File = other.File;
  29. #if defined(_WIN32)
  30. other.File = INVALID_HANDLE_VALUE;
  31. #else
  32. other.File = -1;
  33. #endif
  34. this->Filename = std::move(other.Filename);
  35. return *this;
  36. }
  37. cmFileLockResult cmFileLock::Lock(const std::string& filename,
  38. unsigned long timeout)
  39. {
  40. if (filename.empty()) {
  41. // Error is internal since all the directories and file must be created
  42. // before actual lock called.
  43. return cmFileLockResult::MakeInternal();
  44. }
  45. if (!this->Filename.empty()) {
  46. // Error is internal since double-lock must be checked in class
  47. // cmFileLockPool by the cmFileLock::IsLocked method.
  48. return cmFileLockResult::MakeInternal();
  49. }
  50. this->Filename = filename;
  51. cmFileLockResult result = this->OpenFile();
  52. if (result.IsOk()) {
  53. if (timeout == static_cast<unsigned long>(-1)) {
  54. result = this->LockWithoutTimeout();
  55. } else {
  56. result = this->LockWithTimeout(timeout);
  57. }
  58. }
  59. if (!result.IsOk()) {
  60. this->Filename.clear();
  61. }
  62. return result;
  63. }
  64. bool cmFileLock::IsLocked(const std::string& filename) const
  65. {
  66. return filename == this->Filename;
  67. }
  68. #if defined(_WIN32)
  69. # include "cmFileLockWin32.cxx"
  70. #else
  71. # include "cmFileLockUnix.cxx"
  72. #endif