Skip to content

Commit

Permalink
Merge pull request #1 from dragostis/ci
Browse files Browse the repository at this point in the history
Added initial CI.
  • Loading branch information
dragostis authored Sep 13, 2024
2 parents 9a9a0d1 + 918b809 commit e750e10
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Continuous integration

on:
push:

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/[email protected]
- uses: Swatinem/rust-cache@v2
- run: cargo check --all --no-default-features
- run: cargo check --all --all-features

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/[email protected]
- uses: Swatinem/rust-cache@v2
- run: cargo test --all --no-default-features
- run: cargo test --all --all-features

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/[email protected]
with:
components: rustfmt
- run: cargo fmt --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/[email protected]
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --all --no-default-features -- -D warnings
- run: cargo clippy --all --all-features -- -D warnings

miri:
name: Miri
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
components: miri
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@nextest
- run: cargo +nightly miri setup
- run: cargo +nightly miri nextest run -j8 -E 'not (test(join_very_long))'
- run: cargo +nightly miri test --many-seeds=0..16 -- join_wait
17 changes: 12 additions & 5 deletions src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<T> Job<T> {
// `Future` in `self.fur_or_next` that should get passed here.
let fut = unsafe { &*(fut as *const Future<T>) };

(*fut).complete(panic::catch_unwind(AssertUnwindSafe(|| f(scope))));
fut.complete(panic::catch_unwind(AssertUnwindSafe(|| f(scope))));
}

Self {
Expand Down Expand Up @@ -190,11 +190,18 @@ impl<T> Job<T> {

/// It should only be called after being popped from a `JobQueue`.
pub unsafe fn wait(&self) -> Option<thread::Result<T>> {
self.fut_or_next
.get()
// Before being popped, the `JobQueue` allocates and store a
self.fut_or_next.get().and_then(|fut| {
// Before being popped, the `JobQueue` allocates and stores a
// `Future` in `self.fur_or_next` that should get passed here.
.and_then(|fut| unsafe { Box::from_raw(fut.as_ptr()).wait() })
let result = unsafe { (*fut.as_ptr()).wait() };
// We only can drop the `Box` *after* waiting on the `Future`
// in order to ensure unique access.
unsafe {
drop(Box::from_raw(fut.as_ptr()));
}

result
})
}

/// It should only be called in the case where the job has been popped
Expand Down
25 changes: 18 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ mod tests {
#[test]
fn join_wait() {
let mut threat_pool = ThreadPool::with_config(Config {
thread_count: Some(2),
heartbeat_interval: Duration::from_micros(1),
..Default::default()
})
Expand Down Expand Up @@ -577,23 +578,32 @@ mod tests {
#[should_panic(expected = "panicked across threads")]
fn join_panic() {
let mut threat_pool = ThreadPool::with_config(Config {
thread_count: Some(2),
heartbeat_interval: Duration::from_micros(1),
..Default::default()
})
.unwrap();

fn increment(s: &mut Scope, slice: &mut [u32], id: ThreadId) {
if let Some(thread_count) = thread::available_parallelism().ok().map(NonZero::get) {
if thread_count == 1 {
// Pass test artificially when only one thread is available.
panic!("panicked across threads");
}
}

fn increment(s: &mut Scope, slice: &mut [ThreadId], id: ThreadId) {
match slice.len() {
0 => (),
1 => slice[0] += 1,
_ => {
let (_, tail) = slice.split_at_mut(1);
let (head, tail) = slice.split_at_mut(1);

s.join(
|_| {
thread::sleep(Duration::from_micros(10));

if thread::current().id() != id {
let current_id = thread::current().id();
head[0] = current_id;

if current_id != id {
panic!("panicked across threads");
}
},
Expand All @@ -603,10 +613,11 @@ mod tests {
}
}

let mut vals = [0; 10];
let mut vals = [thread::current().id(); 10];

increment(&mut threat_pool.scope(), &mut vals, thread::current().id());

assert_eq!(vals, [1; 10]);
// Just in case this test fails.
panic!("thread IDs: {:?}", vals);
}
}

0 comments on commit e750e10

Please sign in to comment.