Skip to content

Commit

Permalink
Replace pthread_mutex with std::mutex
Browse files Browse the repository at this point in the history
This allows use of std::scoped_lock and a few gotos can be removed.

Change-Id: I9f2ec9ccac35d3600d64ae438a277432fd53a5b2
  • Loading branch information
killenheladagen committed Sep 9, 2024
1 parent 7efbb45 commit 3248cb7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 27 deletions.
8 changes: 1 addition & 7 deletions src/inference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -309,7 +304,6 @@ Status Inference::Predict(ServerContext* context,
larodDestroyModel(&_ppModel);
CloseTmpFiles(inFiles);
CloseTmpFiles(outFiles);
pthread_mutex_unlock(&_mtx);
larodClearError(&error);
return status;
}
Expand Down
3 changes: 2 additions & 1 deletion src/inference.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "prediction_service.grpc.pb.h"
#include "video_capture.h"
#include <larod.h>
#include <mutex>

namespace acap_runtime {
class Inference : public tensorflow::serving::PredictionService::Service {
Expand Down Expand Up @@ -77,7 +78,7 @@ class Inference : public tensorflow::serving::PredictionService::Service {
std::map<std::string, larodModel*> _models;
larodModel* _ppModel;
larodMap* _ppMap;
pthread_mutex_t _mtx;
std::mutex _mutex;
larodTensor** _ppInputTensors;
larodTensor** _ppOutputTensors;
larodTensor** _inputTensors;
Expand Down
23 changes: 5 additions & 18 deletions src/video_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -182,7 +177,7 @@ bool Capture::GetImgDataFromStream(unsigned int stream,
PrintStreamInfo(vdoStream);
}

pthread_mutex_lock(&_mutex);
scoped_lock lock(_mutex);

MaybeDeleteOldestFrame(currentStream->second);

Expand All @@ -205,8 +200,6 @@ bool Capture::GetImgDataFromStream(unsigned int stream,

frameRef = SaveFrame(currentStream->second, buffer, size);

pthread_mutex_unlock(&_mutex);

return true;
}

Expand Down Expand Up @@ -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
Expand All @@ -250,25 +242,20 @@ 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;

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
Expand Down
3 changes: 2 additions & 1 deletion src/video_capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <deque>
#include <map>
#include <mutex>

#include "videocapture.grpc.pb.h"

Expand Down Expand Up @@ -83,7 +84,7 @@ class Capture final : public videocapture::v1::VideoCapture::Service {
std::map<unsigned int, Stream> _streams;
bool _verbose;
const uint32_t MAX_NBR_SAVED_FRAMES = 3;
pthread_mutex_t _mutex;
std::mutex _mutex;
};
} // namespace acap_runtime

Expand Down

0 comments on commit 3248cb7

Please sign in to comment.