@@ -17,6 +17,7 @@ limitations under the License.
17
17
#define XLA_PJRT_GPU_TFRT_GPU_EVENT_H_
18
18
19
19
#include < cstddef>
20
+ #include < utility>
20
21
21
22
#include " absl/container/inlined_vector.h"
22
23
#include " absl/types/span.h"
@@ -63,6 +64,31 @@ class TfrtEventSet {
63
64
absl::InlinedVector<tsl::AsyncValueRef<GpuEvent>, 4 > events_;
64
65
};
65
66
67
+ // A RAII helper class used to set an AsyncValueRef<GpuEvent> to a ready state
68
+ // upon destruction. In many cases in PjRt implementation, there will be
69
+ // multiple return statements in the function, all of which require setting
70
+ // some AsyncValueRef<GpuEvent> to be ready. This class could make such code
71
+ // more robust by using setting the AsyncValue in the destructor.
72
+ class MarkEventReadyOnExit {
73
+ public:
74
+ explicit MarkEventReadyOnExit (tsl::AsyncValueRef<GpuEvent> event)
75
+ : event_(std::move(event)) {}
76
+
77
+ MarkEventReadyOnExit (const MarkEventReadyOnExit&) = delete ;
78
+ MarkEventReadyOnExit& operator =(const MarkEventReadyOnExit&) = delete ;
79
+ MarkEventReadyOnExit (MarkEventReadyOnExit&&) = default ;
80
+ MarkEventReadyOnExit& operator =(MarkEventReadyOnExit&&) = default ;
81
+
82
+ ~MarkEventReadyOnExit () {
83
+ if (event_) event_.SetStateConcrete ();
84
+ }
85
+
86
+ tsl::AsyncValueRef<GpuEvent> Release () && { return std::move (event_); }
87
+
88
+ private:
89
+ tsl::AsyncValueRef<GpuEvent> event_;
90
+ };
91
+
66
92
} // namespace xla
67
93
68
94
#endif // XLA_PJRT_GPU_TFRT_GPU_EVENT_H_
0 commit comments