2018-01-31 16:51:16 +08:00
|
|
|
#include "Threading.h"
|
2018-06-12 19:56:21 +08:00
|
|
|
#include "Trace.h"
|
2018-02-08 15:37:35 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
2018-11-27 20:09:13 +08:00
|
|
|
#include <atomic>
|
2018-02-08 15:37:35 +08:00
|
|
|
#include <thread>
|
2018-10-31 23:37:09 +08:00
|
|
|
#ifdef __USE_POSIX
|
2018-10-30 20:13:27 +08:00
|
|
|
#include <pthread.h>
|
2019-02-25 17:19:26 +08:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <sys/resource.h>
|
2019-02-27 18:16:03 +08:00
|
|
|
#elif defined (_WIN32)
|
2019-04-17 11:02:18 +08:00
|
|
|
#include <windows.h>
|
2018-10-30 20:13:27 +08:00
|
|
|
#endif
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
void Notification::notify() {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
Notified = true;
|
|
|
|
}
|
|
|
|
CV.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::wait() const {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mu);
|
|
|
|
CV.wait(Lock, [this] { return Notified; });
|
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
Semaphore::Semaphore(std::size_t MaxLocks) : FreeSlots(MaxLocks) {}
|
|
|
|
|
2018-12-13 21:09:50 +08:00
|
|
|
bool Semaphore::try_lock() {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
if (FreeSlots > 0) {
|
|
|
|
--FreeSlots;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void Semaphore::lock() {
|
2018-06-12 19:56:21 +08:00
|
|
|
trace::Span Span("WaitForFreeSemaphoreSlot");
|
|
|
|
// trace::Span can also acquire locks in ctor and dtor, we make sure it
|
|
|
|
// happens when Semaphore's own lock is not held.
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
SlotsChanged.wait(Lock, [&]() { return FreeSlots > 0; });
|
|
|
|
--FreeSlots;
|
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Semaphore::unlock() {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
++FreeSlots;
|
|
|
|
Lock.unlock();
|
|
|
|
|
|
|
|
SlotsChanged.notify_one();
|
2018-02-06 23:53:42 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
AsyncTaskRunner::~AsyncTaskRunner() { wait(); }
|
2018-02-08 15:37:35 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
bool AsyncTaskRunner::wait(Deadline D) const {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
2018-02-13 16:59:23 +08:00
|
|
|
return clangd::wait(Lock, TasksReachedZero, D,
|
|
|
|
[&] { return InFlightTasks == 0; });
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-02-06 23:53:42 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void AsyncTaskRunner::runAsync(const llvm::Twine &Name,
|
|
|
|
llvm::unique_function<void()> Action) {
|
2018-01-31 16:51:16 +08:00
|
|
|
{
|
2018-02-13 16:59:23 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-02-08 15:37:35 +08:00
|
|
|
++InFlightTasks;
|
|
|
|
}
|
2018-02-07 03:22:40 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
auto CleanupTask = llvm::make_scope_exit([this]() {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
int NewTasksCnt = --InFlightTasks;
|
|
|
|
if (NewTasksCnt == 0) {
|
|
|
|
// Note: we can't unlock here because we don't want the object to be
|
|
|
|
// destroyed before we notify.
|
|
|
|
TasksReachedZero.notify_one();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
std::thread(
|
2018-02-19 17:56:28 +08:00
|
|
|
[](std::string Name, decltype(Action) Action, decltype(CleanupTask)) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::set_thread_name(Name);
|
2018-02-08 15:37:35 +08:00
|
|
|
Action();
|
|
|
|
// Make sure function stored by Action is destroyed before CleanupTask
|
|
|
|
// is run.
|
|
|
|
Action = nullptr;
|
|
|
|
},
|
2018-02-19 17:56:28 +08:00
|
|
|
Name.str(), std::move(Action), std::move(CleanupTask))
|
2018-02-08 15:37:35 +08:00
|
|
|
.detach();
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
Deadline timeoutSeconds(llvm::Optional<double> Seconds) {
|
2018-02-13 16:59:23 +08:00
|
|
|
using namespace std::chrono;
|
|
|
|
if (!Seconds)
|
2018-03-02 16:56:37 +08:00
|
|
|
return Deadline::infinity();
|
2018-02-13 16:59:23 +08:00
|
|
|
return steady_clock::now() +
|
|
|
|
duration_cast<steady_clock::duration>(duration<double>(*Seconds));
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
void wait(std::unique_lock<std::mutex> &Lock, std::condition_variable &CV,
|
|
|
|
Deadline D) {
|
|
|
|
if (D == Deadline::zero())
|
|
|
|
return;
|
|
|
|
if (D == Deadline::infinity())
|
|
|
|
return CV.wait(Lock);
|
|
|
|
CV.wait_until(Lock, D.time());
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|