1 #include "caffe2/core/net_async_task_future.h" 3 #include "c10/util/Logging.h" 4 #include "caffe2/core/common.h" 8 AsyncTaskFuture::AsyncTaskFuture() : completed_(false), failed_(false) {}
10 AsyncTaskFuture::AsyncTaskFuture(
const std::vector<AsyncTaskFuture*>& futures)
11 : completed_(false), failed_(false) {
12 if (futures.size() > 1) {
13 parent_counter_ = caffe2::make_unique<ParentCounter>(futures.size());
14 for (
auto future : futures) {
15 future->SetCallback([
this](
const AsyncTaskFuture* f) {
17 std::unique_lock<std::mutex> lock(parent_counter_->err_mutex);
18 if (parent_counter_->parent_failed) {
19 parent_counter_->err_msg +=
", " + f->ErrorMessage();
21 parent_counter_->parent_failed =
true;
22 parent_counter_->err_msg = f->ErrorMessage();
25 int count = --parent_counter_->parent_count;
28 if (!parent_counter_->parent_failed) {
31 SetCompleted(parent_counter_->err_msg.c_str());
37 CAFFE_ENFORCE_EQ(futures.size(), 1);
38 auto future = futures.back();
39 future->SetCallback([
this](
const AsyncTaskFuture* f) {
43 SetCompleted(f->ErrorMessage().c_str());
49 bool AsyncTaskFuture::IsCompleted()
const {
53 bool AsyncTaskFuture::IsFailed()
const {
57 std::string AsyncTaskFuture::ErrorMessage()
const {
61 void AsyncTaskFuture::Wait()
const {
62 std::unique_lock<std::mutex> lock(mutex_);
64 cv_completed_.wait(lock);
68 void AsyncTaskFuture::SetCallback(
69 std::function<
void(
const AsyncTaskFuture*)> callback) {
70 std::unique_lock<std::mutex> lock(mutex_);
72 callbacks_.push_back(callback);
78 void AsyncTaskFuture::SetCompleted(
const char* err_msg) {
79 std::unique_lock<std::mutex> lock(mutex_);
81 CAFFE_ENFORCE(!completed_,
"Calling SetCompleted on a completed future");
89 for (
auto& callback : callbacks_) {
93 cv_completed_.notify_all();
98 void AsyncTaskFuture::ResetState() {
99 std::unique_lock<std::mutex> lock(mutex_);
100 if (parent_counter_) {
101 parent_counter_->Reset();
108 AsyncTaskFuture::~AsyncTaskFuture() {}
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...