1 //===--- ThreadsafeFS.h ------------------------------------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_THREADSAFEFS_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_THREADSAFEFS_H 11 12 #include "Path.h" 13 #include "llvm/ADT/IntrusiveRefCntPtr.h" 14 #include "llvm/ADT/None.h" 15 #include "llvm/Support/VirtualFileSystem.h" 16 #include <memory> 17 18 namespace clang { 19 namespace clangd { 20 21 /// Wrapper for vfs::FileSystem for use in multithreaded programs like clangd. 22 /// As FileSystem is not threadsafe, concurrent threads must each obtain one. 23 /// Implementations may choose to depend on Context::current() e.g. to implement 24 /// snapshot semantics. clangd will not create vfs::FileSystems for use in 25 /// different contexts, so either ThreadsafeFS::view or the returned FS may 26 /// contain this logic. 27 class ThreadsafeFS { 28 public: 29 virtual ~ThreadsafeFS() = default; 30 31 /// Obtain a vfs::FileSystem with an arbitrary initial working directory. 32 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> view(llvm::NoneType CWD)33 view(llvm::NoneType CWD) const { 34 return viewImpl(); 35 } 36 37 /// Obtain a vfs::FileSystem with a specified working directory. 38 /// If the working directory can't be set (e.g. doesn't exist), logs and 39 /// returns the FS anyway. 40 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> view(PathRef CWD) const; 41 42 private: 43 /// Overridden by implementations to provide a vfs::FileSystem. 44 /// This is distinct from view(NoneType) to avoid GCC's -Woverloaded-virtual. 45 virtual llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const = 0; 46 }; 47 48 class RealThreadsafeFS : public ThreadsafeFS { 49 private: 50 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override; 51 }; 52 53 } // namespace clangd 54 } // namespace clang 55 56 #endif 57