Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rework the internal structure of the event-listener crate #51

Merged
merged 21 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move node.rs and queue.rs to be submodules of list.rs
This move abstracts them to be a sub-module of the inner list.
  • Loading branch information
notgull committed Mar 31, 2023
commit 393566ada711e65b78a9873d753376e8d83e68c4
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ extern crate alloc;
extern crate std;

mod list;
mod node;
mod queue;
mod sync;

use alloc::sync::Arc;
Expand Down
21 changes: 14 additions & 7 deletions src/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
//! The inner list of listeners.

use crate::node::{Node, TaskWaiting};
use crate::queue::Queue;
#[path = "list/node.rs"]
fogti marked this conversation as resolved.
Show resolved Hide resolved
mod node;

#[path = "list/queue.rs"]
mod queue;

use node::{Node, TaskWaiting};
use queue::Queue;

use crate::sync::atomic::{AtomicBool, Ordering};
use crate::sync::cell::{Cell, UnsafeCell};
use crate::sync::Arc;
Expand All @@ -15,7 +22,7 @@ use slab::Slab;

impl crate::Inner {
/// Locks the list.
fn lock(&self) -> Option<ListGuard<'_>> {
fn try_lock(&self) -> Option<ListGuard<'_>> {
self.list.inner.try_lock().map(|guard| ListGuard {
inner: self,
guard: Some(guard),
Expand All @@ -31,7 +38,7 @@ impl crate::Inner {
return;
}

match self.lock() {
match self.try_lock() {
Some(mut lock) => {
let key = lock.insert(State::Created);
*listener = Listener::HasNode(key);
Expand All @@ -50,7 +57,7 @@ impl crate::Inner {
pub(crate) fn remove(&self, listener: &mut Listener, propogate: bool) -> Option<State> {
let state = match mem::replace(listener, Listener::Discarded) {
Listener::HasNode(key) => {
match self.lock() {
match self.try_lock() {
Some(mut list) => {
// Fast path removal.
list.remove(Listener::HasNode(key), propogate)
Expand Down Expand Up @@ -85,7 +92,7 @@ impl crate::Inner {
/// Notifies a number of entries.
#[cold]
pub(crate) fn notify(&self, n: usize, additional: bool) {
match self.lock() {
match self.try_lock() {
Some(mut guard) => {
// Notify the listeners.
guard.notify(n, additional);
Expand All @@ -112,7 +119,7 @@ impl crate::Inner {
match mem::replace(listener, Listener::Discarded) {
Listener::HasNode(key) => {
*listener = Listener::HasNode(key);
match self.lock() {
match self.try_lock() {
Some(mut guard) => {
// Fast path registration.
return guard.register(listener, task);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/queue.rs → src/list/queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The queue of nodes that keeps track of pending operations.

use crate::node::Node;
use super::node::Node;
use crate::sync::atomic::{AtomicPtr, Ordering};

use crossbeam_utils::CachePadded;
Expand Down