rippled
TaskQueue.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2019 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/nodestore/impl/TaskQueue.h>
21 
22 #include <cassert>
23 
24 namespace ripple {
25 namespace NodeStore {
26 
28  : Stoppable("TaskQueue", parent)
29  , workers_(*this, nullptr, "Shard store taskQueue", 1)
30 {
31 }
32 
33 void
35 {
37  stopped();
38 }
39 
40 void
42 {
43  std::lock_guard lock{mutex_};
44 
45  tasks_.emplace(std::move(task));
46  workers_.addTask();
47 }
48 
49 void
51 {
52  std::function<void()> task;
53 
54  {
55  std::lock_guard lock{mutex_};
56  assert(!tasks_.empty());
57 
58  task = std::move(tasks_.front());
59  tasks_.pop();
60  }
61 
62  task();
63 }
64 
65 } // namespace NodeStore
66 } // namespace ripple
ripple::Stoppable::stopped
void stopped()
Called by derived classes to indicate that the stoppable has stopped.
Definition: Stoppable.cpp:72
std::queue::emplace
T emplace(T... args)
ripple::NodeStore::TaskQueue::workers_
Workers workers_
Definition: TaskQueue.h:49
std::lock_guard
STL class.
ripple::NodeStore::TaskQueue::processTask
void processTask(int instance) override
Perform a task.
Definition: TaskQueue.cpp:50
std::function
ripple::NodeStore::TaskQueue::tasks_
std::queue< std::function< void()> > tasks_
Definition: TaskQueue.h:50
std::queue::front
T front(T... args)
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::NodeStore::TaskQueue::addTask
void addTask(std::function< void()> task)
Adds a task to the queue.
Definition: TaskQueue.cpp:41
std::queue::pop
T pop(T... args)
ripple::Workers::pauseAllThreadsAndWait
void pauseAllThreadsAndWait()
Pause all threads and wait until they are paused.
Definition: Workers.cpp:114
ripple::NodeStore::TaskQueue::TaskQueue
TaskQueue(Stoppable &parent)
Definition: TaskQueue.cpp:27
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::TaskQueue::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: TaskQueue.cpp:34
cassert
ripple::Workers::addTask
void addTask()
Add a task to be performed.
Definition: Workers.cpp:126
std::queue::empty
T empty(T... args)
ripple::NodeStore::TaskQueue::mutex_
std::mutex mutex_
Definition: TaskQueue.h:48