Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CI

on: [push, pull_request]

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
components: clippy
- name: Clippy
run: cargo clippy --all-features --all-targets -- -Dwarnings
- name: Test
run: cargo test --all-features
doc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- name: Build docs
run: |
cargo doc --all-features --no-deps
printf '<meta http-equiv="refresh" content="0;url=%s/index.html">' $(cargo tree | head -1 | cut -d' ' -f1 | tr '-' '_') > target/doc/index.html
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: target/doc

deploy:
runs-on: ubuntu-latest
needs: doc
if: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
permissions:
contents: read
pages: write
id-token: write
environment:
name: github-pages
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
171 changes: 171 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ linux-raw-sys = { version = "0.11", default-features = false, features = [
"general",
] }
spin = { version = "0.10", default-features = false, features = ["spin_mutex"] }

[dev-dependencies]
futures = "0.3"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
102 changes: 102 additions & 0 deletions tests/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::{
future::Future,
pin::Pin,
sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
},
task::{Context, Poll},
time::Duration,
};

use axpoll::PollSet;
use tokio::time;

struct WaitFuture {
ps: Arc<PollSet>,
ready: Arc<AtomicBool>,
}

impl Future for WaitFuture {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.ready.load(Ordering::SeqCst) {
Poll::Ready(())
} else {
self.ps.register(cx.waker());
Poll::Pending
}
}
}

impl WaitFuture {
fn new(ps: Arc<PollSet>, ready: Arc<AtomicBool>) -> Self {
Self { ps, ready }
}
}

struct Counter(AtomicUsize);

impl Counter {
fn new() -> Arc<Self> {
Arc::new(Self(AtomicUsize::new(0)))
}

fn count(&self) -> usize {
self.0.load(Ordering::SeqCst)
}

fn add(&self) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}

#[tokio::test]
async fn async_wake_single() {
let ps = Arc::new(PollSet::new());
let ready = Arc::new(AtomicBool::new(false));

let f = WaitFuture::new(ps.clone(), ready.clone());

let handle = tokio::spawn(async move {
ready.clone().store(true, Ordering::SeqCst);
ps.clone().wake();
});

f.await;
handle.await.unwrap();
}

#[tokio::test]
async fn async_wake_many() {
let ps = Arc::new(PollSet::new());
let counter = Counter::new();

let mut flags = Vec::new();
let mut handles = Vec::new();

for _ in 0..65 {
let flag = Arc::new(AtomicBool::new(false));
let f = WaitFuture::new(ps.clone(), flag.clone());
let counter = counter.clone();
let h = tokio::spawn(async move {
f.await;
counter.add();
});
flags.push(flag);
handles.push(h);
}

time::sleep(Duration::from_millis(20)).await;

for f in &flags {
f.store(true, Ordering::SeqCst);
}
ps.wake();
for h in handles {
h.await.unwrap();
}

assert_eq!(counter.count(), 65);
}
Loading