Skip to content

Commit

Permalink
jcr: fix compiler warnings
Browse files Browse the repository at this point in the history
std::find_if is sometimes declared as [[nodiscard]].  As such we
should not ignore its return value.

This commit also slightly refactors the code to remove the use of
std::find_if alltogether.
  • Loading branch information
sebsura authored and BareosBot committed Jan 30, 2024
1 parent 60b8dbb commit 18d9ce0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
6 changes: 3 additions & 3 deletions core/src/include/jcr.h
Expand Up @@ -282,9 +282,9 @@ void InitJcr(std::shared_ptr<JobControlRecord> jcr,
JCR_free_HANDLER* daemon_free_jcr);
std::size_t GetJcrCount();
std::shared_ptr<JobControlRecord> GetJcrById(uint32_t JobId);
std::shared_ptr<JobControlRecord> GetJcrByFullName(std::string name);
std::shared_ptr<JobControlRecord> GetJcrByPartialName(std::string name);
std::shared_ptr<JobControlRecord> GetJcrBySession(const VolumeSessionInfo& vsi);
std::shared_ptr<JobControlRecord> GetJcrByFullName(std::string_view name);
std::shared_ptr<JobControlRecord> GetJcrByPartialName(std::string_view name);
std::shared_ptr<JobControlRecord> GetJcrBySession(VolumeSessionInfo vsi);
uint32_t GetJobIdByThreadId(pthread_t tid);
/* ************* */

Expand Down
68 changes: 29 additions & 39 deletions core/src/lib/jcr.cc
Expand Up @@ -246,7 +246,7 @@ void InitJcr(std::shared_ptr<JobControlRecord> jcr,

LockJobs();
LockJcrChain();
job_control_record_cache.emplace_back(jcr);
job_control_record_cache.emplace_back(std::move(jcr));
UnlockJcrChain();
UnlockJobs();
}
Expand Down Expand Up @@ -474,45 +474,34 @@ JobControlRecord* get_jcr_by_id(uint32_t JobId)
return jcr;
}

static void CleanupExpired(std::vector<std::weak_ptr<JobControlRecord>>& v)
{
v.erase(std::remove_if(v.begin(), v.end(),
[](const auto& p) { return p.expired(); }),
v.end());
}

std::size_t GetJcrCount()
{
LockJcrChain();
std::size_t count = count_if(
job_control_record_cache.begin(), job_control_record_cache.end(),
[](std::weak_ptr<JobControlRecord>& p) { return !p.expired(); });
UnlockJcrChain();
std::unique_lock _{jcr_chain_mutex};

return count;
CleanupExpired(job_control_record_cache);

return job_control_record_cache.size();
}

static std::shared_ptr<JobControlRecord> GetJcr(
std::function<bool(const JobControlRecord*)> compare)
template <typename P>
static std::shared_ptr<JobControlRecord> GetJcr(P predicate)
{
std::shared_ptr<JobControlRecord> result;

LockJcrChain();
std::unique_lock _{jcr_chain_mutex};

// cleanup chache
job_control_record_cache.erase(
std::remove_if(
job_control_record_cache.begin(), job_control_record_cache.end(),
[](std::weak_ptr<JobControlRecord>& p) { return p.expired(); }),
job_control_record_cache.end());

std::ignore = find_if(
job_control_record_cache.begin(), job_control_record_cache.end(),
[&compare, &result](std::weak_ptr<JobControlRecord>& p) {
auto jcr = p.lock();
if (compare(jcr.get())) {
result = jcr;
return true;
}
return false;
});
CleanupExpired(job_control_record_cache);

UnlockJcrChain();
for (const auto& ptr : job_control_record_cache) {
if (auto jcr = ptr.lock(); jcr && predicate(jcr.get())) { return jcr; }
}

return result;
return {};
}

std::shared_ptr<JobControlRecord> GetJcrById(uint32_t JobId)
Expand All @@ -521,23 +510,24 @@ std::shared_ptr<JobControlRecord> GetJcrById(uint32_t JobId)
[JobId](const JobControlRecord* jcr) { return jcr->JobId == JobId; });
}

std::shared_ptr<JobControlRecord> GetJcrByFullName(std::string name)
std::shared_ptr<JobControlRecord> GetJcrByFullName(std::string_view name)
{
return GetJcr([&name](const JobControlRecord* jcr) {
return std::string{jcr->Job} == name;
return GetJcr([name](const JobControlRecord* jcr) {
return std::string_view{jcr->Job} == name;
});
}

std::shared_ptr<JobControlRecord> GetJcrByPartialName(std::string name)
std::shared_ptr<JobControlRecord> GetJcrByPartialName(std::string_view name)
{
return GetJcr([&name](const JobControlRecord* jcr) {
return std::string{jcr->Job}.find(name) == 0;
return GetJcr([name](const JobControlRecord* jcr) {
return std::string_view{jcr->Job}.find(name) == 0;
});
}

std::shared_ptr<JobControlRecord> GetJcrBySession(const VolumeSessionInfo& vsi)
std::shared_ptr<JobControlRecord> GetJcrBySession(VolumeSessionInfo vsi)
{
return GetJcr([&vsi](const JobControlRecord* jcr) {
static_assert(sizeof(vsi) == 8);
return GetJcr([vsi](const JobControlRecord* jcr) {
return (VolumeSessionInfo{jcr->VolSessionId, jcr->VolSessionTime} == vsi);
});
}
Expand Down

0 comments on commit 18d9ce0

Please sign in to comment.