mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Migrate to absl::AnyInvocable
PiperOrigin-RevId: 501392167
This commit is contained in:
committed by
Copybara-Service
parent
3be5844fc3
commit
4fc1583b94
@@ -122,8 +122,7 @@ class BwuManager : public EndpointManager::FrameProcessor {
|
||||
absl::Seconds(5);
|
||||
|
||||
void InitBwuHandlers();
|
||||
void RunOnBwuManagerThread(const std::string& name,
|
||||
std::function<void()> runnable);
|
||||
void RunOnBwuManagerThread(const std::string& name, Runnable runnable);
|
||||
std::vector<Medium> StripOutUnavailableMediums(
|
||||
const std::vector<Medium>& mediums) const;
|
||||
Medium ChooseBestUpgradeMedium(const std::string& endpoint_id,
|
||||
|
||||
@@ -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<void>(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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
@@ -45,11 +45,9 @@ using SingleThreadExecutor = ::nearby::api::SubmittableExecutor;
|
||||
- (void)testRunnables {
|
||||
std::unique_ptr<SingleThreadExecutor> 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];
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#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<nearby::apple::AtomicBoolean>(false);
|
||||
return wrapper;
|
||||
}
|
||||
@@ -102,7 +103,7 @@ std::shared_ptr<api::Cancelable> 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<api::Cancelable> ScheduledExecutor::Schedule(Runnable &&runnable
|
||||
return std::shared_ptr<api::Cancelable>(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();
|
||||
}];
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/time/clock.h"
|
||||
#include "internal/platform/implementation/cancelable.h"
|
||||
@@ -64,12 +65,12 @@ std::shared_ptr<api::Cancelable> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
#include "internal/platform/monitored_runnable.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#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) {
|
||||
|
||||
@@ -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_;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#ifndef PLATFORM_BASE_RUNNABLE_H_
|
||||
#define PLATFORM_BASE_RUNNABLE_H_
|
||||
|
||||
#include <functional>
|
||||
#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<void()>;
|
||||
using Runnable = absl::AnyInvocable<void()>;
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define PLATFORM_PUBLIC_SETTABLE_FUTURE_H_
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "internal/platform/condition_variable.h"
|
||||
#include "internal/platform/mutex.h"
|
||||
@@ -115,7 +116,7 @@ class SettableFuture : public api::SettableFuture<T> {
|
||||
|
||||
mutable Mutex mutex_;
|
||||
ConditionVariable completed_{&mutex_};
|
||||
std::vector<std::pair<api::Executor*, std::function<void()>>> listeners_;
|
||||
std::vector<std::pair<api::Executor*, Runnable>> listeners_;
|
||||
bool done_{false};
|
||||
T value_;
|
||||
Exception exception_{Exception::kFailed};
|
||||
|
||||
@@ -21,15 +21,15 @@
|
||||
#include <utility>
|
||||
|
||||
#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"
|
||||
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
#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_();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user