Skip to content

Commit

Permalink
bnet-thread-server: reworked initialization of atomic for server state
Browse files Browse the repository at this point in the history
- changed enum into enum class BnetServerState
- removed atomic_init due to linker problems on ubuntu
  • Loading branch information
franku committed Nov 18, 2018
1 parent 35d2509 commit f82d48e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
7 changes: 3 additions & 4 deletions core/src/dird/socket_server.cc
Expand Up @@ -50,7 +50,7 @@ static alist *sock_fds = NULL;
static pthread_t tcp_server_tid;
static ConnectionPool *client_connections = NULL;

static std::atomic<int> server_state;
static std::atomic<BnetServerState> server_state(BnetServerState::kUndefined);

struct s_addr_port {
char *addr;
Expand Down Expand Up @@ -146,7 +146,6 @@ bool StartSocketServer(dlist *addrs)

if (client_connections == nullptr) { client_connections = New(ConnectionPool()); }

std::atomic_init(&server_state, (int)kUndefined);
if ((status = pthread_create(&tcp_server_tid, nullptr, connect_thread, (void *)myaddrs)) != 0) {
BErrNo be;
Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.bstrerror(status));
Expand All @@ -156,12 +155,12 @@ bool StartSocketServer(dlist *addrs)
int wait_ms = 100;
do {
Bmicrosleep(0, wait_ms * 1000);
if (server_state.load() == kStarted) {
if (server_state.load() == BnetServerState::kStarted) {
break;
}
} while (--tries);

if (server_state != kStarted) {
if (server_state != BnetServerState::kStarted) {
if (client_connections) {
delete (client_connections);
client_connections = nullptr;
Expand Down
12 changes: 6 additions & 6 deletions core/src/lib/bnet_server_tcp.cc
Expand Up @@ -146,7 +146,7 @@ void BnetThreadServerTcp(dlist *addr_list,
void *HandleConnectionRequest(ConfigurationParser *config,
void *bsock),
ConfigurationParser *config,
std::atomic<int> *const server_state)
std::atomic<BnetServerState> *const server_state)
{
int newsockfd, status;
socklen_t clilen;
Expand All @@ -169,7 +169,7 @@ void BnetThreadServerTcp(dlist *addr_list,

BNetThreadServerCleanupObject cleanup_object(sockfds, client_wq);

if (server_state) { server_state->store(kStarting); }
if (server_state) { server_state->store(BnetServerState::kStarting); }

/*
* Remove any duplicate addresses.
Expand Down Expand Up @@ -254,7 +254,7 @@ void BnetThreadServerTcp(dlist *addr_list,
BErrNo be;
Emsg2(M_ERROR, 0, _("Cannot bind port %d: ERR=%s.\n"), ntohs(fd_ptr->port),
be.bstrerror());
if (server_state) { server_state->store(kError); }
if (server_state) { server_state->store(BnetServerState::kError); }
return;
}

Expand Down Expand Up @@ -301,7 +301,7 @@ void BnetThreadServerTcp(dlist *addr_list,
}
#endif

if (server_state) { server_state->store(kStarted); }
if (server_state) { server_state->store(BnetServerState::kStarted); }

while (!quit) {
#ifndef HAVE_POLL
Expand All @@ -322,7 +322,7 @@ void BnetThreadServerTcp(dlist *addr_list,
if (errno == EINTR) {
continue;
}
if(server_state) { server_state->store(kError); }
if(server_state) { server_state->store(BnetServerState::kError); }
Emsg1(M_FATAL, 0, _("Error in select: %s\n"), be.bstrerror());
break;
}
Expand Down Expand Up @@ -413,5 +413,5 @@ void BnetThreadServerTcp(dlist *addr_list,
}
}
}
if(server_state) { server_state->store(kEnded); }
if(server_state) { server_state->store(BnetServerState::kEnded); }
}
6 changes: 3 additions & 3 deletions core/src/lib/bnet_server_tcp.h
Expand Up @@ -25,13 +25,13 @@

class ConfigurationParser;

typedef enum {
enum class BnetServerState {
kUndefined = 0,
kStarting,
kError,
kStarted,
kEnded
}BnetServerState;
};

void BnetThreadServerTcp(dlist *addr_list,
int max_clients,
Expand All @@ -41,7 +41,7 @@ void BnetThreadServerTcp(dlist *addr_list,
void *HandleConnectionRequest(ConfigurationParser *config,
void *bsock),
ConfigurationParser *config,
std::atomic<int> * const server_state = nullptr);
std::atomic<BnetServerState> * const server_state = nullptr);
void BnetStopAndWaitForThreadServerTcp(pthread_t tid);

#endif // BAREOS_LIB_BNET_SEVER_TCP_H_

0 comments on commit f82d48e

Please sign in to comment.