Caffe2 - C++ API
A deep learning, cross platform ML framework
store_handler.h
1 
17 #pragma once
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <stdexcept>
22 #include <string>
23 #include <vector>
24 
25 namespace caffe2 {
26 
27 class StoreHandler {
28  public:
29  static constexpr std::chrono::milliseconds kDefaultTimeout =
30  std::chrono::seconds(30);
31  static constexpr std::chrono::milliseconds kNoTimeout =
32  std::chrono::milliseconds::zero();
33 
34  virtual ~StoreHandler();
35 
36  /*
37  * Set data for the key if it doesn't exist.
38  * If the key exists the data should be the same as the existing key.
39  */
40  virtual void set(const std::string& name, const std::string& data) = 0;
41 
42  /*
43  * Get the data for the key.
44  * The call should wait until the key is stored with default timeout
45  * and return data if set else fail.
46  */
47  virtual std::string get(const std::string& name) = 0;
48 
49  /*
50  * Does an atomic add operation on the key and returns the latest updated
51  * value.
52  * Note: To access the current value for this counter call with value = 0
53  */
54  virtual int64_t add(const std::string& name, int64_t value) = 0;
55 
56  /*
57  * Check if a keys exist in the store.
58  */
59  virtual bool check(const std::vector<std::string>& names) = 0;
60 
61  /*
62  * Wait for Keys to be stored.
63  */
64  virtual void wait(
65  const std::vector<std::string>& names,
66  const std::chrono::milliseconds& timeout = kDefaultTimeout) = 0;
67 };
68 
69 struct StoreHandlerTimeoutException : public std::runtime_error {
70  StoreHandlerTimeoutException() = default;
71  explicit StoreHandlerTimeoutException(const std::string& msg)
72  : std::runtime_error(msg) {}
73 };
74 
75 #define STORE_HANDLER_TIMEOUT(...) \
76  throw ::caffe2::StoreHandlerTimeoutException( \
77  ::caffe2::MakeString("[", __FILE__, ":", __LINE__, "] ", __VA_ARGS__));
78 }
Copyright (c) 2016-present, Facebook, Inc.