Caffe2 - C++ API
A deep learning, cross platform ML framework
net_async_task_future.h
1 #ifndef CAFFE2_NET_ASYNC_TASK_FUTURE_H
2 #define CAFFE2_NET_ASYNC_TASK_FUTURE_H
3 
4 #include <atomic>
5 #include <condition_variable>
6 #include <functional>
7 #include <memory>
8 #include <mutex>
9 #include <string>
10 #include <vector>
11 
12 namespace caffe2 {
13 
14 // Represents the state of AsyncTask execution, that can be queried with
15 // IsCompleted/IsFailed. Callbacks are supported through SetCallback and
16 // are called upon future's completion.
17 
19  public:
21  // Creates a future completed when all given futures are completed
22  explicit AsyncTaskFuture(const std::vector<AsyncTaskFuture*>& futures);
23  ~AsyncTaskFuture();
24 
25  AsyncTaskFuture(const AsyncTaskFuture&) = delete;
26 
27  AsyncTaskFuture& operator=(const AsyncTaskFuture&) = delete;
28 
29  bool IsCompleted() const;
30 
31  bool IsFailed() const;
32 
33  std::string ErrorMessage() const;
34 
35  void Wait() const;
36 
37  void SetCallback(std::function<void(const AsyncTaskFuture*)> callback);
38 
39  void SetCompleted(const char* err_msg = nullptr);
40 
41  void ResetState();
42 
43  private:
44  mutable std::mutex mutex_;
45  mutable std::condition_variable cv_completed_;
46  std::atomic<bool> completed_;
47  std::atomic<bool> failed_;
48  std::string err_msg_;
49  std::vector<std::function<void(const AsyncTaskFuture*)>> callbacks_;
50 
51  struct ParentCounter {
52  explicit ParentCounter(int init_parent_count)
53  : init_parent_count_(init_parent_count),
54  parent_count(init_parent_count),
55  parent_failed(false) {}
56 
57  void Reset() {
58  std::unique_lock<std::mutex> lock(err_mutex);
59  parent_count = init_parent_count_;
60  parent_failed = false;
61  err_msg = "";
62  }
63 
64  const int init_parent_count_;
65  std::atomic<int> parent_count;
66  std::mutex err_mutex;
67  std::atomic<bool> parent_failed;
68  std::string err_msg;
69  };
70 
71  std::unique_ptr<ParentCounter> parent_counter_;
72 };
73 
74 } // namespace caffe2
75 
76 #endif // CAFFE2_NET_ASYNC_TASK_FUTURE_H
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13