replace synchronization primitives with those from parking_lot (#1593)
* parking_lot in cargo.toml * replace all lock invocations with parking_lot ones * use parking_lot synchronization primitives
This commit is contained in:
committed by
Gav Wood
parent
4226c0f631
commit
36d3d0d7d7
@@ -16,10 +16,10 @@
|
||||
|
||||
use std::thread;
|
||||
use std::time::{Instant, Duration};
|
||||
use std::sync::{mpsc, Mutex, RwLock, Arc};
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::collections::HashMap;
|
||||
use jsonrpc_core;
|
||||
use util::{U256, Lockable, RwLockable};
|
||||
use util::{Mutex, RwLock, U256};
|
||||
use v1::helpers::{TransactionRequest, TransactionConfirmation};
|
||||
|
||||
/// Result that can be returned from JSON RPC.
|
||||
@@ -110,7 +110,7 @@ pub struct ConfirmationPromise {
|
||||
impl ConfirmationToken {
|
||||
/// Submit solution to all listeners
|
||||
fn resolve(&self, result: Option<RpcResult>) {
|
||||
let mut res = self.result.locked();
|
||||
let mut res = self.result.lock();
|
||||
*res = result.map_or(ConfirmationResult::Rejected, |h| ConfirmationResult::Confirmed(h));
|
||||
// Notify listener
|
||||
self.handle.unpark();
|
||||
@@ -142,7 +142,7 @@ impl ConfirmationPromise {
|
||||
// Park thread (may wake up spuriously)
|
||||
thread::park_timeout(deadline - now);
|
||||
// Take confirmation result
|
||||
let res = self.result.locked();
|
||||
let res = self.result.lock();
|
||||
// Check the result
|
||||
match *res {
|
||||
ConfirmationResult::Rejected => return None,
|
||||
@@ -183,7 +183,7 @@ impl ConfirmationsQueue {
|
||||
/// This method can be used only once (only single consumer of events can exist).
|
||||
pub fn start_listening<F>(&self, listener: F) -> Result<(), QueueError>
|
||||
where F: Fn(QueueEvent) -> () {
|
||||
let recv = self.receiver.locked().take();
|
||||
let recv = self.receiver.lock().take();
|
||||
if let None = recv {
|
||||
return Err(QueueError::AlreadyUsed);
|
||||
}
|
||||
@@ -208,13 +208,13 @@ impl ConfirmationsQueue {
|
||||
/// Notifies receiver about the event happening in this queue.
|
||||
fn notify(&self, message: QueueEvent) {
|
||||
// We don't really care about the result
|
||||
let _ = self.sender.locked().send(message);
|
||||
let _ = self.sender.lock().send(message);
|
||||
}
|
||||
|
||||
/// Removes transaction from this queue and notifies `ConfirmationPromise` holders about the result.
|
||||
/// Notifies also a receiver about that event.
|
||||
fn remove(&self, id: U256, result: Option<RpcResult>) -> Option<TransactionConfirmation> {
|
||||
let token = self.queue.unwrapped_write().remove(&id);
|
||||
let token = self.queue.write().remove(&id);
|
||||
|
||||
if let Some(token) = token {
|
||||
// notify receiver about the event
|
||||
@@ -241,13 +241,13 @@ impl SigningQueue for ConfirmationsQueue {
|
||||
fn add_request(&self, transaction: TransactionRequest) -> ConfirmationPromise {
|
||||
// Increment id
|
||||
let id = {
|
||||
let mut last_id = self.id.locked();
|
||||
let mut last_id = self.id.lock();
|
||||
*last_id = *last_id + U256::from(1);
|
||||
*last_id
|
||||
};
|
||||
// Add request to queue
|
||||
let res = {
|
||||
let mut queue = self.queue.unwrapped_write();
|
||||
let mut queue = self.queue.write();
|
||||
queue.insert(id, ConfirmationToken {
|
||||
result: Arc::new(Mutex::new(ConfirmationResult::Waiting)),
|
||||
handle: thread::current(),
|
||||
@@ -266,7 +266,7 @@ impl SigningQueue for ConfirmationsQueue {
|
||||
}
|
||||
|
||||
fn peek(&self, id: &U256) -> Option<TransactionConfirmation> {
|
||||
self.queue.unwrapped_read().get(id).map(|token| token.request.clone())
|
||||
self.queue.read().get(id).map(|token| token.request.clone())
|
||||
}
|
||||
|
||||
fn request_rejected(&self, id: U256) -> Option<TransactionConfirmation> {
|
||||
@@ -280,17 +280,17 @@ impl SigningQueue for ConfirmationsQueue {
|
||||
}
|
||||
|
||||
fn requests(&self) -> Vec<TransactionConfirmation> {
|
||||
let queue = self.queue.unwrapped_read();
|
||||
let queue = self.queue.read();
|
||||
queue.values().map(|token| token.request.clone()).collect()
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
let queue = self.queue.unwrapped_read();
|
||||
let queue = self.queue.read();
|
||||
queue.len()
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
let queue = self.queue.unwrapped_read();
|
||||
let queue = self.queue.read();
|
||||
queue.is_empty()
|
||||
}
|
||||
}
|
||||
@@ -300,8 +300,8 @@ impl SigningQueue for ConfirmationsQueue {
|
||||
mod test {
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use util::{Address, U256, H256, Lockable};
|
||||
use std::sync::Arc;
|
||||
use util::{Address, U256, H256, Mutex};
|
||||
use v1::helpers::{SigningQueue, ConfirmationsQueue, QueueEvent, TransactionRequest};
|
||||
use v1::types::H256 as NH256;
|
||||
use jsonrpc_core::to_value;
|
||||
@@ -354,7 +354,7 @@ mod test {
|
||||
let r = received.clone();
|
||||
let handle = thread::spawn(move || {
|
||||
q.start_listening(move |notification| {
|
||||
let mut v = r.locked();
|
||||
let mut v = r.lock();
|
||||
*v = Some(notification);
|
||||
}).expect("Should be closed nicely.")
|
||||
});
|
||||
@@ -363,7 +363,7 @@ mod test {
|
||||
|
||||
// then
|
||||
handle.join().expect("Thread should finish nicely");
|
||||
let r = received.locked().take();
|
||||
let r = received.lock().take();
|
||||
assert_eq!(r, Some(QueueEvent::NewRequest(U256::from(1))));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user