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 
27 TaskQueue::TaskQueue() : workers_(*this, nullptr, "Shard store taskQueue", 1)
28 {
29 }
30 
31 void
33 {
34  workers_.stop();
35 }
36 
37 void
39 {
40  {
41  std::lock_guard lock{mutex_};
42  tasks_.emplace(std::move(task));
43  }
44  workers_.addTask();
45 }
46 
47 size_t
49 {
50  std::lock_guard lock{mutex_};
51  return tasks_.size() + processing_;
52 }
53 
54 void
56 {
57  std::function<void()> task;
58 
59  {
60  std::lock_guard lock{mutex_};
61 
62  assert(!tasks_.empty());
63  task = std::move(tasks_.front());
64  tasks_.pop();
65 
66  ++processing_;
67  }
68 
69  task();
70 
71  std::lock_guard lock{mutex_};
72  --processing_;
73 }
74 
75 } // namespace NodeStore
76 } // namespace ripple
ripple::Workers::stop
void stop()
Pause all threads and wait until they are paused.
Definition: Workers.cpp:114
ripple::NodeStore::TaskQueue::size
size_t size() const
Return the queue size.
Definition: TaskQueue.cpp:48
std::queue::size
T size(T... args)
std::queue::emplace
T emplace(T... args)
ripple::NodeStore::TaskQueue::workers_
Workers workers_
Definition: TaskQueue.h:53
std::lock_guard
STL class.
ripple::NodeStore::TaskQueue::processTask
void processTask(int instance) override
Perform a task.
Definition: TaskQueue.cpp:55
ripple::NodeStore::TaskQueue::TaskQueue
TaskQueue()
Definition: TaskQueue.cpp:27
std::function
ripple::NodeStore::TaskQueue::tasks_
std::queue< std::function< void()> > tasks_
Definition: TaskQueue.h:54
std::queue::front
T front(T... args)
ripple::NodeStore::TaskQueue::addTask
void addTask(std::function< void()> task)
Adds a task to the queue.
Definition: TaskQueue.cpp:38
std::queue::pop
T pop(T... args)
ripple::NodeStore::TaskQueue::stop
void stop()
Definition: TaskQueue.cpp:32
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
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:52
ripple::NodeStore::TaskQueue::processing_
std::uint64_t processing_
Definition: TaskQueue.h:55