cmFileLock.cxx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "cmFileLockResult.h"
  5. #include <assert.h>
  6. // Common implementation
  7. cmFileLock::~cmFileLock()
  8. {
  9. if (!this->Filename.empty()) {
  10. const cmFileLockResult result = this->Release();
  11. static_cast<void>(result);
  12. assert(result.IsOk());
  13. }
  14. }
  15. cmFileLockResult cmFileLock::Lock(const std::string& filename,
  16. unsigned long timeout)
  17. {
  18. if (filename.empty()) {
  19. // Error is internal since all the directories and file must be created
  20. // before actual lock called.
  21. return cmFileLockResult::MakeInternal();
  22. }
  23. if (!this->Filename.empty()) {
  24. // Error is internal since double-lock must be checked in class
  25. // cmFileLockPool by the cmFileLock::IsLocked method.
  26. return cmFileLockResult::MakeInternal();
  27. }
  28. this->Filename = filename;
  29. cmFileLockResult result = this->OpenFile();
  30. if (result.IsOk()) {
  31. if (timeout == static_cast<unsigned long>(-1)) {
  32. result = this->LockWithoutTimeout();
  33. } else {
  34. result = this->LockWithTimeout(timeout);
  35. }
  36. }
  37. if (!result.IsOk()) {
  38. this->Filename.clear();
  39. }
  40. return result;
  41. }
  42. bool cmFileLock::IsLocked(const std::string& filename) const
  43. {
  44. return filename == this->Filename;
  45. }
  46. #if defined(_WIN32)
  47. #include "cmFileLockWin32.cxx"
  48. #else
  49. #include "cmFileLockUnix.cxx"
  50. #endif