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/core/Job.h>
21 #include <ripple/beast/core/CurrentThreadName.h>
22 #include <cassert>
23 
24 namespace ripple {
25 
27  : mType (jtINVALID)
28  , mJobIndex (0)
29 {
30 }
31 
33  : mType (type)
34  , mJobIndex (index)
35 {
36 }
37 
39  std::string const& name,
40  std::uint64_t index,
41  LoadMonitor& lm,
42  std::function <void (Job&)> const& job,
43  CancelCallback cancelCallback)
44  : m_cancelCallback (cancelCallback)
45  , mType (type)
46  , mJobIndex (index)
47  , mJob (job)
48  , mName (name)
49  , m_queue_time (clock_type::now ())
50 {
51  m_loadEvent = std::make_shared <LoadEvent> (std::ref (lm), name, false);
52 }
53 
55 {
56  return mType;
57 }
58 
60 {
61  assert (m_cancelCallback);
62  return m_cancelCallback;
63 }
64 
65 Job::clock_type::time_point const& Job::queue_time () const
66 {
67  return m_queue_time;
68 }
69 
70 bool Job::shouldCancel () const
71 {
72  if (m_cancelCallback)
73  return m_cancelCallback ();
74  return false;
75 }
76 
77 void Job::doJob ()
78 {
79  beast::setCurrentThreadName ("doJob: " + mName);
80  m_loadEvent->start ();
81  m_loadEvent->setName (mName);
82 
83  mJob (*this);
84 
85  // Destroy the lambda, otherwise we won't include
86  // its duration in the time measurement
87  mJob = nullptr;
88 }
89 
90 void Job::rename (std::string const& newName)
91 {
92  mName = newName;
93 }
94 
95 bool Job::operator> (const Job& j) const
96 {
97  if (mType < j.mType)
98  return true;
99 
100  if (mType > j.mType)
101  return false;
102 
103  return mJobIndex > j.mJobIndex;
104 }
105 
106 bool Job::operator>= (const Job& j) const
107 {
108  if (mType < j.mType)
109  return true;
110 
111  if (mType > j.mType)
112  return false;
113 
114  return mJobIndex >= j.mJobIndex;
115 }
116 
117 bool Job::operator< (const Job& j) const
118 {
119  if (mType < j.mType)
120  return false;
121 
122  if (mType > j.mType)
123  return true;
124 
125  return mJobIndex < j.mJobIndex;
126 }
127 
128 bool Job::operator<= (const Job& j) const
129 {
130  if (mType < j.mType)
131  return false;
132 
133  if (mType > j.mType)
134  return true;
135 
136  return mJobIndex <= j.mJobIndex;
137 }
138 
139 }
std::chrono::steady_clock
ripple::Job::mJob
std::function< void(Job &)> mJob
Definition: Job.h:143
ripple::Job::mName
std::string mName
Definition: Job.h:145
std::string
STL class.
ripple::Job::operator>=
bool operator>=(const Job &j) const
Definition: Job.cpp:106
ripple::Job::mJobIndex
std::uint64_t mJobIndex
Definition: Job.h:142
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:128
ripple::Job::operator<
bool operator<(const Job &j) const
Definition: Job.cpp:117
ripple::Job::getCancelCallback
CancelCallback getCancelCallback() const
Definition: Job.cpp:59
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:70
ripple::jtINVALID
@ jtINVALID
Definition: Job.h:36
ripple::Job::m_cancelCallback
CancelCallback m_cancelCallback
Definition: Job.h:140
ripple::Job
Definition: Job.h:83
std::uint64_t
ripple::LoadMonitor
Definition: LoadMonitor.h:35
ripple::Job::mType
JobType mType
Definition: Job.h:141
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Definition: CurrentThreadName.cpp:113
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:90
cassert
ripple::JobType
JobType
Definition: Job.h:33
ripple::Job::getType
JobType getType() const
Definition: Job.cpp:54
ripple::Job::operator>
bool operator>(const Job &j) const
Definition: Job.cpp:95
ripple::Job::doJob
void doJob()
Definition: Job.cpp:77
ripple::Job::m_queue_time
clock_type::time_point m_queue_time
Definition: Job.h:146
ripple::Job::m_loadEvent
std::shared_ptr< LoadEvent > m_loadEvent
Definition: Job.h:144
std::ref
T ref(T... args)