rippled
CountedObject.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/basics/CountedObject.h>
21 #include <type_traits>
22 
23 namespace ripple {
24 
25 CountedObjects&
27 {
28  static CountedObjects instance;
29 
30  return instance;
31 }
32 
33 CountedObjects::CountedObjects() noexcept : m_count(0), m_head(nullptr)
34 {
35 }
36 
38 CountedObjects::getCounts(int minimumThreshold) const
39 {
40  List counts;
41 
42  // When other operations are concurrent, the count
43  // might be temporarily less than the actual count.
44  int const count = m_count.load();
45 
46  counts.reserve(count);
47 
48  CounterBase* counter = m_head.load();
49 
50  while (counter != nullptr)
51  {
52  if (counter->getCount() >= minimumThreshold)
53  {
54  Entry entry;
55 
56  entry.first = counter->getName();
57  entry.second = counter->getCount();
58 
59  counts.push_back(entry);
60  }
61 
62  counter = counter->getNext();
63  }
64 
65  return counts;
66 }
67 
68 //------------------------------------------------------------------------------
69 
71 {
72  // Insert ourselves at the front of the lock-free linked list
73 
75  CounterBase* head;
76 
77  do
78  {
79  head = instance.m_head.load();
80  m_next = head;
81  } while (instance.m_head.exchange(this) != head);
82 
83  ++instance.m_count;
84 }
85 
87 {
88  // VFALCO NOTE If the counters are destroyed before the singleton,
89  // undefined behavior will result if the singleton's member
90  // functions are called.
91 }
92 
93 } // namespace ripple
ripple::CountedObjects::m_count
std::atomic< int > m_count
Definition: CountedObject.h:96
std::pair
std::vector::reserve
T reserve(T... args)
std::vector
STL class.
ripple::CountedObjects::CounterBase::getNext
CounterBase * getNext() const noexcept
Definition: CountedObject.h:74
ripple::CountedObjects::CountedObjects
CountedObjects() noexcept
Definition: CountedObject.cpp:33
ripple::CountedObjects::CounterBase::m_next
CounterBase * m_next
Definition: CountedObject.h:88
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:26
ripple::CountedObjects::CounterBase::getCount
int getCount() const noexcept
Definition: CountedObject.h:68
std::vector::push_back
T push_back(T... args)
std::atomic::load
T load(T... args)
ripple::CountedObjects::CounterBase::~CounterBase
virtual ~CounterBase() noexcept
Definition: CountedObject.cpp:86
ripple::CountedObjects
Manages all counted object types.
Definition: CountedObject.h:31
ripple::CountedObjects::CounterBase
Implementation for CountedObject.
Definition: CountedObject.h:48
ripple::CountedObjects::CounterBase::getName
virtual char const * getName() const =0
ripple::CountedObjects::m_head
std::atomic< CounterBase * > m_head
Definition: CountedObject.h:97
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:38
ripple::CountedObjects::CounterBase::CounterBase
CounterBase() noexcept
Definition: CountedObject.cpp:70
type_traits