• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_UTIL_REFFED_STATUS_CALLBACK_H_
17 #define TENSORFLOW_CORE_UTIL_REFFED_STATUS_CALLBACK_H_
18 
19 #include "tensorflow/core/lib/core/refcount.h"
20 #include "tensorflow/core/lib/core/status.h"
21 #include "tensorflow/core/platform/mutex.h"
22 
23 namespace tensorflow {
24 
25 // The ReffedStatusCallback is a refcounted object that accepts a
26 // StatusCallback.  When it is destroyed (its refcount goes to 0), the
27 // StatusCallback is called with the first non-OK status passed to
28 // UpdateStatus(), or Status::OK() if no non-OK status was set.
29 class ReffedStatusCallback : public core::RefCounted {
30  public:
ReffedStatusCallback(StatusCallback done)31   explicit ReffedStatusCallback(StatusCallback done)
32       : done_(std::move(done)), status_(Status::OK()) {}
33 
UpdateStatus(const Status & s)34   void UpdateStatus(const Status& s) {
35     if (!s.ok()) {
36       mutex_lock lock(mu_);
37       if (status_.ok()) status_.Update(s);
38     }
39   }
40 
ok()41   bool ok() {
42     mutex_lock lock(mu_);
43     return status_.ok();
44   }
45 
46   // Returns a copy of the current status.
status()47   Status status() {
48     mutex_lock lock(mu_);
49     return status_;
50   }
51 
~ReffedStatusCallback()52   ~ReffedStatusCallback() { done_(status_); }
53 
54  private:
55   StatusCallback done_;
56   mutex mu_;
57   Status status_ GUARDED_BY(mu_);
58 };
59 
60 }  // namespace tensorflow
61 
62 #endif  // TENSORFLOW_CORE_UTIL_REFFED_STATUS_CALLBACK_H_
63