Skip to content

Commit

Permalink
Use paritytech/nohash-hasher and add comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
twittner committed Sep 18, 2018
1 parent 33471ca commit 1235f93
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 48 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "MIT"
[dependencies]
bytes = "0.4"
futures = "0.1"
nohash-hasher = { git = "https://github.com/paritytech/nohash-hasher" }
log = "0.4"
parking_lot = "0.6"
quick-error = "0.1"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
extern crate bytes;
#[macro_use]
extern crate futures;
extern crate nohash_hasher;
#[macro_use]
extern crate log;
extern crate parking_lot;
Expand Down
16 changes: 12 additions & 4 deletions src/notify/mod.rs → src/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mod hash;

use futures::{executor, task};
use parking_lot::Mutex;
use self::hash::HashMap;
use nohash_hasher::IntMap;
use std::sync::atomic::{AtomicUsize, Ordering};


Expand All @@ -32,26 +31,35 @@ task_local!{
}


/// A notifier maintains a collection of tasks which should be
/// notified at some point. Useful in conjuction with `futures::executor::Spawn`.
pub struct Notifier {
tasks: Mutex<HashMap<usize, task::Task>>
tasks: Mutex<IntMap<usize, task::Task>>
}

impl Notifier {
pub fn new() -> Self {
Notifier { tasks: Mutex::new(HashMap::default()) }
Notifier { tasks: Mutex::new(IntMap::default()) }
}

/// Insert the current task to the set of tasks to be notified.
///
/// # Panics
///
/// If called outside of a tokio task.
pub fn insert_current(&self) {
self.tasks.lock().insert(TASK_ID.with(|&t| t), task::current());
}

/// Notify all registered tasks.
pub fn notify_all(&self) {
let mut tasks = self.tasks.lock();
for (_, t) in tasks.drain() {
t.notify();
}
}

/// Return the number of currently registered tasks.
pub fn len(&self) -> usize {
self.tasks.lock().len()
}
Expand Down
44 changes: 0 additions & 44 deletions src/notify/hash.rs

This file was deleted.

0 comments on commit 1235f93

Please sign in to comment.