rippled
Job.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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/beast/core/CurrentThreadName.h>
21 #include <ripple/core/Job.h>
22 #include <cassert>
23 
24 namespace ripple {
25 
26 Job::Job() : mType(jtINVALID), mJobIndex(0)
27 {
28 }
29 
30 Job::Job(JobType type, std::uint64_t index) : mType(type), mJobIndex(index)
31 {
32 }
33 
35  JobType type,
36  std::string const& name,
37  std::uint64_t index,
38  LoadMonitor& lm,
39  std::function<void(Job&)> const& job,
40  CancelCallback cancelCallback)
41  : m_cancelCallback(cancelCallback)
42  , mType(type)
43  , mJobIndex(index)
44  , mJob(job)
45  , mName(name)
46  , m_queue_time(clock_type::now())
47 {
48  m_loadEvent = std::make_shared<LoadEvent>(std::ref(lm), name, false);
49 }
50 
51 JobType
52 Job::getType() const
53 {
54  return mType;
55 }
56 
59 {
60  assert(m_cancelCallback);
61  return m_cancelCallback;
62 }
63 
64 Job::clock_type::time_point const&
66 {
67  return m_queue_time;
68 }
69 
70 bool
72 {
73  if (m_cancelCallback)
74  return m_cancelCallback();
75  return false;
76 }
77 
78 void
80 {
82  m_loadEvent->start();
83  m_loadEvent->setName(mName);
84 
85  mJob(*this);
86 
87  // Destroy the lambda, otherwise we won't include
88  // its duration in the time measurement
89  mJob = nullptr;
90 }
91 
92 void
93 Job::rename(std::string const& newName)
94 {
95  mName = newName;
96 }
97 
98 bool
99 Job::operator>(const Job& j) const
100 {
101  if (mType < j.mType)
102  return true;
103 
104  if (mType > j.mType)
105  return false;
106 
107  return mJobIndex > j.mJobIndex;
108 }
109 
110 bool
111 Job::operator>=(const Job& j) const
112 {
113  if (mType < j.mType)
114  return true;
115 
116  if (mType > j.mType)
117  return false;
118 
119  return mJobIndex >= j.mJobIndex;
120 }
121 
122 bool
123 Job::operator<(const Job& j) const
124 {
125  if (mType < j.mType)
126  return false;
127 
128  if (mType > j.mType)
129  return true;
130 
131  return mJobIndex < j.mJobIndex;
132 }
133 
134 bool
135 Job::operator<=(const Job& j) const
136 {
137  if (mType < j.mType)
138  return false;
139 
140  if (mType > j.mType)
141  return true;
142 
143  return mJobIndex <= j.mJobIndex;
144 }
145 
146 } // namespace ripple
std::chrono::steady_clock
ripple::Job::mName
std::string mName
Definition: Job.h:154
std::string
STL class.
ripple::Job::operator>=
bool operator>=(const Job &j) const
Definition: Job.cpp:111
ripple::Job::mJobIndex
std::uint64_t mJobIndex
Definition: Job.h:151
ripple::Job::queue_time
clock_type::time_point const & queue_time() const
Returns the time when the job was queued.
Definition: Job.cpp:65
std::function
ripple::Job::operator<=
bool operator<=(const Job &j) const
Definition: Job.cpp:135
ripple::Job::operator<
bool operator<(const Job &j) const
Definition: Job.cpp:123
ripple::Job::getCancelCallback
CancelCallback getCancelCallback() const
Definition: Job.cpp:58
ripple::Job::mJob
std::function< void(Job &)> mJob
Definition: Job.h:152
ripple::Job::Job
Job()
Default constructor.
Definition: Job.cpp:26
ripple::Job::shouldCancel
bool shouldCancel() const
Returns true if the running job should make a best-effort cancel.
Definition: Job.cpp:71
ripple::jtINVALID
@ jtINVALID
Definition: Job.h:35
ripple::Job::m_cancelCallback
CancelCallback m_cancelCallback
Definition: Job.h:149
ripple::Job
Definition: Job.h:82
std::uint64_t
ripple::LoadMonitor
Definition: LoadMonitor.h:36
ripple::Job::mType
JobType mType
Definition: Job.h:150
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Definition: CurrentThreadName.cpp:119
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Job::rename
void rename(std::string const &n)
Definition: Job.cpp:93
cassert
ripple::JobType
JobType
Definition: Job.h:33
ripple::Job::getType
JobType getType() const
Definition: Job.cpp:52
ripple::Job::operator>
bool operator>(const Job &j) const
Definition: Job.cpp:99
ripple::Job::doJob
void doJob()
Definition: Job.cpp:79
ripple::Job::m_queue_time
clock_type::time_point m_queue_time
Definition: Job.h:155
ripple::Job::m_loadEvent
std::shared_ptr< LoadEvent > m_loadEvent
Definition: Job.h:153
std::ref
T ref(T... args)