From af57b5358345554a9ae469cec70f9e04401d3660 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 20 Jan 2023 01:38:09 -0500 Subject: [PATCH] Queue packets instead of packet locks in event thread. Since we are using std::shared_ptr and not modifying the packet, should not need locking. Also, locking in one thread and unlocking in another is apparentlyundefined behaviour and doesn't work infreebsd. --- src/zm_event.cpp | 18 ++++++++---------- src/zm_event.h | 4 ++-- src/zm_monitor.cpp | 9 ++++++--- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/zm_event.cpp b/src/zm_event.cpp index d4d4527ec..9817728b4 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -333,10 +333,11 @@ void Event::updateNotes(const StringSetMap &newNoteSetMap) { } // end if update } // void Event::updateNotes(const StringSetMap &newNoteSetMap) -void Event::AddPacket(ZMLockedPacket *packetlock) { +void Event::AddPacket(const std::shared_ptr&packet) { { std::unique_lock lck(packet_queue_mutex); - packet_queue.push(packetlock); + + packet_queue.push(std::move(packet)); } packet_queue_condition.notify_one(); } @@ -715,11 +716,10 @@ void Event::Run() { if (storage != monitor->getStorage()) delete storage; - // The idea is to process the queue no matter what so that all packets get processed. // We only break if the queue is empty while (true) { - ZMLockedPacket * packet_lock = nullptr; + std::shared_ptr packet = nullptr; { std::unique_lock lck(packet_queue_mutex); @@ -729,15 +729,13 @@ void Event::Run() { } if (!packet_queue.empty()) { - // Packets on this queue are locked. They are locked by analysis thread - packet_lock = packet_queue.front(); + packet = packet_queue.front(); packet_queue.pop(); } } // end lock scope - if (packet_lock) { - Debug(1, "Adding packet %d", packet_lock->packet_->image_index); - this->AddPacket_(packet_lock->packet_); - delete packet_lock; + if (packet) { + Debug(1, "Adding packet %d", packet->image_index); + this->AddPacket_(packet); } } // end while } // end Run() diff --git a/src/zm_event.h b/src/zm_event.h index 31a5a6355..73cd9b85b 100644 --- a/src/zm_event.h +++ b/src/zm_event.h @@ -99,7 +99,7 @@ class Event { void createNotes(std::string ¬es); - std::queue packet_queue; + std::queue> packet_queue; std::mutex packet_queue_mutex; std::condition_variable packet_queue_condition; @@ -128,7 +128,7 @@ class Event { const struct timeval &StartTime() const { return start_time; } const struct timeval &EndTime() const { return end_time; } - void AddPacket(ZMLockedPacket *); + void AddPacket(const std::shared_ptr &p); void AddPacket_(const std::shared_ptr &p); bool WritePacket(const std::shared_ptr &p); bool SendFrameImage(const Image *image, bool alarm_frame=false); diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 3af580723..2abf983ac 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -2143,7 +2143,7 @@ bool Monitor::Analyse() { } if (event) { - event->AddPacket(packet_lock); + event->AddPacket(snap); } else { // In the case where people have pre-alarm frames, the web ui will generate the frame images // from the mp4. So no one will notice anyways. @@ -2164,8 +2164,8 @@ bool Monitor::Analyse() { if (snap->out_frame) av_frame_free(&snap->out_frame); if (snap->buffer) av_freep(&snap->buffer); - delete packet_lock; } + delete packet_lock; } // end scope for event_lock packetqueue.increment_it(analysis_it); @@ -2703,7 +2703,10 @@ Event * Monitor::openEvent( // Write out starting packets, do not modify packetqueue it will garbage collect itself while (starting_packet_lock && (*start_it != *analysis_it) && !zm_terminate) { ZM_DUMP_PACKET(starting_packet_lock->packet_->packet, "Queuing packet for event"); - event->AddPacket(starting_packet_lock); + + event->AddPacket(starting_packet); + delete starting_packet_lock; + starting_packet_lock = nullptr; packetqueue.increment_it(start_it); if ((*start_it) != *analysis_it) {