diff --git a/connections/implementation/bwu_manager.h b/connections/implementation/bwu_manager.h index 85d9929f..354564e6 100644 --- a/connections/implementation/bwu_manager.h +++ b/connections/implementation/bwu_manager.h @@ -122,8 +122,7 @@ class BwuManager : public EndpointManager::FrameProcessor { absl::Seconds(5); void InitBwuHandlers(); - void RunOnBwuManagerThread(const std::string& name, - std::function runnable); + void RunOnBwuManagerThread(const std::string& name, Runnable runnable); std::vector StripOutUnavailableMediums( const std::vector& mediums) const; Medium ChooseBestUpgradeMedium(const std::string& endpoint_id, diff --git a/connections/implementation/mediums/webrtc/connection_flow.cc b/connections/implementation/mediums/webrtc/connection_flow.cc index 73643313..438c3fdd 100644 --- a/connections/implementation/mediums/webrtc/connection_flow.cc +++ b/connections/implementation/mediums/webrtc/connection_flow.cc @@ -520,7 +520,7 @@ bool ConnectionFlow::RunOnSignalingThread(Runnable&& runnable) { // but we can access the signaling thread handle. pc->signaling_thread()->PostTask( [can_run_tasks = std::weak_ptr(can_run_tasks_), - task = std::move(runnable)] { + task = std::move(runnable)]() mutable { // don't run the task if the weak_ptr is no longer valid. // shared_ptr |can_run_tasks_| is destroyed on the same thread // (signaling thread). This guarantees that if the weak_ptr is valid diff --git a/internal/platform/BUILD b/internal/platform/BUILD index f20ce238..780b29ee 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -56,6 +56,7 @@ cc_library( deps = [ "//proto:connections_enums_cc_proto", "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/meta:type_traits", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", diff --git a/internal/platform/implementation/apple/Tests/GNCMultiThreadExecutorTest.mm b/internal/platform/implementation/apple/Tests/GNCMultiThreadExecutorTest.mm index f91a8388..886ff646 100644 --- a/internal/platform/implementation/apple/Tests/GNCMultiThreadExecutorTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCMultiThreadExecutorTest.mm @@ -48,9 +48,8 @@ using MultiThreadExecutor = ::nearby::api::SubmittableExecutor; // Execute two runnables that increment the counter. const int kIncrements = 13; - Runnable incrementer = [self]() { self.counter++; }; for (int i = 0; i < kIncrements; i++) { - executor->Execute(std::move(incrementer)); + executor->Execute([self]() { self.counter++; }); [NSThread sleepForTimeInterval:0.01]; } @@ -64,9 +63,8 @@ using MultiThreadExecutor = ::nearby::api::SubmittableExecutor; // Submit two runnables that increment the counter. const int kIncrements = 13; - Runnable incrementer = [self]() { self.counter++; }; for (int i = 0; i < kIncrements; i++) { - executor->DoSubmit(std::move(incrementer)); + executor->DoSubmit([self]() { self.counter++; }); [NSThread sleepForTimeInterval:0.01]; } diff --git a/internal/platform/implementation/apple/Tests/GNCScheduledExecutorTest.mm b/internal/platform/implementation/apple/Tests/GNCScheduledExecutorTest.mm index 2ef2f1f7..4f0740d6 100644 --- a/internal/platform/implementation/apple/Tests/GNCScheduledExecutorTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCScheduledExecutorTest.mm @@ -46,8 +46,6 @@ using ::nearby::api::ScheduledExecutor; XCTestExpectation *expectation = [self expectationWithDescription:@"finished"]; - Runnable incrementer = [self]() { self.counter++; }; - void (^checkCounter)(int, NSTimeInterval, dispatch_block_t) = ^(int expectedCount, NSTimeInterval delay, dispatch_block_t finalBlock) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), @@ -58,13 +56,19 @@ using ::nearby::api::ScheduledExecutor; }; // Schedule two runnables that increment the counter, at 0.4 and 0.8 seconds. - executor->Schedule(std::move(incrementer), absl::Seconds(0.4)); - executor->Schedule(std::move(incrementer), absl::Seconds(0.8)); + executor->Schedule([self]() { self.counter++; }, absl::Seconds(0.4)); + executor->Schedule([self]() { self.counter++; }, absl::Seconds(0.8)); // Check that the counter contains the expected values at 0.2, 0.6, and 1.0 seconds. - checkCounter(0, 0.2, ^{}); - checkCounter(1, 0.6, ^{}); - checkCounter(2, 1.0, ^{ [expectation fulfill]; }); + checkCounter(0, 0.2, + ^{ + }); + checkCounter(1, 0.6, + ^{ + }); + checkCounter(2, 1.0, ^{ + [expectation fulfill]; + }); [self waitForExpectationsWithTimeout:1.2 handler:nil]; } diff --git a/internal/platform/implementation/apple/Tests/GNCSingleThreadExecutorTest.mm b/internal/platform/implementation/apple/Tests/GNCSingleThreadExecutorTest.mm index 92d9c017..e5857cb8 100644 --- a/internal/platform/implementation/apple/Tests/GNCSingleThreadExecutorTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCSingleThreadExecutorTest.mm @@ -45,11 +45,9 @@ using SingleThreadExecutor = ::nearby::api::SubmittableExecutor; - (void)testRunnables { std::unique_ptr executor([self executor]); - Runnable incrementer = [self]() { self.counter++; }; - // Schedule two runnables that increment the counter. - executor->Execute(std::move(incrementer)); - executor->Execute(std::move(incrementer)); + executor->Execute([self]() { self.counter++; }); + executor->Execute([self]() { self.counter++; }); // Check that the counter has the expected value after a moment. [NSThread sleepForTimeInterval:0.01]; diff --git a/internal/platform/implementation/apple/scheduled_executor.mm b/internal/platform/implementation/apple/scheduled_executor.mm index c24efbd6..b4a5e2ec 100644 --- a/internal/platform/implementation/apple/scheduled_executor.mm +++ b/internal/platform/implementation/apple/scheduled_executor.mm @@ -17,6 +17,7 @@ #import #include +#include #include "absl/time/time.h" #import "internal/platform/implementation/apple/atomic_boolean.h" @@ -35,7 +36,7 @@ + (instancetype)wrapperWithRunnable:(nearby::Runnable)runnable { GNCRunnableWrapper *wrapper = [[GNCRunnableWrapper alloc] init]; - wrapper->_runnable = runnable; + wrapper->_runnable = std::move(runnable); wrapper->_canceled = std::make_unique(false); return wrapper; } @@ -102,7 +103,7 @@ std::shared_ptr ScheduledExecutor::Schedule(Runnable &&runnable // Execute the runnable only if the executor is not shutting down, and the runnable isn't // canceled. // Warning: This block should reference only Obj-C objects, and never C++ objects. - if (!impl.shuttingDown && !wrapper->_canceled->Get()) { + if (!impl.shuttingDown && !wrapper->_canceled->Get()) { wrapper->_runnable(); } }]; @@ -111,9 +112,7 @@ std::shared_ptr ScheduledExecutor::Schedule(Runnable &&runnable return std::shared_ptr(cancelable); } -void ScheduledExecutor::Execute(Runnable &&runnable) { - DoSubmit(std::move(runnable)); -} +void ScheduledExecutor::Execute(Runnable &&runnable) { DoSubmit(std::move(runnable)); } bool ScheduledExecutor::DoSubmit(Runnable &&runnable) { if (impl_.shuttingDown) { @@ -121,7 +120,7 @@ bool ScheduledExecutor::DoSubmit(Runnable &&runnable) { } // Submit the runnable to the queue. - Runnable local_runnable = std::move(runnable); + __block Runnable local_runnable = std::move(runnable); [impl_.queue addOperationWithBlock:^{ local_runnable(); }]; diff --git a/internal/platform/implementation/g3/scheduled_executor.cc b/internal/platform/implementation/g3/scheduled_executor.cc index f198cb24..9f66152e 100644 --- a/internal/platform/implementation/g3/scheduled_executor.cc +++ b/internal/platform/implementation/g3/scheduled_executor.cc @@ -16,6 +16,7 @@ #include #include +#include #include "absl/time/clock.h" #include "internal/platform/implementation/cancelable.h" @@ -64,12 +65,12 @@ std::shared_ptr ScheduledExecutor::Schedule( if (executor_.InShutdown()) { return scheduled_cancelable; } - executor_.ScheduleAfter( - delay, [this, scheduled_cancelable, runnable(std::move(runnable))]() { - if (!executor_.InShutdown() && scheduled_cancelable->MarkExecuted()) { - runnable(); - } - }); + executor_.ScheduleAfter(delay, [this, scheduled_cancelable, + runnable(std::move(runnable))]() mutable { + if (!executor_.InShutdown() && scheduled_cancelable->MarkExecuted()) { + runnable(); + } + }); return scheduled_cancelable; } diff --git a/internal/platform/implementation/windows/thread_pool.cc b/internal/platform/implementation/windows/thread_pool.cc index fb11649f..e32511ad 100644 --- a/internal/platform/implementation/windows/thread_pool.cc +++ b/internal/platform/implementation/windows/thread_pool.cc @@ -147,7 +147,7 @@ void ThreadPool::RunNextTask() { NEARBY_LOGS(VERBOSE) << __func__ << ": Run task(" << &tasks_.front() << ")."; - task = tasks_.front(); + task = std::move(tasks_.front()); tasks_.pop(); } } diff --git a/internal/platform/monitored_runnable.cc b/internal/platform/monitored_runnable.cc index 30127a2a..e0952298 100644 --- a/internal/platform/monitored_runnable.cc +++ b/internal/platform/monitored_runnable.cc @@ -14,6 +14,8 @@ #include "internal/platform/monitored_runnable.h" +#include + #include "internal/platform/logging.h" #include "internal/platform/pending_job_registry.h" @@ -25,17 +27,15 @@ absl::Duration kMinReportedTaskDuration = absl::Seconds(10); } // namespace MonitoredRunnable::MonitoredRunnable(Runnable&& runnable) - : runnable_{runnable} {} + : runnable_{std::move(runnable)} {} MonitoredRunnable::MonitoredRunnable(const std::string& name, Runnable&& runnable) - : name_{name}, runnable_{runnable} { + : name_{name}, runnable_{std::move(runnable)} { PendingJobRegistry::GetInstance().AddPendingJob(name_, post_time_); } -MonitoredRunnable::~MonitoredRunnable() = default; - -void MonitoredRunnable::operator()() const { +void MonitoredRunnable::operator()() { auto start_time = SystemClock::ElapsedRealtime(); auto start_delay = start_time - post_time_; if (start_delay >= kMinReportedStartDelay) { diff --git a/internal/platform/monitored_runnable.h b/internal/platform/monitored_runnable.h index d5770aee..bc1571ae 100644 --- a/internal/platform/monitored_runnable.h +++ b/internal/platform/monitored_runnable.h @@ -31,9 +31,8 @@ class MonitoredRunnable { public: explicit MonitoredRunnable(Runnable&& runnable); MonitoredRunnable(const std::string& name, Runnable&& runnable); - ~MonitoredRunnable(); - void operator()() const; + void operator()(); private: const std::string name_; diff --git a/internal/platform/pipe_test.cc b/internal/platform/pipe_test.cc index 3aa0e632..bebb8531 100644 --- a/internal/platform/pipe_test.cc +++ b/internal/platform/pipe_test.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include "gtest/gtest.h" #include "internal/platform/prng.h" @@ -162,7 +163,7 @@ class Thread { ~Thread() { pthread_attr_destroy(&attr_); } void Start(Runnable runnable) { - runnable_ = runnable; + runnable_ = std::move(runnable); pthread_create(&thread_, &attr_, Thread::Body, this); } diff --git a/internal/platform/runnable.h b/internal/platform/runnable.h index b4786b1b..a0360e1f 100644 --- a/internal/platform/runnable.h +++ b/internal/platform/runnable.h @@ -15,7 +15,7 @@ #ifndef PLATFORM_BASE_RUNNABLE_H_ #define PLATFORM_BASE_RUNNABLE_H_ -#include +#include "absl/functional/any_invocable.h" namespace nearby { @@ -24,7 +24,7 @@ namespace nearby { // // https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html -using Runnable = std::function; +using Runnable = absl::AnyInvocable; } // namespace nearby diff --git a/internal/platform/scheduled_executor.h b/internal/platform/scheduled_executor.h index 5baa68bc..f62ad426 100644 --- a/internal/platform/scheduled_executor.h +++ b/internal/platform/scheduled_executor.h @@ -21,15 +21,15 @@ #include "absl/base/thread_annotations.h" #include "absl/time/time.h" -#include "internal/platform/implementation/platform.h" -#include "internal/platform/implementation/scheduled_executor.h" -#include "internal/platform/runnable.h" #include "internal/platform/cancelable.h" #include "internal/platform/cancellable_task.h" +#include "internal/platform/implementation/platform.h" +#include "internal/platform/implementation/scheduled_executor.h" #include "internal/platform/lockable.h" #include "internal/platform/monitored_runnable.h" #include "internal/platform/mutex.h" #include "internal/platform/mutex_lock.h" +#include "internal/platform/runnable.h" #include "internal/platform/thread_check_callable.h" #include "internal/platform/thread_check_runnable.h" diff --git a/internal/platform/settable_future.h b/internal/platform/settable_future.h index aab3fbdb..89e0659b 100644 --- a/internal/platform/settable_future.h +++ b/internal/platform/settable_future.h @@ -16,6 +16,7 @@ #define PLATFORM_PUBLIC_SETTABLE_FUTURE_H_ #include +#include #include "internal/platform/condition_variable.h" #include "internal/platform/mutex.h" @@ -115,7 +116,7 @@ class SettableFuture : public api::SettableFuture { mutable Mutex mutex_; ConditionVariable completed_{&mutex_}; - std::vector>> listeners_; + std::vector> listeners_; bool done_{false}; T value_; Exception exception_{Exception::kFailed}; diff --git a/internal/platform/submittable_executor.h b/internal/platform/submittable_executor.h index 48685bf5..f49ebbdd 100644 --- a/internal/platform/submittable_executor.h +++ b/internal/platform/submittable_executor.h @@ -21,15 +21,15 @@ #include #include "absl/base/thread_annotations.h" +#include "internal/platform/callable.h" +#include "internal/platform/future.h" #include "internal/platform/implementation/executor.h" #include "internal/platform/implementation/submittable_executor.h" -#include "internal/platform/callable.h" -#include "internal/platform/runnable.h" -#include "internal/platform/future.h" #include "internal/platform/lockable.h" #include "internal/platform/monitored_runnable.h" #include "internal/platform/mutex.h" #include "internal/platform/mutex_lock.h" +#include "internal/platform/runnable.h" #include "internal/platform/thread_check_callable.h" #include "internal/platform/thread_check_runnable.h" diff --git a/internal/platform/thread_check_runnable.h b/internal/platform/thread_check_runnable.h index 0d9c2415..2659ef5b 100644 --- a/internal/platform/thread_check_runnable.h +++ b/internal/platform/thread_check_runnable.h @@ -17,9 +17,8 @@ #include -#include "absl/base/thread_annotations.h" -#include "internal/platform/runnable.h" #include "internal/platform/lockable.h" +#include "internal/platform/runnable.h" namespace nearby { @@ -28,9 +27,9 @@ namespace nearby { class ThreadCheckRunnable { public: ThreadCheckRunnable(const Lockable *lockable, Runnable &&runnable) - : lockable_{lockable}, runnable_{runnable} {} + : lockable_{lockable}, runnable_{std::move(runnable)} {} - void operator()() const { + void operator()() { ThreadLockHolder thread_lock(lockable_); runnable_(); }