Skip to content

Commit

Permalink
bsock: switch volatile to std::atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
alaaeddineelamri authored and arogge committed Nov 7, 2022
1 parent 74ff88e commit dedc7f8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
6 changes: 3 additions & 3 deletions core/src/lib/bsock.cc
Expand Up @@ -97,11 +97,11 @@ BareosSocket::BareosSocket(const BareosSocket& other)
in_msg_no = other.in_msg_no;
out_msg_no = other.out_msg_no;
message_length = other.message_length;
timer_start = other.timer_start;
timer_start = other.timer_start.load();
b_errno = other.b_errno;
blocking_ = other.blocking_;
errors = other.errors;
suppress_error_msgs_ = other.suppress_error_msgs_;
errors = other.errors.load();
suppress_error_msgs_ = other.suppress_error_msgs_.load();
sleep_time_after_authentication_error
= other.sleep_time_after_authentication_error;
client_addr = other.client_addr;
Expand Down
38 changes: 20 additions & 18 deletions core/src/lib/bsock.h
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
Copyright (C) 2016-2021 Bareos GmbH & Co. KG
Copyright (C) 2016-2022 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 @@ -37,16 +37,18 @@
#define BAREOS_LIB_BSOCK_H_

#include <include/bareos.h>
#include <mutex>
#include <functional>
#include <cassert>
#include "lib/address_conf.h"
#include "lib/bnet_network_dump.h"
#include "lib/tls.h"
#include "lib/s_password.h"
#include "lib/tls_conf.h"
#include "include/version_numbers.h"

#include <mutex>
#include <functional>
#include <cassert>
#include <atomic>

struct btimer_t; /* forward reference */
class BareosSocket;
class Tls;
Expand All @@ -62,20 +64,20 @@ class BareosSocket {
* bat breaks on some systems such as RedHat.
*/
public:
int fd_; /* Socket file descriptor */
uint64_t read_seqno; /* Read sequence number */
POOLMEM* msg; /* Message pool buffer */
POOLMEM* errmsg; /* Edited error message */
int spool_fd_; /* Spooling file */
IPADDR* src_addr; /* IP address to source connections from */
uint32_t in_msg_no; /* Input message number */
uint32_t out_msg_no; /* Output message number */
int32_t message_length; /* Message length */
volatile time_t timer_start; /* Time started read/write */
int b_errno; /* BareosSocket errno */
int blocking_; /* Blocking state (0 = nonblocking, 1 = blocking) */
volatile int errors; /* Incremented for each error on socket */
volatile bool suppress_error_msgs_; /* Set to suppress error messages */
int fd_; /* Socket file descriptor */
uint64_t read_seqno; /* Read sequence number */
POOLMEM* msg; /* Message pool buffer */
POOLMEM* errmsg; /* Edited error message */
int spool_fd_; /* Spooling file */
IPADDR* src_addr; /* IP address to source connections from */
uint32_t in_msg_no; /* Input message number */
uint32_t out_msg_no; /* Output message number */
int32_t message_length; /* Message length */
std::atomic<time_t> timer_start; /* Time started read/write */
int b_errno; /* BareosSocket errno */
int blocking_; /* Blocking state (0 = nonblocking, 1 = blocking) */
std::atomic<int> errors; /* Incremented for each error on socket */
std::atomic<bool> suppress_error_msgs_; /* Set to suppress error messages */
int sleep_time_after_authentication_error;

struct sockaddr client_addr; /* Client's IP address */
Expand Down
12 changes: 6 additions & 6 deletions core/src/lib/bsock_tcp.cc
Expand Up @@ -396,7 +396,7 @@ bool BareosSocketTCP::SendPacket(int32_t* hdr, int32_t pktsiz)
rc = write_nbytes((char*)hdr, pktsiz);
timer_start = 0; /* clear timer */
if (rc != pktsiz) {
errors++;
++errors;
if (errno == 0) {
b_errno = EIO;
} else {
Expand Down Expand Up @@ -451,7 +451,7 @@ bool BareosSocketTCP::send()
if (errors) {
if (!suppress_error_msgs_) {
Qmsg4(jcr_, M_ERROR, 0, _("Socket has errors=%d on call to %s:%s:%d\n"),
errors, who_, host_, port_);
errors.load(), who_, host_, port_);
}
return false;
}
Expand Down Expand Up @@ -544,13 +544,13 @@ int32_t BareosSocketTCP::recv()
} else {
b_errno = errno;
}
errors++;
++errors;
nbytes = BNET_HARDEOF; /* assume hard EOF received */
goto get_out;
}
timer_start = 0; /* clear timer */
if (nbytes != header_length) {
errors++;
++errors;
b_errno = EIO;
Qmsg5(jcr_, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
header_length, nbytes, who_, host_, port_);
Expand Down Expand Up @@ -600,7 +600,7 @@ int32_t BareosSocketTCP::recv()
} else {
b_errno = errno;
}
errors++;
++errors;
Qmsg4(jcr_, M_ERROR, 0, _("Read error from %s:%s:%d: ERR=%s\n"), who_,
host_, port_, this->bstrerror());
nbytes = BNET_ERROR;
Expand All @@ -611,7 +611,7 @@ int32_t BareosSocketTCP::recv()
message_length = nbytes;
if (nbytes != pktsiz) {
b_errno = EIO;
errors++;
++errors;
Qmsg5(jcr_, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
pktsiz, nbytes, who_, host_, port_);
nbytes = BNET_ERROR;
Expand Down
4 changes: 3 additions & 1 deletion core/src/lib/connection_pool.h
Expand Up @@ -28,6 +28,8 @@
#ifndef BAREOS_LIB_CONNECTION_POOL_H_
#define BAREOS_LIB_CONNECTION_POOL_H_

#include <atomic>

template <typename T> class alist;
class BareosSocket;

Expand Down Expand Up @@ -57,7 +59,7 @@ class Connection {
char name_[MAX_NAME_LENGTH];
int protocol_version_;
bool authenticated_;
volatile bool in_use_;
std::atomic<bool> in_use_;
time_t connect_time_;
pthread_mutex_t mutex_;
};
Expand Down

0 comments on commit dedc7f8

Please sign in to comment.