forked from amjuarez/bytecoin
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ducknote
committed
Aug 12, 2014
1 parent
b4a27b9
commit 5ba1c9f
Showing
44 changed files
with
4,619 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) 2012-2013 The Cryptonote developers | ||
// Distributed under the MIT/X11 software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#pragma once | ||
|
||
#include <mutex> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
namespace tools { | ||
|
||
template<typename T> | ||
class ObserverManager { | ||
public: | ||
bool add(T* observer) { | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
auto it = std::find(m_observers.begin(), m_observers.end(), observer); | ||
if (m_observers.end() == it) { | ||
m_observers.push_back(observer); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
bool remove(T* observer) { | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
auto it = std::find(m_observers.begin(), m_observers.end(), observer); | ||
if (m_observers.end() == it) { | ||
return false; | ||
} else { | ||
m_observers.erase(it); | ||
return true; | ||
} | ||
} | ||
|
||
void clear() { | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
m_observers.clear(); | ||
} | ||
|
||
#if defined(_MSC_VER) | ||
template<typename F> | ||
void notify(F notification) { | ||
std::vector<T*> observersCopy; | ||
{ | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
observersCopy = m_observers; | ||
} | ||
|
||
for (T* observer : observersCopy) { | ||
(observer->*notification)(); | ||
} | ||
} | ||
|
||
template<typename F, typename Arg0> | ||
void notify(F notification, const Arg0& arg0) { | ||
std::vector<T*> observersCopy; | ||
{ | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
observersCopy = m_observers; | ||
} | ||
|
||
for (T* observer : observersCopy) { | ||
(observer->*notification)(arg0); | ||
} | ||
} | ||
|
||
template<typename F, typename Arg0, typename Arg1> | ||
void notify(F notification, const Arg0& arg0, const Arg1& arg1) { | ||
std::vector<T*> observersCopy; | ||
{ | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
observersCopy = m_observers; | ||
} | ||
|
||
for (T* observer : observersCopy) { | ||
(observer->*notification)(arg0, arg1); | ||
} | ||
} | ||
|
||
template<typename F, typename Arg0, typename Arg1, typename Arg2> | ||
void notify(F notification, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2) { | ||
std::vector<T*> observersCopy; | ||
{ | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
observersCopy = m_observers; | ||
} | ||
|
||
for (T* observer : observersCopy) { | ||
(observer->*notification)(arg0, arg1, arg2); | ||
} | ||
} | ||
|
||
#else | ||
|
||
template<typename F, typename... Args> | ||
void notify(F notification, Args... args) { | ||
std::vector<T*> observersCopy; | ||
{ | ||
std::unique_lock<std::mutex> lock(m_observersMutex); | ||
observersCopy = m_observers; | ||
} | ||
|
||
for (T* observer : observersCopy) { | ||
(observer->*notification)(args...); | ||
} | ||
} | ||
#endif | ||
|
||
private: | ||
std::vector<T*> m_observers; | ||
std::mutex m_observersMutex; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) 2012-2013 The Cryptonote developers | ||
// Distributed under the MIT/X11 software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
|
||
#include "include_base_utils.h" | ||
|
||
namespace tools { | ||
|
||
class InitState { | ||
public: | ||
InitState() : m_state(STATE_NOT_INITIALIZED) { | ||
} | ||
|
||
bool initialized() const volatile { | ||
return STATE_INITIALIZED == m_state.load(std::memory_order_acquire); | ||
} | ||
|
||
bool beginInit() volatile { | ||
State state = STATE_NOT_INITIALIZED; | ||
if (!m_state.compare_exchange_strong(state, STATE_INITIALIZING, std::memory_order_seq_cst)) { | ||
LOG_ERROR("object has been already initialized"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool endInit() volatile { | ||
State expectedState = STATE_INITIALIZING; | ||
if (!m_state.compare_exchange_strong(expectedState, STATE_INITIALIZED, std::memory_order_seq_cst)) { | ||
LOG_ERROR("Unexpected state: " << expectedState); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool beginShutdown() volatile { | ||
while (true) { | ||
State state = m_state.load(std::memory_order_relaxed); | ||
if (STATE_NOT_INITIALIZED == state) { | ||
return true; | ||
} else if (STATE_INITIALIZING == state) { | ||
LOG_ERROR("Object is being initialized"); | ||
return false; | ||
} else if (STATE_INITIALIZED == state) { | ||
if (m_state.compare_exchange_strong(state, STATE_SHUTTING_DOWN, std::memory_order_seq_cst)) { | ||
return true; | ||
} | ||
} else if (STATE_SHUTTING_DOWN == state) { | ||
LOG_ERROR("Object is being shutting down"); | ||
return false; | ||
} else { | ||
LOG_ERROR("Unknown state " << state); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
bool endShutdown() volatile { | ||
State expectedState = STATE_SHUTTING_DOWN; | ||
if (!m_state.compare_exchange_strong(expectedState, STATE_NOT_INITIALIZED, std::memory_order_seq_cst)) { | ||
LOG_ERROR("Unexpected state: " << expectedState); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private: | ||
enum State { | ||
STATE_NOT_INITIALIZED, | ||
STATE_INITIALIZING, | ||
STATE_INITIALIZED, | ||
STATE_SHUTTING_DOWN | ||
}; | ||
|
||
private: | ||
std::atomic<State> m_state; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) 2012-2013 The Cryptonote developers | ||
// Distributed under the MIT/X11 software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "NodeErrors.h" | ||
|
||
namespace cryptonote { | ||
namespace error { | ||
|
||
NodeErrorCategory NodeErrorCategory::INSTANCE; | ||
|
||
} | ||
} |
Oops, something went wrong.