From 3248cb7c6a6b1f3fcd5a98776151846a72954807 Mon Sep 17 00:00:00 2001 From: Mattias Axelsson Date: Fri, 30 Aug 2024 20:21:28 +0200 Subject: [PATCH] Replace pthread_mutex with std::mutex This allows use of std::scoped_lock and a few gotos can be removed. Change-Id: I9f2ec9ccac35d3600d64ae438a277432fd53a5b2 --- src/inference.cpp | 8 +------- src/inference.h | 3 ++- src/video_capture.cpp | 23 +++++------------------ src/video_capture.h | 3 ++- 4 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/inference.cpp b/src/inference.cpp index 0cbd1b5..49caf24 100644 --- a/src/inference.cpp +++ b/src/inference.cpp @@ -68,11 +68,6 @@ Inference::Inference(const bool verbose, TRACELOG << "Init chipId=" << chipId << endl; - if (pthread_mutex_init(&_mtx, NULL) != 0) { - ERRORLOG << "Init mutex FAILED" << endl; - throw runtime_error("Could not Init Inference Service"); - } - // Connect to larod service if (!larodConnect(&_conn, &error)) { PrintError("Connecting to larod FAILED", error); @@ -189,7 +184,7 @@ Status Inference::Predict(ServerContext* context, auto& model = model_it->second; // Make larod calls atomic and threadsafe - pthread_mutex_lock(&_mtx); + scoped_lock lock(_mutex); // Clear class data _ppModel = nullptr; @@ -309,7 +304,6 @@ Status Inference::Predict(ServerContext* context, larodDestroyModel(&_ppModel); CloseTmpFiles(inFiles); CloseTmpFiles(outFiles); - pthread_mutex_unlock(&_mtx); larodClearError(&error); return status; } diff --git a/src/inference.h b/src/inference.h index 3cd4786..1dd38a4 100644 --- a/src/inference.h +++ b/src/inference.h @@ -17,6 +17,7 @@ #include "prediction_service.grpc.pb.h" #include "video_capture.h" #include +#include namespace acap_runtime { class Inference : public tensorflow::serving::PredictionService::Service { @@ -77,7 +78,7 @@ class Inference : public tensorflow::serving::PredictionService::Service { std::map _models; larodModel* _ppModel; larodMap* _ppMap; - pthread_mutex_t _mtx; + std::mutex _mutex; larodTensor** _ppInputTensors; larodTensor** _ppOutputTensors; larodTensor** _inputTensors; diff --git a/src/video_capture.cpp b/src/video_capture.cpp index 52d38df..0472d88 100644 --- a/src/video_capture.cpp +++ b/src/video_capture.cpp @@ -40,11 +40,6 @@ namespace acap_runtime { // Initialize the capture service Capture::Capture(const bool verbose) : _verbose(verbose) { TRACELOG << "Init" << endl; - - if (pthread_mutex_init(&_mutex, NULL) != 0) { - ERRORLOG << "Init mutex failed" << endl; - throw runtime_error("Could not initialize VideoCapture service"); - } } // Create a new stream @@ -182,7 +177,7 @@ bool Capture::GetImgDataFromStream(unsigned int stream, PrintStreamInfo(vdoStream); } - pthread_mutex_lock(&_mutex); + scoped_lock lock(_mutex); MaybeDeleteOldestFrame(currentStream->second); @@ -205,8 +200,6 @@ bool Capture::GetImgDataFromStream(unsigned int stream, frameRef = SaveFrame(currentStream->second, buffer, size); - pthread_mutex_unlock(&_mutex); - return true; } @@ -240,8 +233,7 @@ void Capture::MaybeDeleteOldestFrame(Stream& stream) { // Find a frame based on the frame reference and put its data into the response bool Capture::GetDataFromSavedFrame(Stream& stream, uint32_t frameRef, GetFrameResponse* response) { - pthread_mutex_lock(&_mutex); - bool ret = false; + scoped_lock lock(_mutex); gpointer data = NULL; // Find the buffer @@ -250,7 +242,7 @@ bool Capture::GetDataFromSavedFrame(Stream& stream, uint32_t frameRef, GetFrameR }); if (buffer == stream.buffers.end()) { ERRORLOG << "Frame reference " << frameRef << " not found" << endl; - goto end; + return false; } TRACELOG << "Found saved VDO buffer. ID: " << buffer->id << endl; @@ -258,17 +250,12 @@ bool Capture::GetDataFromSavedFrame(Stream& stream, uint32_t frameRef, GetFrameR data = vdo_buffer_get_data(buffer->vdo_buffer); if (nullptr == data) { ERRORLOG << "Getting data from saved buffer failed" << endl; - goto end; + return false; } response->set_data(data, buffer->size); response->set_size(buffer->size); - - ret = true; - -end: - pthread_mutex_unlock(&_mutex); - return ret; + return true; } // Print dimensions and fps of the stream diff --git a/src/video_capture.h b/src/video_capture.h index de8222a..2da1dce 100644 --- a/src/video_capture.h +++ b/src/video_capture.h @@ -22,6 +22,7 @@ #include #include +#include #include "videocapture.grpc.pb.h" @@ -83,7 +84,7 @@ class Capture final : public videocapture::v1::VideoCapture::Service { std::map _streams; bool _verbose; const uint32_t MAX_NBR_SAVED_FRAMES = 3; - pthread_mutex_t _mutex; + std::mutex _mutex; }; } // namespace acap_runtime