mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Minor refactor.
This commit is contained in:
@@ -16,21 +16,18 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "internal/platform/implementation/linux/thread_pool.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
Executor::Executor() : Executor(1) {}
|
||||
|
||||
Executor::Executor(int32_t max_concurrency)
|
||||
: max_concurrency_(max_concurrency) {
|
||||
assert(max_concurrency_ >= 1);
|
||||
thread_pool_ = linux::ThreadPool::Create(max_concurrency);
|
||||
Executor::Executor(size_t max_concurrency)
|
||||
: thread_pool_(std::make_unique<ThreadPool>(max_concurrency)) {
|
||||
assert(max_concurrency >= 1);
|
||||
assert(thread_pool_ != nullptr);
|
||||
}
|
||||
|
||||
void Executor::Execute(Runnable&& runnable) {
|
||||
void Executor::Execute(Runnable &&runnable) {
|
||||
if (shut_down_) {
|
||||
NEARBY_LOGS(VERBOSE) << "Warning: " << __func__
|
||||
<< ": Attempt to execute on a shut down pool.";
|
||||
@@ -51,5 +48,5 @@ void Executor::Shutdown() {
|
||||
thread_pool_ = nullptr;
|
||||
}
|
||||
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -28,8 +28,7 @@ namespace linux {
|
||||
// Executor.
|
||||
class Executor : public api::Executor {
|
||||
public:
|
||||
Executor();
|
||||
explicit Executor(int max_concurrency);
|
||||
Executor(size_t max_concurrency = 1);
|
||||
|
||||
// Before returning from destructor, executor must wait for all pending
|
||||
// jobs to finish.
|
||||
@@ -41,7 +40,6 @@ class Executor : public api::Executor {
|
||||
private:
|
||||
std::unique_ptr<linux::ThreadPool> thread_pool_ = nullptr;
|
||||
std::atomic<bool> shut_down_ = false;
|
||||
int32_t max_concurrency_;
|
||||
};
|
||||
|
||||
} // namespace linux
|
||||
|
||||
@@ -32,8 +32,8 @@ ScheduledExecutor::ScheduledExecutor()
|
||||
// We want Cancelable to live until both caller and executor are done with it.
|
||||
// Exclusive ownership model does not work for this case;
|
||||
// using std:shared_ptr<> instead of std::unique_ptr<>.
|
||||
std::shared_ptr<api::Cancelable> ScheduledExecutor::Schedule(
|
||||
Runnable&& runnable, absl::Duration duration) {
|
||||
std::shared_ptr<api::Cancelable>
|
||||
ScheduledExecutor::Schedule(Runnable &&runnable, absl::Duration duration) {
|
||||
if (shut_down_) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Attempt to Schedule on a shut down executor.";
|
||||
@@ -42,9 +42,11 @@ std::shared_ptr<api::Cancelable> ScheduledExecutor::Schedule(
|
||||
}
|
||||
|
||||
// Cleans completed tasks
|
||||
std::remove_if(
|
||||
scheduled_tasks_.begin(), scheduled_tasks_.end(),
|
||||
[](std::shared_ptr<ScheduledTask>& task) { return task->IsDone(); });
|
||||
scheduled_tasks_.erase(
|
||||
std::remove_if(
|
||||
scheduled_tasks_.begin(), scheduled_tasks_.end(),
|
||||
[](std::shared_ptr<ScheduledTask> &task) { return task->IsDone(); }),
|
||||
scheduled_tasks_.end());
|
||||
|
||||
std::shared_ptr<ScheduledTask> task =
|
||||
std::make_shared<ScheduledTask>(std::move(runnable), duration);
|
||||
@@ -54,7 +56,7 @@ std::shared_ptr<api::Cancelable> ScheduledExecutor::Schedule(
|
||||
return task;
|
||||
}
|
||||
|
||||
void ScheduledExecutor::Execute(Runnable&& runnable) {
|
||||
void ScheduledExecutor::Execute(Runnable &&runnable) {
|
||||
if (shut_down_) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Attempt to Execute on a shut down executor.";
|
||||
@@ -67,7 +69,7 @@ void ScheduledExecutor::Execute(Runnable&& runnable) {
|
||||
void ScheduledExecutor::Shutdown() {
|
||||
if (!shut_down_) {
|
||||
shut_down_ = true;
|
||||
for (auto& task : scheduled_tasks_) {
|
||||
for (auto &task : scheduled_tasks_) {
|
||||
task->Cancel();
|
||||
}
|
||||
|
||||
@@ -78,5 +80,5 @@ void ScheduledExecutor::Shutdown() {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Attempt to Shutdown on a shut down executor.";
|
||||
}
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -20,9 +20,7 @@
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
SubmittableExecutor::SubmittableExecutor() : SubmittableExecutor(1) {}
|
||||
|
||||
SubmittableExecutor::SubmittableExecutor(int32_t max_concurrancy)
|
||||
SubmittableExecutor::SubmittableExecutor(size_t max_concurrancy)
|
||||
: executor_(std::make_unique<nearby::linux::Executor>(max_concurrancy)),
|
||||
shut_down_(false) {}
|
||||
|
||||
|
||||
@@ -27,8 +27,7 @@ namespace linux {
|
||||
// Platform must override bool submit(absl::AnyInvocable<void()>) method.
|
||||
class SubmittableExecutor : public api::SubmittableExecutor {
|
||||
public:
|
||||
SubmittableExecutor();
|
||||
SubmittableExecutor(int32_t maxConcurrancy);
|
||||
SubmittableExecutor(size_t maxConcurrancy = 1);
|
||||
~SubmittableExecutor() override = default;
|
||||
|
||||
// Submit a callable (with no delay).
|
||||
|
||||
Reference in New Issue
Block a user