mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Applied thread pool implementation from Windows platform
PiperOrigin-RevId: 449385710
This commit is contained in:
committed by
Copybara-Service
parent
412f9e0752
commit
4aee0b681c
@@ -14,64 +14,41 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/executor.h"
|
||||
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.System.Threading.Core.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.System.Threading.h"
|
||||
#include "internal/platform/implementation/windows/runner.h"
|
||||
#include "internal/platform/implementation/windows/thread_pool.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
|
||||
Executor::Executor() : Executor(1) {
|
||||
// Call thread pool creator
|
||||
}
|
||||
Executor::Executor() : Executor(1) {}
|
||||
|
||||
Executor::Executor(int32_t max_concurrency)
|
||||
: thread_pool_(std::make_unique<ThreadPool>(max_concurrency, false)),
|
||||
executor_state_(ExecutorState::NotReady),
|
||||
max_concurrency_(max_concurrency) {
|
||||
if (max_concurrency_ < 1) {
|
||||
throw(std::invalid_argument("max_concurrency"));
|
||||
}
|
||||
|
||||
InitializeThreadPool();
|
||||
: max_concurrency_(max_concurrency) {
|
||||
assert(max_concurrency_ >= 1);
|
||||
thread_pool_ = ThreadPool::Create(max_concurrency);
|
||||
assert(thread_pool_ != nullptr);
|
||||
}
|
||||
|
||||
bool Executor::InitializeThreadPool() {
|
||||
if (executor_state_ != ExecutorState::NotReady) {
|
||||
// To create a new pool, destroy the existing one first
|
||||
return false;
|
||||
}
|
||||
|
||||
thread_pool_->SetPoolSize(max_concurrency_);
|
||||
thread_pool_->Create();
|
||||
executor_state_ = ExecutorState::Ready;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html#execute-java.lang.Runnable-
|
||||
void Executor::Execute(Runnable&& runnable) {
|
||||
if (shut_down_) {
|
||||
NEARBY_LOGS(VERBOSE) << "Warning: " << __func__
|
||||
<< ": Attempt to execute on a shut down pool.";
|
||||
<< ": Attempt to execute on a shut down pool.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (runnable == nullptr) {
|
||||
NEARBY_LOGS(VERBOSE) << "Error: " << __func__ << "Runnable was null.";
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Runnable was null.";
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_ptr<Runner> runner = std::make_unique<Runner>(runnable);
|
||||
thread_pool_->Run(std::move(runner));
|
||||
thread_pool_->Run(std::move(runnable));
|
||||
}
|
||||
|
||||
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html#shutdown--
|
||||
void Executor::Shutdown() {
|
||||
shut_down_ = true;
|
||||
thread_pool_->ShutDown();
|
||||
thread_pool_ = nullptr;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user