public bool pop(ref T v) { std::unique_lock <object> lk = new std::unique_lock <object>(m_mutex); while (m_queue.empty()) { if (m_closed) { // all data has been processed, queue is closed return(false); } m_haveData.wait(lk); } v = std::move(m_queue.front()); m_queue.pop_front(); // we can have several waiting threads to unblock if (m_closed && m_queue.empty()) { m_haveSpace.notify_all(); } else { m_haveSpace.notify_one(); } return(true); }
//returns true if contexts are finished before timeout public void waitAsyncContextsFinish() { std::unique_lock <object> @lock = new std::unique_lock <object>(m_mutex); while (m_asyncContexts > 0) { m_cv.wait(@lock); } }
public void delAsyncContext() { std::unique_lock <object> @lock = new std::unique_lock <object>(m_mutex); m_asyncContexts--; if (m_asyncContexts == 0) { m_cv.notify_one(); } }
//C++ TO C# CONVERTER TODO TASK: The original C++ template specifier was replaced with a C# generic specifier, which may not produce the same behavior: //ORIGINAL LINE: template<typename F, typename Arg0, typename Arg1> public void notify <F, Arg0, Arg1>(F notification, Arg0 arg0, Arg1 arg1) { List <T> observersCopy = new List <T>(); { std::unique_lock <object> @lock = new std::unique_lock <object>(m_observersMutex); observersCopy = new List <T>(m_observers); } foreach (T observer in observersCopy) { notification(arg0, arg1); } }
public bool add(T observer) { std::unique_lock <object> @lock = new std::unique_lock <object>(m_observersMutex); var it = std::find(m_observers.GetEnumerator(), m_observers.end(), observer); if (m_observers.end() == it) { m_observers.Add(observer); return(true); } else { return(false); } }
//C++ TO C# CONVERTER WARNING: 'const' methods are not available in C#: //ORIGINAL LINE: virtual ClassicVector<Crypto::Hash> getConsumerKnownBlocks(IBlockchainConsumer& consumer) const override public override List <Crypto.Hash> getConsumerKnownBlocks(IBlockchainConsumer consumer) { std::unique_lock <object> lk = new std::unique_lock <object>(m_consumersMutex); var state = getConsumerSynchronizationState(consumer); if (state == null) { var message = "Failed to get consumer known blocks: not found"; m_logger.functorMethod(ERROR, BRIGHT_RED) << message << ", consumer " << consumer; throw new System.ArgumentException(message); } return(state.getKnownBlockHashes()); }
public void close(bool wait = false) { std::unique_lock <object> lk = new std::unique_lock <object>(m_mutex); m_closed = true; m_haveData.notify_all(); // wake up threads in pop() m_haveSpace.notify_all(); if (wait) { while (!m_queue.empty()) { m_haveSpace.wait(lk); } } }
public bool remove(T observer) { std::unique_lock <object> @lock = new std::unique_lock <object>(m_observersMutex); var it = std::find(m_observers.GetEnumerator(), m_observers.end(), observer); if (m_observers.end() == it) { return(false); } else { //C++ TO C# CONVERTER TODO TASK: There is no direct equivalent to the STL vector 'erase' method in C#: m_observers.erase(it); return(true); } }
//C++ TO C# CONVERTER TODO TASK: The original C++ template specifier was replaced with a C# generic specifier, which may not produce the same behavior: //ORIGINAL LINE: template <typename TT> //C++ TO C# CONVERTER TODO TASK: 'rvalue references' have no equivalent in C#: public bool push <TT>(TT && v) { std::unique_lock <object> lk = new std::unique_lock <object>(m_mutex); while (!m_closed && m_queue.size() >= m_maxSize) { m_haveSpace.wait(lk); } if (m_closed) { return(false); } m_queue.push_back(std::forward <TT>(v)); m_haveData.notify_one(); return(true); }
public override std::future removeUnconfirmedTransaction(Crypto.Hash transactionHash) { m_logger.functorMethod(INFO, BRIGHT_WHITE) << "Removing unconfirmed transaction, hash " << transactionHash; std::unique_lock <object> @lock = new std::unique_lock <object>(m_stateMutex); if (m_currentState == State.stopped || m_futureState == State.stopped) { var message = "Failed to remove unconfirmed transaction: not stopped"; m_logger.functorMethod(ERROR, BRIGHT_RED) << message << ", hash " << transactionHash; throw new System.Exception(message); } std::promise promise = new std::promise(); var future = promise.get_future(); m_removeTransactionTasks.emplace_back(transactionHash, std::move(promise)); m_hasWork.notify_one(); return(future); }
public override std::future <std::error_code> addUnconfirmedTransaction(ITransactionReader transaction) { m_logger.functorMethod(INFO, BRIGHT_WHITE) << "Adding unconfirmed transaction, hash " << transaction.GetTransactionHash(); std::unique_lock <object> @lock = new std::unique_lock <object>(m_stateMutex); if (m_currentState == State.stopped || m_futureState == State.stopped) { var message = "Failed to add unconfirmed transaction: not stopped"; m_logger.functorMethod(ERROR, BRIGHT_RED) << message << ", hash " << transaction.GetTransactionHash(); throw new System.Exception(message); } std::promise <std::error_code> promise = new std::promise <std::error_code>(); var future = promise.get_future(); m_addTransactionTasks.emplace_back(transaction, std::move(promise)); m_hasWork.notify_one(); return(future); }
public void clear() { std::unique_lock <object> @lock = new std::unique_lock <object>(m_observersMutex); m_observers.Clear(); }
public void addAsyncContext() { std::unique_lock <object> @lock = new std::unique_lock <object>(m_mutex); m_asyncContexts++; }
//C++ TO C# CONVERTER WARNING: 'const' methods are not available in C#: //ORIGINAL LINE: virtual IStreamSerializable* getConsumerState(IBlockchainConsumer* consumer) const override public override IStreamSerializable getConsumerState(IBlockchainConsumer consumer) { std::unique_lock <object> lk = new std::unique_lock <object>(m_consumersMutex); return(getConsumerSynchronizationState(consumer)); }
public ulong size() { std::unique_lock <object> lk = new std::unique_lock <object>(m_mutex); return(m_queue.size()); }
public override void poolChanged() { logger.functorMethod(DEBUGGING) << "Got poolChanged notification."; if (!synchronized.load() || observersCounter.load() == 0) { return; } if (!poolUpdateGuard.beginUpdate()) { return; } ScopeExitHandler poolUpdateEndGuard = new ScopeExitHandler(std::bind(this.poolUpdateEndHandler, this)); std::unique_lock <object> @lock = new std::unique_lock <object>(mutex); var rawNewTransactionsPtr = new List <std::unique_ptr <ITransactionReader> >(); var removedTransactionsPtr = new List <Hash>(); var isBlockchainActualPtr = new bool(false); //C++ TO C# CONVERTER TODO TASK: Only lambda expressions having all locals passed by reference can be converted to C#: //ORIGINAL LINE: NodeRequest request([this, rawNewTransactionsPtr, removedTransactionsPtr, isBlockchainActualPtr](const INode::Callback& callback) NodeRequest new request((INodeOriginal.Callback callback) => { List <Hash> hashes = new List <Hash>(); hashes.Capacity = knownPoolState.Count; foreach (Tuple <Hash, TransactionDetails> kv in knownPoolState) { hashes.Add(kv.Item1); } node.getPoolSymmetricDifference(std::move(hashes), reinterpret_cast <Hash&>(knownBlockchainTop.hash), ref *isBlockchainActualPtr, *rawNewTransactionsPtr, *removedTransactionsPtr, callback); }); //C++ TO C# CONVERTER TODO TASK: Only lambda expressions having all locals passed by reference can be converted to C#: //ORIGINAL LINE: request.performAsync(asyncContextCounter, [this, rawNewTransactionsPtr, removedTransactionsPtr, isBlockchainActualPtr](std::error_code ec) request.performAsync(asyncContextCounter, (std::error_code ec) => { ScopeExitHandler poolUpdateEndGuard = new ScopeExitHandler(std::bind(this.poolUpdateEndHandler, this)); if (ec != null) { logger.functorMethod(ERROR) << "Can't send poolChanged notification because can't get pool symmetric difference: " << ec.message(); return; } std::unique_lock <object> @lock = new std::unique_lock <object>(mutex); List <Hash> newTransactionsHashesPtr = new List <Hash>(); newTransactionsHashesPtr.Capacity = rawNewTransactionsPtr.Count; foreach (var rawTransaction in *rawNewTransactionsPtr) { var hash = rawTransaction.getTransactionHash(); logger.functorMethod(DEBUGGING) << "Pool responded with new transaction: " << hash; if (knownPoolState.count(hash) == 0) { newTransactionsHashesPtr.Add(hash); } } var removedTransactionsHashesPtr = new List <Tuple <Hash, TransactionRemoveReason> >(); removedTransactionsHashesPtr.Capacity = removedTransactionsPtr.Count; foreach (Hash hash in *removedTransactionsPtr) { logger.functorMethod(DEBUGGING) << "Pool responded with deleted transaction: " << hash; var iter = knownPoolState.find(hash); if (iter != knownPoolState.end()) { removedTransactionsHashesPtr.Add({ hash, TransactionRemoveReason.INCLUDED_IN_BLOCK }); }