Backend.cc 4.1 KB

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