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 "absl/strings/str_cat.h" |
20 | #include "tensorflow/core/lib/core/refcount.h" |
21 | #include "tensorflow/core/lib/core/status.h" |
22 | #include "tensorflow/core/platform/mutex.h" |
23 | |
24 | namespace tensorflow { |
25 | |
26 | // The ReffedStatusCallback is a refcounted object that accepts a |
27 | // StatusCallback. When it is destroyed (its refcount goes to 0), the |
28 | // StatusCallback is called with the first non-OK status passed to |
29 | // UpdateStatus(), or Status::OK() if no non-OK status was set. |
30 | class ReffedStatusCallback : public core::RefCounted { |
31 | public: |
32 | explicit ReffedStatusCallback(StatusCallback done) : done_(std::move(done)) {} |
33 | |
34 | void UpdateStatus(const Status& s) { |
35 | mutex_lock lock(mu_); |
36 | status_group_.Update(s); |
37 | } |
38 | |
39 | bool ok() { |
40 | tf_shared_lock lock(mu_); |
41 | return status_group_.ok(); |
42 | } |
43 | |
44 | // Returns a copy of the current status. |
45 | Status status() { |
46 | tf_shared_lock lock(mu_); |
47 | return status_group_.as_summary_status(); |
48 | } |
49 | |
50 | ~ReffedStatusCallback() { done_(status_group_.as_summary_status()); } |
51 | |
52 | private: |
53 | StatusCallback done_; |
54 | mutex mu_; |
55 | StatusGroup status_group_ TF_GUARDED_BY(mu_); |
56 | }; |
57 | |
58 | } // namespace tensorflow |
59 | |
60 | #endif // TENSORFLOW_CORE_UTIL_REFFED_STATUS_CALLBACK_H_ |
61 |