rippled
ripple
app
ledger
LedgerHolder.h
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
#ifndef RIPPLE_APP_LEDGER_LEDGERHOLDER_H_INCLUDED
21
#define RIPPLE_APP_LEDGER_LEDGERHOLDER_H_INCLUDED
22
23
#include <ripple/basics/CountedObject.h>
24
#include <ripple/basics/contract.h>
25
#include <
mutex
>
26
27
namespace
ripple
{
28
29
// Can std::atomic<std::shared_ptr>> make this lock free?
30
31
// VFALCO NOTE This class can be replaced with atomic<shared_ptr<...>>
32
39
class
LedgerHolder
:
public
CountedObject
<LedgerHolder>
40
{
41
public
:
42
// Update the held ledger
43
void
44
set
(
std::shared_ptr<Ledger const>
ledger)
45
{
46
if
(!ledger)
47
LogicError
(
"LedgerHolder::set with nullptr"
);
48
if
(!ledger->isImmutable())
49
LogicError
(
"LedgerHolder::set with mutable Ledger"
);
50
std::lock_guard
sl(
m_lock
);
51
m_heldLedger
= std::move(ledger);
52
}
53
54
// Return the (immutable) held ledger
55
std::shared_ptr<Ledger const>
56
get
()
57
{
58
std::lock_guard
sl(
m_lock
);
59
return
m_heldLedger
;
60
}
61
62
bool
63
empty
()
64
{
65
std::lock_guard
sl(
m_lock
);
66
return
m_heldLedger
==
nullptr
;
67
}
68
69
private
:
70
std::mutex
m_lock
;
71
std::shared_ptr<Ledger const>
m_heldLedger
;
72
};
73
74
}
// namespace ripple
75
76
#endif
ripple::CountedObject
Tracks the number of instances of an object.
Definition:
CountedObject.h:124
std::shared_ptr
STL class.
ripple::LedgerHolder::set
void set(std::shared_ptr< Ledger const > ledger)
Definition:
LedgerHolder.h:44
std::lock_guard
STL class.
ripple::LedgerHolder::m_lock
std::mutex m_lock
Definition:
LedgerHolder.h:70
ripple::LedgerHolder::empty
bool empty()
Definition:
LedgerHolder.h:63
ripple::LedgerHolder::get
std::shared_ptr< Ledger const > get()
Definition:
LedgerHolder.h:56
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition:
contract.cpp:48
ripple::LedgerHolder
Hold a ledger in a thread-safe way.
Definition:
LedgerHolder.h:39
mutex
ripple::LedgerHolder::m_heldLedger
std::shared_ptr< Ledger const > m_heldLedger
Definition:
LedgerHolder.h:71
Generated by
1.8.17