kkraus14 commented on code in PR #37040:
URL: https://github.com/apache/arrow/pull/37040#discussion_r1293862129
##########
cpp/src/arrow/device.h:
##########
@@ -98,6 +101,90 @@ class ARROW_EXPORT Device : public
std::enable_shared_from_this<Device>,
/// \brief Return the DeviceAllocationType of this device
virtual DeviceAllocationType device_type() const = 0;
+ /// \brief EXPERIMENTAL: An object that provides event/stream sync primitives
+ ///
+ /// This class is thread-safe.
+ class ARROW_EXPORT SyncEvent {
+ public:
+ virtual ~SyncEvent() = default;
+
+ /// @brief Block until sync event is completed.
+ Status wait() {
+ const std::lock_guard<std::mutex> lock(mx_);
+ return wait_internal();
+ }
+
+ /// @brief Make the provided stream wait on the sync event.
+ ///
+ /// Tells the provided stream that it should wait until the
+ /// synchronization event is completed without blocking the CPU.
+ /// @param stream Should be appropriate for the underlying device
+ Status stream_wait(void* stream) {
+ const std::lock_guard<std::mutex> lock(mx_);
+ return stream_wait_internal(stream);
+ }
+
+ void set_stream(void* stream) {
+ const std::lock_guard<std::mutex> lock(mx_);
+ stream_ = stream;
+ }
+
+ /// @brief Returns the stored raw event or creates a new one to return.
+ ///
+ /// clear_event should always be called to cleanup afterwards. If this
+ /// creates the event, then clear_event will call release_event
+ /// internally. If this doesn't own the event, then the lifetime should
+ /// be controlled externally to this class.
+ Result<void*> get_event() {
+ const std::lock_guard<std::mutex> lock(mx_);
+ if (!sync_event_) {
+ ARROW_ASSIGN_OR_RAISE(sync_event_, create_event());
+ owns_event_ = true;
+ }
+ return sync_event_;
+ }
+
+ void clear_event() {
+ const std::lock_guard<std::mutex> lock(mx_);
+ if (owns_event_) {
+ release_event(sync_event_);
+ }
+ sync_event_ = nullptr;
+ }
Review Comment:
If someone calls `get_event()` from one thread and `clear_event()` from a
different thread, the lock will guarantee that it doesn't blow up in the
`get_event()` call / `clear_event()` call, but the caller of `get_event()`
could be left with a dangling pointer as far as I can tell?
Maybe we should consider using a `shared_ptr` here? Potentially adds
non-trivial overhead though if we're sharing that shared_ptr all over.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]