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

Commit

Permalink
Redone the storage queue fix, this time without deadlocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
zit-hb committed May 14, 2015
1 parent b70fa7d commit 4853211
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ namespace swd {
* @brief Notify consumer threads on new requests in the queue.
*/
boost::condition_variable cond_;

/**
* @brief Mutex required for condition variable.
*/
boost::mutex consumer_mutex_;
};
}

Expand Down
34 changes: 24 additions & 10 deletions src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,35 @@ void swd::storage::add(swd::request_ptr request) {
}

void swd::storage::process_next() {
boost::unique_lock<boost::mutex> scoped_lock(queue_mutex_);
boost::unique_lock<boost::mutex> consumer_lock(consumer_mutex_);

while (!stop_) {
/* Wait for new request in the queue. */
cond_.wait(scoped_lock);
/* Wait for a new request in the queue. */
while (1) {
/* Do not wait if there are still elements in the queue. */
{
boost::unique_lock<boost::mutex> queue_lock(queue_mutex_);

if (!queue_.empty()) {
break;
}
}

cond_.wait(consumer_lock);
}

/* Save all queued requests. */
while (!queue_.empty()) {
/* Remove oldest request from queue. */
swd::request_ptr request = queue_.front();
queue_.pop();
/* Move oldest element of queue to request. */
swd::request_ptr request;

/* Save request in the database. */
this->save(request);
{
boost::unique_lock<boost::mutex> queue_lock(queue_mutex_);

request = queue_.front();
queue_.pop();
}

/* Saving is the time-consuming part, do outside of mutex. */
this->save(request);
}
}

Expand Down

0 comments on commit 4853211

Please sign in to comment.