Skip to content

Commit

Permalink
jcr: switch to std::mutex/std::unique_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura authored and BareosBot committed Mar 18, 2024
1 parent 4ad0590 commit 385bc7a
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 98 deletions.
9 changes: 3 additions & 6 deletions core/src/dird/job.cc
Expand Up @@ -140,7 +140,7 @@ bool SetupJob(JobControlRecord* jcr, bool suppress_output)
{
int errstat;

jcr->lock();
std::unique_lock l(jcr->mutex_guard());

// See if we should suppress all output.
if (!suppress_output) {
Expand All @@ -154,7 +154,6 @@ bool SetupJob(JobControlRecord* jcr, bool suppress_output)
BErrNo be;
Jmsg1(jcr, M_FATAL, 0, T_("Unable to init job cond variable: ERR=%s\n"),
be.bstrerror(errstat));
jcr->unlock();
goto bail_out;
}
jcr->dir_impl->term_wait_inited = true;
Expand All @@ -165,14 +164,13 @@ bool SetupJob(JobControlRecord* jcr, bool suppress_output)
Jmsg1(jcr, M_FATAL, 0,
T_("Unable to init job nextrun cond variable: ERR=%s\n"),
be.bstrerror(errstat));
jcr->unlock();
goto bail_out;
}
jcr->dir_impl->nextrun_ready_inited = true;

CreateUniqueJobName(jcr, jcr->dir_impl->res.job->resource_name_);
jcr->setJobStatusWithPriorityCheck(JS_Created);
jcr->unlock();
l.unlock();

// Open database
Dmsg0(100, "Open database\n");
Expand Down Expand Up @@ -654,13 +652,12 @@ static void* job_thread(void* arg)

void SdMsgThreadSendSignal(JobControlRecord* jcr, int sig)
{
jcr->lock();
std::unique_lock l(jcr->mutex_guard());
if (!jcr->dir_impl->sd_msg_thread_done && jcr->dir_impl->SD_msg_chan_started
&& !pthread_equal(jcr->dir_impl->SD_msg_chan, pthread_self())) {
Dmsg1(800, "Send kill to SD msg chan jid=%d\n", jcr->JobId);
pthread_kill(jcr->dir_impl->SD_msg_chan, sig);
}
jcr->unlock();
}

/**
Expand Down
14 changes: 9 additions & 5 deletions core/src/dird/msgchan.cc
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -436,10 +436,14 @@ extern "C" void MsgThreadCleanup(void* arg)
JobControlRecord* jcr = (JobControlRecord*)arg;

jcr->db->EndTransaction(jcr); /* Terminate any open transaction */
jcr->lock();
jcr->dir_impl->sd_msg_thread_done = true;
jcr->dir_impl->SD_msg_chan_started = false;
jcr->unlock();

{
std::unique_lock l(jcr->mutex_guard());

jcr->dir_impl->sd_msg_thread_done = true;
jcr->dir_impl->SD_msg_chan_started = false;
}

pthread_cond_broadcast(
&jcr->dir_impl->nextrun_ready); /* wakeup any waiting threads */
pthread_cond_broadcast(
Expand Down
9 changes: 5 additions & 4 deletions core/src/dird/ndmp_fhdb_common.cc
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2015-2023 Bareos GmbH & Co. KG
Copyright (C) 2015-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -41,9 +41,10 @@ extern "C" int BndmpFhdbAddFile(struct ndmlog* ixlog,
{
NIS* nis = (NIS*)ixlog->ctx;

nis->jcr->lock();
nis->jcr->JobFiles++;
nis->jcr->unlock();
{
std::unique_lock l(nis->jcr->mutex_guard());
nis->jcr->JobFiles++;
}

if (nis->save_filehist) {
int8_t FileType = 0;
Expand Down
7 changes: 4 additions & 3 deletions core/src/dird/ndmp_fhdb_lmdb.cc
Expand Up @@ -159,9 +159,10 @@ extern "C" int bndmp_fhdb_lmdb_add_node(struct ndmlog* ixlog,
NIS* nis;
nis = (NIS*)ixlog->ctx;

nis->jcr->lock();
nis->jcr->JobFiles++;
nis->jcr->unlock();
{
std::unique_lock l(nis->jcr->mutex_guard());
nis->jcr->JobFiles++;
}

if (nis->save_filehist) {
int result;
Expand Down
9 changes: 5 additions & 4 deletions core/src/dird/ndmp_fhdb_mem.cc
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2015-2015 Planets Communications B.V.
Copyright (C) 2015-2023 Bareos GmbH & Co. KG
Copyright (C) 2015-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -576,9 +576,10 @@ extern "C" int bndmp_fhdb_mem_add_node(struct ndmlog* ixlog,
{
NIS* nis = (NIS*)ixlog->ctx;

nis->jcr->lock();
nis->jcr->JobFiles++;
nis->jcr->unlock();
{
std::unique_lock l(nis->jcr->mutex_guard());
nis->jcr->JobFiles++;
}

if (nis->save_filehist) {
int attr_size;
Expand Down
11 changes: 6 additions & 5 deletions core/src/filed/backup.cc
Expand Up @@ -1597,11 +1597,12 @@ bool EncodeAndSendAttributes(JobControlRecord* jcr,
Dmsg3(300, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname,
attribs.c_str(), attribsEx);

jcr->lock();
jcr->JobFiles++; /* increment number of files sent */
ff_pkt->FileIndex = jcr->JobFiles; /* return FileIndex */
PmStrcpy(jcr->fd_impl->last_fname, ff_pkt->fname);
jcr->unlock();
{
std::unique_lock l(jcr->mutex_guard());
jcr->JobFiles++; /* increment number of files sent */
ff_pkt->FileIndex = jcr->JobFiles; /* return FileIndex */
PmStrcpy(jcr->fd_impl->last_fname, ff_pkt->fname);
}

// Debug code: check if we must hangup
if (hangup && (jcr->JobFiles > (uint32_t)hangup)) {
Expand Down
12 changes: 8 additions & 4 deletions core/src/filed/restore.cc
Expand Up @@ -587,10 +587,14 @@ void DoRestore(JobControlRecord* jcr)
if (status == CF_CORE) {
status = CreateFile(jcr, attr, &rctx.bfd, jcr->fd_impl->replace);
}
jcr->lock();
PmStrcpy(jcr->fd_impl->last_fname, attr->ofname);
jcr->fd_impl->last_type = attr->type;
jcr->unlock();

{
// NOTE: This should probably be a lock in jcr->fd_impl instead
std::unique_lock l(jcr->mutex_guard());
PmStrcpy(jcr->fd_impl->last_fname, attr->ofname);
jcr->fd_impl->last_type = attr->type;
}

Dmsg2(130, "Outfile=%s CreateFile status=%d\n", attr->ofname, status);
switch (status) {
case CF_ERROR:
Expand Down
18 changes: 10 additions & 8 deletions core/src/filed/status.cc
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2001-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
Expand Down Expand Up @@ -207,10 +207,11 @@ static void ListRunningJobsPlain(StatusPacket* sp)
edit_uint64_with_commas(njcr->fd_impl->num_files_examined, b1));
sp->send(msg, len);
if (njcr->JobFiles > 0) {
njcr->lock();
len = Mmsg(msg, T_(" Processing file: %s\n"),
njcr->fd_impl->last_fname);
njcr->unlock();
{
std::unique_lock l(njcr->mutex_guard());
len = Mmsg(msg, T_(" Processing file: %s\n"),
njcr->fd_impl->last_fname);
}
sp->send(msg, len);
}

Expand Down Expand Up @@ -278,9 +279,10 @@ static void ListRunningJobsApi(StatusPacket* sp)
edit_uint64(njcr->fd_impl->num_files_examined, b1));
sp->send(msg, len);
if (njcr->JobFiles > 0) {
njcr->lock();
len = Mmsg(msg, " Processing file=%s\n", njcr->fd_impl->last_fname);
njcr->unlock();
{
std::unique_lock l(njcr->mutex_guard());
len = Mmsg(msg, " Processing file=%s\n", njcr->fd_impl->last_fname);
}
sp->send(msg, len);
}

Expand Down
9 changes: 5 additions & 4 deletions core/src/filed/verify.cc
Expand Up @@ -199,10 +199,11 @@ static int VerifyFile(JobControlRecord* jcr, FindFilesPacket* ff_pkt, bool)
ff_pkt->LinkFI, 0);
encode_attribsEx(jcr, attribsEx.c_str(), ff_pkt);

jcr->lock();
jcr->JobFiles++; /* increment number of files sent */
PmStrcpy(jcr->fd_impl->last_fname, ff_pkt->fname);
jcr->unlock();
{
std::unique_lock l(jcr->mutex_guard());
jcr->JobFiles++; /* increment number of files sent */
PmStrcpy(jcr->fd_impl->last_fname, ff_pkt->fname);
}

/* Path needs to be stripped just like during the
* backup. The director catalog only knows about the
Expand Down
20 changes: 11 additions & 9 deletions core/src/filed/verify_vol.cc
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2002-2010 Free Software Foundation Europe e.V.
Copyright (C) 2016-2023 Bareos GmbH & Co. KG
Copyright (C) 2016-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -168,11 +168,13 @@ void DoVerifyVolume(JobControlRecord* jcr)
} else {
*lname = 0;
}
jcr->lock();
jcr->JobFiles++;
jcr->fd_impl->num_files_examined++;
PmStrcpy(jcr->fd_impl->last_fname, fname); /* last file examined */
jcr->unlock();

{
std::unique_lock l(jcr->mutex_guard());
jcr->JobFiles++;
jcr->fd_impl->num_files_examined++;
PmStrcpy(jcr->fd_impl->last_fname, fname); /* last file examined */
}

/* Send file attributes to Director
* File_index
Expand Down Expand Up @@ -258,11 +260,11 @@ void DoVerifyVolume(JobControlRecord* jcr)
dir->msg);
break;

case STREAM_RESTORE_OBJECT:
jcr->lock();
case STREAM_RESTORE_OBJECT: {
std::unique_lock l(jcr->mutex_guard());
jcr->JobFiles++;
jcr->fd_impl->num_files_examined++;
jcr->unlock();
}

Dmsg2(400, "send inx=%d STREAM_RESTORE_OBJECT-%d\n", jcr->JobFiles,
STREAM_RESTORE_OBJECT);
Expand Down
10 changes: 4 additions & 6 deletions core/src/include/jcr.h
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -77,7 +77,7 @@ typedef void(JCR_free_HANDLER)(JobControlRecord* jcr);
/* clang-format off */
class JobControlRecord {
private:
pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER; /**< Jcr mutex */
std::mutex mutex_; /**< Jcr mutex */
std::atomic<int32_t> use_count_{}; /**< Use count */
std::atomic<int32_t> JobStatus_{}; /**< ready, running, blocked, terminated */
int32_t JobType_{}; /**< Backup, restore, verify ... */
Expand Down Expand Up @@ -110,8 +110,8 @@ class JobControlRecord {
JobControlRecord& operator=(const JobControlRecord& other) = delete;
JobControlRecord& operator=(const JobControlRecord&& other) = delete;

void lock() { lock_mutex(mutex_); }
void unlock() { unlock_mutex(mutex_); }
[[nodiscard]] std::mutex& mutex_guard() { return mutex_; }

void IncUseCount(void)
{
++use_count_;
Expand All @@ -121,8 +121,6 @@ class JobControlRecord {
--use_count_;
}
int32_t UseCount() const { return use_count_; }
void InitMutex(void) { pthread_mutex_init(&mutex_, NULL); }
void DestroyMutex(void) { pthread_mutex_destroy(&mutex_); }
bool IsJobCanceled() { return JobStatus_ == JS_Canceled
|| JobStatus_ == JS_ErrorTerminated
|| JobStatus_ == JS_FatalError; }
Expand Down

0 comments on commit 385bc7a

Please sign in to comment.