diff options
author | Chris Wilson <chris+github@qwirx.com> | 2015-05-16 09:17:07 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2015-05-16 09:17:07 +0000 |
commit | 32630dc6beebc5ef01497711e2d0ee3ec26fd174 (patch) | |
tree | 547706bde7f416a88933102aff15e94169ec0903 /lib/common | |
parent | 41b716a473e2efae0993a0dc323cd786c329ff7a (diff) |
Fix NamedLock release on Windows.
On Windows we can't delete the file while it's open, and we don't need to,
because we opened it for exclusive use, so another process can't lock it
between us unlocking and deleting it.
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/NamedLock.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/common/NamedLock.cpp b/lib/common/NamedLock.cpp index c862bb86..6b867e89 100644 --- a/lib/common/NamedLock.cpp +++ b/lib/common/NamedLock.cpp @@ -21,8 +21,9 @@ #include <sys/file.h> #endif -#include "NamedLock.h" #include "CommonException.h" +#include "NamedLock.h" +#include "Utils.h" #include "MemLeakFindOn.h" @@ -208,10 +209,17 @@ void NamedLock::ReleaseLock() THROW_EXCEPTION(CommonException, NamedLockNotHeld) } - // Delete the file. We need to do this before closing the filehandle, otherwise - // someone could acquire the lock, release and delete it between us closing (and - // hence releasing) and deleting it, and we'd fail when it came to deleting the - // file. This happens in tests much more often than you'd expect! +#ifndef WIN32 + // Delete the file. We need to do this before closing the filehandle, + // if we used flock() or fcntl() to lock it, otherwise someone could + // acquire the lock, release and delete it between us closing (and + // hence releasing) and deleting it, and we'd fail when it came to + // deleting the file. This happens in tests much more often than + // you'd expect! + // + // This doesn't apply on systems using plain lockfile locking, such as + // Windows, and there we need to close the file before deleting it, + // otherwise the system won't let us delete it. if(::unlink(mFileName.c_str()) != 0) { @@ -219,6 +227,7 @@ void NamedLock::ReleaseLock() BOX_FILE_MESSAGE(mFileName, "Failed to delete lockfile"), CommonException, OSFileError); } +#endif // !WIN32 // Close the file if(::close(mFileDescriptor) != 0) @@ -228,6 +237,18 @@ void NamedLock::ReleaseLock() CommonException, OSFileError); } +#ifdef WIN32 + // On Windows we need to close the file before deleting it, otherwise + // the system won't let us delete it. + + if(::unlink(mFileName.c_str()) != 0) + { + THROW_EMU_ERROR( + BOX_FILE_MESSAGE(mFileName, "Failed to delete lockfile"), + CommonException, OSFileError); + } +#endif // WIN32 + // Mark as unlocked, so we don't try to close it again if the unlink() fails. mFileDescriptor = -1; |