1
0
forked from 0ad/0ad

make VFS thread-safe, per long-standing request by Philip

This was SVN commit r9369.
This commit is contained in:
janwas 2011-04-30 15:57:43 +00:00
parent 635c2a12e2
commit 1636f062a3
4 changed files with 22 additions and 31 deletions

View File

@ -24,6 +24,7 @@
#include "lib/file/vfs/vfs.h" #include "lib/file/vfs/vfs.h"
#include "lib/allocators/shared_ptr.h" #include "lib/allocators/shared_ptr.h"
#include "lib/posix/posix_pthread.h"
#include "lib/file/file_system_util.h" #include "lib/file/file_system_util.h"
#include "lib/file/common/file_stats.h" #include "lib/file/common/file_stats.h"
#include "lib/file/common/trace.h" #include "lib/file/common/trace.h"
@ -38,6 +39,13 @@ ERROR_ASSOCIATE(ERR::VFS_DIR_NOT_FOUND, L"VFS directory not found", -1);
ERROR_ASSOCIATE(ERR::VFS_FILE_NOT_FOUND, L"VFS file not found", -1); ERROR_ASSOCIATE(ERR::VFS_FILE_NOT_FOUND, L"VFS file not found", -1);
ERROR_ASSOCIATE(ERR::VFS_ALREADY_MOUNTED, L"VFS path already mounted", -1); ERROR_ASSOCIATE(ERR::VFS_ALREADY_MOUNTED, L"VFS path already mounted", -1);
static pthread_mutex_t vfs_mutex = PTHREAD_MUTEX_INITIALIZER;
struct ScopedLock
{
ScopedLock() { pthread_mutex_lock(&vfs_mutex); }
~ScopedLock() { pthread_mutex_unlock(&vfs_mutex); }
};
class VFS : public IVFS class VFS : public IVFS
{ {
public: public:
@ -49,6 +57,7 @@ public:
virtual LibError Mount(const VfsPath& mountPoint, const OsPath& path, size_t flags /* = 0 */, size_t priority /* = 0 */) virtual LibError Mount(const VfsPath& mountPoint, const OsPath& path, size_t flags /* = 0 */, size_t priority /* = 0 */)
{ {
ScopedLock s;
if(!fs_util::DirectoryExists(path)) if(!fs_util::DirectoryExists(path))
{ {
if(flags & VFS_MOUNT_MUST_EXIST) if(flags & VFS_MOUNT_MUST_EXIST)
@ -67,6 +76,7 @@ public:
virtual LibError GetFileInfo(const VfsPath& pathname, FileInfo* pfileInfo) const virtual LibError GetFileInfo(const VfsPath& pathname, FileInfo* pfileInfo) const
{ {
ScopedLock s;
VfsDirectory* directory; VfsFile* file; VfsDirectory* directory; VfsFile* file;
LibError ret = vfs_Lookup(pathname, &m_rootDirectory, directory, &file); LibError ret = vfs_Lookup(pathname, &m_rootDirectory, directory, &file);
if(!pfileInfo) // just indicate if the file exists without raising warnings. if(!pfileInfo) // just indicate if the file exists without raising warnings.
@ -78,6 +88,7 @@ public:
virtual LibError GetFilePriority(const VfsPath& pathname, size_t* ppriority) const virtual LibError GetFilePriority(const VfsPath& pathname, size_t* ppriority) const
{ {
ScopedLock s;
VfsDirectory* directory; VfsFile* file; VfsDirectory* directory; VfsFile* file;
RETURN_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file)); RETURN_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
*ppriority = file->Priority(); *ppriority = file->Priority();
@ -86,6 +97,7 @@ public:
virtual LibError GetDirectoryEntries(const VfsPath& path, FileInfos* fileInfos, DirectoryNames* subdirectoryNames) const virtual LibError GetDirectoryEntries(const VfsPath& path, FileInfos* fileInfos, DirectoryNames* subdirectoryNames) const
{ {
ScopedLock s;
VfsDirectory* directory; VfsDirectory* directory;
CHECK_ERR(vfs_Lookup(path, &m_rootDirectory, directory, 0)); CHECK_ERR(vfs_Lookup(path, &m_rootDirectory, directory, 0));
@ -115,6 +127,7 @@ public:
virtual LibError CreateFile(const VfsPath& pathname, const shared_ptr<u8>& fileContents, size_t size) virtual LibError CreateFile(const VfsPath& pathname, const shared_ptr<u8>& fileContents, size_t size)
{ {
ScopedLock s;
VfsDirectory* directory; VfsDirectory* directory;
CHECK_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_CREATE)); CHECK_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_CREATE));
@ -135,6 +148,7 @@ public:
virtual LibError LoadFile(const VfsPath& pathname, shared_ptr<u8>& fileContents, size_t& size) virtual LibError LoadFile(const VfsPath& pathname, shared_ptr<u8>& fileContents, size_t& size)
{ {
ScopedLock s;
const bool isCacheHit = m_fileCache.Retrieve(pathname, fileContents, size); const bool isCacheHit = m_fileCache.Retrieve(pathname, fileContents, size);
if(!isCacheHit) if(!isCacheHit)
{ {
@ -170,6 +184,7 @@ public:
virtual std::wstring TextRepresentation() const virtual std::wstring TextRepresentation() const
{ {
ScopedLock s;
std::wstring textRepresentation; std::wstring textRepresentation;
textRepresentation.reserve(100*KiB); textRepresentation.reserve(100*KiB);
DirectoryDescriptionR(textRepresentation, m_rootDirectory, 0); DirectoryDescriptionR(textRepresentation, m_rootDirectory, 0);
@ -178,6 +193,7 @@ public:
virtual LibError GetRealPath(const VfsPath& pathname, OsPath& realPathname) virtual LibError GetRealPath(const VfsPath& pathname, OsPath& realPathname)
{ {
ScopedLock s;
VfsDirectory* directory; VfsFile* file; VfsDirectory* directory; VfsFile* file;
CHECK_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file)); CHECK_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
realPathname = file->Loader()->Path() / pathname.Filename(); realPathname = file->Loader()->Path() / pathname.Filename();
@ -186,6 +202,7 @@ public:
virtual LibError GetVirtualPath(const OsPath& realPathname, VfsPath& pathname) virtual LibError GetVirtualPath(const OsPath& realPathname, VfsPath& pathname)
{ {
ScopedLock s;
const OsPath realPath = realPathname.Parent()/""; const OsPath realPath = realPathname.Parent()/"";
VfsPath path; VfsPath path;
RETURN_ERR(FindRealPathR(realPath, m_rootDirectory, L"", path)); RETURN_ERR(FindRealPathR(realPath, m_rootDirectory, L"", path));
@ -195,6 +212,7 @@ public:
virtual LibError Invalidate(const VfsPath& pathname) virtual LibError Invalidate(const VfsPath& pathname)
{ {
ScopedLock s;
m_fileCache.Remove(pathname); m_fileCache.Remove(pathname);
VfsDirectory* directory; VfsDirectory* directory;
@ -207,6 +225,7 @@ public:
virtual void Clear() virtual void Clear()
{ {
ScopedLock s;
m_rootDirectory.Clear(); m_rootDirectory.Clear();
} }

View File

@ -55,11 +55,13 @@ enum VfsMountFlags
/** /**
* return ERR::VFS_DIR_NOT_FOUND if the given real path doesn't exist. * return ERR::VFS_DIR_NOT_FOUND if the given real path doesn't exist.
* (the default behaviour is to create all real directories in the path) * (the default behavior is to create all real directories in the path)
**/ **/
VFS_MOUNT_MUST_EXIST = 4 VFS_MOUNT_MUST_EXIST = 4
}; };
// (member functions are thread-safe after the instance has been
// constructed - each acquires a pthread mutex.)
struct IVFS struct IVFS
{ {
virtual ~IVFS() {} virtual ~IVFS() {}

View File

@ -75,7 +75,6 @@ need only be renamed (e.g. _open, _stat).
//#include "lib/posix/posix_mman.h" //#include "lib/posix/posix_mman.h"
//#include "lib/posix/posix_pthread.h" //#include "lib/posix/posix_pthread.h"
//#include "lib/posix/posix_sock.h" //#include "lib/posix/posix_sock.h"
//#include "lib/posix/posix_terminal.h"
//#include "lib/posix/posix_time.h" //#include "lib/posix/posix_time.h"
//#include "lib/posix/posix_utsname.h" //#include "lib/posix/posix_utsname.h"

View File

@ -1,29 +0,0 @@
/* Copyright (c) 2010 Wildfire Games
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if OS_WIN
# include "lib/sysdep/os/win/wposix/wterminal.h"
#else
# include <sys/ioctl.h>
#endif
#include "lib/posix/posix_errno.h" // for user convenience