Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Commit

Permalink
Added graceful shut down for storage thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
zit-hb committed Apr 8, 2015
1 parent 9c476ec commit 07c7bff
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(SHADOWD)
# Version
set(SHADOWD_MAJOR_VERSION 1)
set(SHADOWD_MINOR_VERSION 1)
set(SHADOWD_PATCH_VERSION 0)
set(SHADOWD_PATCH_VERSION 1)
set(SHADOWD_VERSION
${SHADOWD_MAJOR_VERSION}.${SHADOWD_MINOR_VERSION}.${SHADOWD_PATCH_VERSION})

Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "Shadow Daemon"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = "1.1.0"
PROJECT_NUMBER = "1.1.1"

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
15 changes: 15 additions & 0 deletions include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,21 @@ namespace swd {
class storage :
public swd::singleton<swd::storage> {
public:
/**
* @brief Initialize the storage object.
*/
storage();

/**
* @brief Start insert thread.
*/
void start();

/**
* @brief Gracefully stop process_next.
*/
void stop();

/**
* @brief Add request to insert queue.
*
Expand Down Expand Up @@ -85,6 +95,11 @@ namespace swd {
* @brief Thread that constantly checks queue for new entries.
*/
boost::thread worker_thread_;

/**
* @brief Switch to exit process_next loop.
*/
bool stop_;
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <boost/thread/thread.hpp>

#include "server.h"
#include "storage.h"
#include "config.h"
#include "log.h"
#include "shared.h"
Expand Down Expand Up @@ -174,5 +175,9 @@ void swd::server::handle_accept(const boost::system::error_code& e) {
}

void swd::server::handle_stop() {
/* Stop the threads of asio. */
io_service_.stop();

/* Also stop the storage thread. */
swd::storage::i()->stop();
}
10 changes: 9 additions & 1 deletion src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@
#include "database.h"
#include "log.h"

swd::storage::storage() :
stop_(false) {
}

void swd::storage::start() {
worker_thread_ = boost::thread(
boost::bind(&swd::storage::process_next, this)
);
}

void swd::storage::stop() {
stop_ = true;
}

void swd::storage::add(swd::request_ptr request) {
/* Mutex to avoid race conditions. */
boost::unique_lock<boost::mutex> scoped_lock(queue_mutex_);
Expand All @@ -51,7 +59,7 @@ void swd::storage::add(swd::request_ptr request) {
}

void swd::storage::process_next() {
while (1) {
while (!stop_) {
queue_mutex_.lock();

if (!queue_.empty()) {
Expand Down

0 comments on commit 07c7bff

Please sign in to comment.