|
| 1 | +//===--- PluginRegistry.h ---------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "llvm/ADT/ArrayRef.h" |
| 14 | +#include "llvm/ADT/StringMap.h" |
| 15 | +#include "llvm/ADT/StringRef.h" |
| 16 | +#include "llvm/Support/Chrono.h" |
| 17 | +#include "llvm/Support/Program.h" |
| 18 | + |
| 19 | +#include <mutex> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +namespace swift { |
| 23 | + |
| 24 | +class LoadedExecutablePlugin { |
| 25 | + const llvm::sys::procid_t pid; |
| 26 | + const llvm::sys::TimePoint<> LastModificationTime; |
| 27 | + const int inputFileDescriptor; |
| 28 | + const int outputFileDescriptor; |
| 29 | + |
| 30 | + /// Opaque value of the protocol capability of the pluugin. This is a |
| 31 | + /// value from ASTGen. |
| 32 | + const void *capability = nullptr; |
| 33 | + |
| 34 | + /// Cleanup function to call ASTGen. |
| 35 | + std::function<void(void)> cleanup; |
| 36 | + |
| 37 | + std::mutex mtx; |
| 38 | + |
| 39 | + ssize_t write(const void *buf, size_t nbyte) const; |
| 40 | + ssize_t read(void *buf, size_t nbyte) const; |
| 41 | + |
| 42 | +public: |
| 43 | + LoadedExecutablePlugin(llvm::sys::procid_t pid, |
| 44 | + llvm::sys::TimePoint<> LastModificationTime, |
| 45 | + int inputFileDescriptor, int outputFileDescriptor); |
| 46 | + ~LoadedExecutablePlugin(); |
| 47 | + llvm::sys::TimePoint<> getLastModificationTime() const { |
| 48 | + return LastModificationTime; |
| 49 | + } |
| 50 | + |
| 51 | + void lock() { mtx.lock(); } |
| 52 | + void unlock() { mtx.unlock(); } |
| 53 | + |
| 54 | + /// Send a message to the plugin. |
| 55 | + bool sendMessage(llvm::StringRef message) const; |
| 56 | + |
| 57 | + /// Wait for a message from plugin and returns it. |
| 58 | + std::string waitForNextMessage() const; |
| 59 | + |
| 60 | + bool isInitialized() const { return bool(cleanup); } |
| 61 | + void setCleanup(std::function<void(void)> cleanup) { |
| 62 | + this->cleanup = cleanup; |
| 63 | + } |
| 64 | + |
| 65 | + llvm::sys::procid_t getPid() { return pid; } |
| 66 | + |
| 67 | + const void *getCapability() { return capability; }; |
| 68 | + void setCapability(const void *newValue) { capability = newValue; }; |
| 69 | +}; |
| 70 | + |
| 71 | +class PluginRegistry { |
| 72 | + /// Record of loaded plugin library modules. |
| 73 | + llvm::StringMap<void *> LoadedPluginLibraries; |
| 74 | + |
| 75 | + /// Record of loaded plugin executables. |
| 76 | + llvm::StringMap<std::unique_ptr<LoadedExecutablePlugin>> |
| 77 | + LoadedPluginExecutables; |
| 78 | + |
| 79 | +public: |
| 80 | + bool loadLibraryPlugin(llvm::StringRef path, const char *&errorMsg); |
| 81 | + LoadedExecutablePlugin *loadExecutablePlugin(llvm::StringRef path, |
| 82 | + const char *&errorMsg); |
| 83 | + |
| 84 | + const llvm::StringMap<void *> &getLoadedLibraryPlugins() const { |
| 85 | + return LoadedPluginLibraries; |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +} // namespace swift |
0 commit comments