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.

pull/3667/head
Isaac Connor 2023-01-20 01:38:09 -05:00
parent a0b4aa04c5
commit af57b53583
3 changed files with 16 additions and 15 deletions

View File

@ -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<ZMPacket>&packet) {
{
std::unique_lock<std::mutex> 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<ZMPacket> packet = nullptr;
{
std::unique_lock<std::mutex> 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()

View File

@ -99,7 +99,7 @@ class Event {
void createNotes(std::string &notes);
std::queue<ZMLockedPacket *> packet_queue;
std::queue<std::shared_ptr<ZMPacket>> 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<ZMPacket> &p);
void AddPacket_(const std::shared_ptr<ZMPacket> &p);
bool WritePacket(const std::shared_ptr<ZMPacket> &p);
bool SendFrameImage(const Image *image, bool alarm_frame=false);

View File

@ -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) {