| 1 |
#ifdef FS_EVENTS |
| 2 |
#include "macos/FSEventsBackend.hh" |
| 3 |
#endif |
| 4 |
#ifdef WATCHMAN |
| 5 |
#include "watchman/WatchmanBackend.hh" |
| 6 |
#endif |
| 7 |
#ifdef WINDOWS |
| 8 |
#include "windows/WindowsBackend.hh" |
| 9 |
#endif |
| 10 |
#ifdef INOTIFY |
| 11 |
#include "linux/InotifyBackend.hh" |
| 12 |
#endif |
| 13 |
#ifdef KQUEUE |
| 14 |
#include "kqueue/KqueueBackend.hh" |
| 15 |
#endif |
| 16 |
#ifdef __wasm32__ |
| 17 |
#include "wasm/WasmBackend.hh" |
| 18 |
#endif |
| 19 |
#include "shared/BruteForceBackend.hh" |
| 20 |
|
| 21 |
#include "Backend.hh" |
| 22 |
#include <unordered_map> |
| 23 |
|
| 24 |
static std::unordered_map<std::string, std::shared_ptr<Backend>>& getSharedBackends() { |
| 25 |
static std::unordered_map<std::string, std::shared_ptr<Backend>>* sharedBackends = |
| 26 |
new std::unordered_map<std::string, std::shared_ptr<Backend>>(); |
| 27 |
return *sharedBackends; |
| 28 |
} |
| 29 |
|
| 30 |
std::shared_ptr<Backend> getBackend(std::string backend) { |
| 31 |
// Use FSEvents on macOS by default. |
| 32 |
// Use watchman by default if available on other platforms. |
| 33 |
// Fall back to brute force. |
| 34 |
#ifdef FS_EVENTS |
| 35 |
if (backend == "fs-events" || backend == "default") { |
| 36 |
return std::make_shared<FSEventsBackend>(); |
| 37 |
} |
| 38 |
#endif |
| 39 |
#ifdef WATCHMAN |
| 40 |
if ((backend == "watchman" || backend == "default") && WatchmanBackend::checkAvailable()) { |
| 41 |
return std::make_shared<WatchmanBackend>(); |
| 42 |
} |
| 43 |
#endif |
| 44 |
#ifdef WINDOWS |
| 45 |
if (backend == "windows" || backend == "default") { |
| 46 |
return std::make_shared<WindowsBackend>(); |
| 47 |
} |
| 48 |
#endif |
| 49 |
#ifdef INOTIFY |
| 50 |
if (backend == "inotify" || backend == "default") { |
| 51 |
return std::make_shared<InotifyBackend>(); |
| 52 |
} |
| 53 |
#endif |
| 54 |
#ifdef KQUEUE |
| 55 |
if (backend == "kqueue" || backend == "default") { |
| 56 |
return std::make_shared<KqueueBackend>(); |
| 57 |
} |
| 58 |
#endif |
| 59 |
#ifdef __wasm32__ |
| 60 |
if (backend == "wasm" || backend == "default") { |
| 61 |
return std::make_shared<WasmBackend>(); |
| 62 |
} |
| 63 |
#endif |
| 64 |
if (backend == "brute-force" || backend == "default") { |
| 65 |
return std::make_shared<BruteForceBackend>(); |
| 66 |
} |
| 67 |
|
| 68 |
return nullptr; |
| 69 |
} |
| 70 |
|
| 71 |
std::shared_ptr<Backend> Backend::getShared(std::string backend) { |
| 72 |
auto found = getSharedBackends().find(backend); |
| 73 |
if (found != getSharedBackends().end()) { |
| 74 |
return found->second; |
| 75 |
} |
| 76 |
|
| 77 |
auto result = getBackend(backend); |
| 78 |
if (!result) { |
| 79 |
return getShared("default"); |
| 80 |
} |
| 81 |
|
| 82 |
result->run(); |
| 83 |
getSharedBackends().emplace(backend, result); |
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
void removeShared(Backend *backend) { |
| 88 |
for (auto it = getSharedBackends().begin(); it != getSharedBackends().end(); it++) { |
| 89 |
if (it->second.get() == backend) { |
| 90 |
getSharedBackends().erase(it); |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Free up memory. |
| 96 |
if (getSharedBackends().size() == 0) { |
| 97 |
getSharedBackends().rehash(0); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void Backend::run() { |
| 102 |
#ifndef __wasm32__ |
| 103 |
mThread = std::thread([this] () { |
| 104 |
try { |
| 105 |
start(); |
| 106 |
} catch (std::exception &err) { |
| 107 |
handleError(err); |
| 108 |
} |
| 109 |
}); |
| 110 |
|
| 111 |
if (mThread.joinable()) { |
| 112 |
mStartedSignal.wait(); |
| 113 |
} |
| 114 |
#else |
| 115 |
try { |
| 116 |
start(); |
| 117 |
} catch (std::exception &err) { |
| 118 |
handleError(err); |
| 119 |
} |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
void Backend::notifyStarted() { |
| 124 |
mStartedSignal.notify(); |
| 125 |
} |
| 126 |
|
| 127 |
void Backend::start() { |
| 128 |
notifyStarted(); |
| 129 |
} |
| 130 |
|
| 131 |
Backend::~Backend() { |
| 132 |
#ifndef __wasm32__ |
| 133 |
// Wait for thread to stop |
| 134 |
if (mThread.joinable()) { |
| 135 |
// If the backend is being destroyed from the thread itself, detach, otherwise join. |
| 136 |
if (mThread.get_id() == std::this_thread::get_id()) { |
| 137 |
mThread.detach(); |
| 138 |
} else { |
| 139 |
mThread.join(); |
| 140 |
} |
| 141 |
} |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
void Backend::watch(WatcherRef watcher) { |
| 146 |
std::unique_lock<std::mutex> lock(mMutex); |
| 147 |
auto res = mSubscriptions.find(watcher); |
| 148 |
if (res == mSubscriptions.end()) { |
| 149 |
try { |
| 150 |
this->subscribe(watcher); |
| 151 |
mSubscriptions.insert(watcher); |
| 152 |
} catch (std::exception&) { |
| 153 |
unref(); |
| 154 |
throw; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void Backend::unwatch(WatcherRef watcher) { |
| 160 |
std::unique_lock<std::mutex> lock(mMutex); |
| 161 |
size_t deleted = mSubscriptions.erase(watcher); |
| 162 |
if (deleted > 0) { |
| 163 |
this->unsubscribe(watcher); |
| 164 |
unref(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void Backend::unref() { |
| 169 |
if (mSubscriptions.size() == 0) { |
| 170 |
removeShared(this); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void Backend::handleWatcherError(WatcherError &err) { |
| 175 |
unwatch(err.mWatcher); |
| 176 |
err.mWatcher->notifyError(err); |
| 177 |
} |
| 178 |
|
| 179 |
void Backend::handleError(std::exception &err) { |
| 180 |
std::unique_lock<std::mutex> lock(mMutex); |
| 181 |
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); it++) { |
| 182 |
(*it)->notifyError(err); |
| 183 |
} |
| 184 |
|
| 185 |
removeShared(this); |
| 186 |
} |