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()> const& job)
40  : mType(type)
41  , mJobIndex(index)
42  , mJob(job)
43  , mName(name)
44  , m_queue_time(clock_type::now())
45 {
46  m_loadEvent = std::make_shared<LoadEvent>(std::ref(lm), name, false);
47 }
48 
49 JobType
50 Job::getType() const
51 {
52  return mType;
53 }
54 
55 Job::clock_type::time_point const&
57 {
58  return m_queue_time;
59 }
60 
61 void
63 {
65  m_loadEvent->start();
66  m_loadEvent->setName(mName);
67 
68  mJob();
69 
70  // Destroy the lambda, otherwise we won't include
71  // its duration in the time measurement
72  mJob = nullptr;
73 }
74 
75 bool
76 Job::operator>(const Job& j) const
77 {
78  if (mType < j.mType)
79  return true;
80 
81  if (mType > j.mType)
82  return false;
83 
84  return mJobIndex > j.mJobIndex;
85 }
86 
87 bool
88 Job::operator>=(const Job& j) const
89 {
90  if (mType < j.mType)
91  return true;
92 
93  if (mType > j.mType)
94  return false;
95 
96  return mJobIndex >= j.mJobIndex;
97 }
98 
99 bool
100 Job::operator<(const Job& j) const
101 {
102  if (mType < j.mType)
103  return false;
104 
105  if (mType > j.mType)
106  return true;
107 
108  return mJobIndex < j.mJobIndex;
109 }
110 
111 bool
112 Job::operator<=(const Job& j) const
113 {
114  if (mType < j.mType)
115  return false;
116 
117  if (mType > j.mType)
118  return true;
119 
120  return mJobIndex <= j.mJobIndex;
121 }
122 
123 } // namespace ripple
std::chrono::steady_clock
ripple::Job::mName
std::string mName
Definition: Job.h:149
std::string
STL class.
ripple::Job::mJob
std::function< void()> mJob
Definition: Job.h:147
ripple::Job::operator>=
bool operator>=(const Job &j) const
Definition: Job.cpp:88
ripple::Job::mJobIndex
std::uint64_t mJobIndex
Definition: Job.h:146
ripple::Job::queue_time
clock_type::time_point const & queue_time() const
Returns the time when the job was queued.
Definition: Job.cpp:56
std::function
ripple::Job::operator<=
bool operator<=(const Job &j) const
Definition: Job.cpp:112
ripple::Job::operator<
bool operator<(const Job &j) const
Definition: Job.cpp:100
ripple::Job::Job
Job()
Default constructor.
Definition: Job.cpp:26
ripple::jtINVALID
@ jtINVALID
Definition: Job.h:37
ripple::Job
Definition: Job.h:96
std::uint64_t
ripple::LoadMonitor
Definition: LoadMonitor.h:36
ripple::Job::mType
JobType mType
Definition: Job.h:145
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
cassert
ripple::JobType
JobType
Definition: Job.h:35
ripple::Job::getType
JobType getType() const
Definition: Job.cpp:50
ripple::Job::operator>
bool operator>(const Job &j) const
Definition: Job.cpp:76
ripple::Job::doJob
void doJob()
Definition: Job.cpp:62
ripple::Job::m_queue_time
clock_type::time_point m_queue_time
Definition: Job.h:150
ripple::Job::m_loadEvent
std::shared_ptr< LoadEvent > m_loadEvent
Definition: Job.h:148
std::ref
T ref(T... args)