Skip to content

Commit

Permalink
feat: add tokio feature (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jul 25, 2024
1 parent d618829 commit c319409
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 206 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: release

on:
workflow_dispatch:
inputs:
releaseKind:
description: "Kind of release"
default: "minor"
type: choice
options:
- patch
- minor
required: true

jobs:
rust:
name: release
runs-on: ubuntu-latest
timeout-minutes: 30

steps:
- name: Clone repository
uses: actions/checkout@v4
with:
token: ${{ secrets.DENOBOT_PAT }}

- uses: denoland/setup-deno@v1
- uses: dsherret/rust-toolchain-file@v1

- name: Tag and release
env:
GITHUB_TOKEN: ${{ secrets.DENOBOT_PAT }}
GH_WORKFLOW_ACTOR: ${{ github.actor }}
run: |
git config user.email "[email protected]"
git config user.name "denobot"
deno run -A https://raw.githubusercontent.com/denoland/automation/0.15.0/tasks/publish_release.ts --${{github.event.inputs.releaseKind}}
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ jobs:
- uses: dsherret/rust-toolchain-file@v1
- name: Build
run: cargo build
- name: Build (no default features)
run: cargo build --no-default-features
- name: Format
run: cargo fmt -- --check
- name: Lint
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Test
run: cargo test --verbose
run: cargo test

miri:
runs-on: ubuntu-latest
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ repository = "https://github.com/denoland/deno_unsync"
description = "A collection of adapters to make working with Tokio single-threaded runtimes easier"
readme = "README.md"

[features]
default = ["tokio"]
tokio = ["dep:tokio"]

[dependencies]
parking_lot = "0.12.3"
tokio = { version = "1", features = ["rt"] }
tokio = { version = "1", features = ["rt"], optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["io-util", "macros", "rt", "sync"] }
Expand Down
45 changes: 0 additions & 45 deletions src/flag.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use std::cell::Cell;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;

/// A flag with interior mutability that can be raised or lowered.
/// Useful for indicating if an event has occurred.
Expand Down Expand Up @@ -31,32 +29,6 @@ impl Flag {
}
}

/// Simplifies the use of an atomic boolean as a flag.
#[derive(Debug, Default)]
pub struct AtomicFlag(AtomicBool);

impl AtomicFlag {
/// Creates a new flag that's raised.
pub fn raised() -> AtomicFlag {
Self(AtomicBool::new(true))
}

/// Raises the flag returning if the raise was successful.
pub fn raise(&self) -> bool {
!self.0.swap(true, Ordering::SeqCst)
}

/// Lowers the flag returning if the lower was successful.
pub fn lower(&self) -> bool {
self.0.swap(false, Ordering::SeqCst)
}

/// Gets if the flag is raised.
pub fn is_raised(&self) -> bool {
self.0.load(Ordering::SeqCst)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -74,21 +46,4 @@ mod test {
assert!(!flag.lower());
assert!(!flag.is_raised());
}

#[test]
fn atomic_flag_raises_lowers() {
let flag = AtomicFlag::default();
assert!(!flag.is_raised()); // false by default
assert!(flag.raise());
assert!(flag.is_raised());
assert!(!flag.raise());
assert!(flag.is_raised());
assert!(flag.lower());
assert!(flag.raise());
assert!(flag.lower());
assert!(!flag.lower());
let flag = AtomicFlag::raised();
assert!(flag.is_raised());
assert!(flag.lower());
}
}
2 changes: 1 addition & 1 deletion src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::task::Context;
use std::task::Wake;
use std::task::Waker;

use crate::AtomicFlag;
use crate::sync::AtomicFlag;

impl<T: ?Sized> LocalFutureExt for T where T: Future {}

Expand Down
17 changes: 6 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@

mod flag;
pub mod future;
mod joinset;
pub mod mpsc;
mod split;
pub mod sync;
mod task;
mod task_queue;
#[cfg(feature = "tokio")]
mod tokio;
mod waker;

pub use flag::AtomicFlag;
pub use flag::Flag;
pub use joinset::JoinSet;
pub use split::split_io;
pub use split::IOReadHalf;
pub use split::IOWriteHalf;
pub use task::spawn;
pub use task::spawn_blocking;
pub use task::JoinHandle;
pub use task::MaskFutureAsSend;
pub use task_queue::TaskQueue;
pub use task_queue::TaskQueuePermit;
pub use task_queue::TaskQueuePermitAcquireFuture;
pub use waker::UnsyncWaker;

#[cfg(feature = "tokio")]
pub use self::tokio::*;

/// Marker for items that are ![`Send`].
#[derive(Copy, Clone, Default, Eq, PartialEq, PartialOrd, Ord, Debug, Hash)]
pub struct UnsendMarker(
Expand Down
8 changes: 4 additions & 4 deletions src/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ mod test {
#[tokio::test(flavor = "current_thread")]
async fn receiver_recv_then_drop_sender() {
let (sender, mut receiver) = unbounded_channel::<usize>();
let future = crate::task::spawn(async move {
let future = crate::spawn(async move {
let value = receiver.recv().await;
value.is_none()
});
let future2 = crate::task::spawn(async move {
let future2 = crate::spawn(async move {
drop(sender);
true
});
Expand All @@ -186,7 +186,7 @@ mod test {
for sender_ticks in [None, Some(1), Some(10)] {
for sender_count in [1000, 100, 10, 2, 1] {
let (sender, mut receiver) = unbounded_channel::<usize>();
let future = crate::task::spawn(async move {
let future = crate::spawn(async move {
let mut values = Vec::with_capacity(1000);
for _ in 0..1000 {
if let Some(ticks) = receiver_ticks {
Expand Down Expand Up @@ -215,7 +215,7 @@ mod test {
for sender_index in 0..sender_count {
let sender = sender.clone();
let batch_count = 1000 / sender_count;
futures.push(crate::task::spawn(async move {
futures.push(crate::spawn(async move {
for i in 0..batch_count {
if let Some(ticks) = sender_ticks {
for _ in 0..ticks {
Expand Down
52 changes: 52 additions & 0 deletions src/sync/flag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;

/// Simplifies the use of an atomic boolean as a flag.
#[derive(Debug, Default)]
pub struct AtomicFlag(AtomicBool);

impl AtomicFlag {
/// Creates a new flag that's raised.
pub fn raised() -> AtomicFlag {
Self(AtomicBool::new(true))
}

/// Raises the flag returning if the raise was successful.
pub fn raise(&self) -> bool {
!self.0.swap(true, Ordering::SeqCst)
}

/// Lowers the flag returning if the lower was successful.
pub fn lower(&self) -> bool {
self.0.swap(false, Ordering::SeqCst)
}

/// Gets if the flag is raised.
pub fn is_raised(&self) -> bool {
self.0.load(Ordering::SeqCst)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn atomic_flag_raises_lowers() {
let flag = AtomicFlag::default();
assert!(!flag.is_raised()); // false by default
assert!(flag.raise());
assert!(flag.is_raised());
assert!(!flag.raise());
assert!(flag.is_raised());
assert!(flag.lower());
assert!(flag.raise());
assert!(flag.lower());
assert!(!flag.lower());
let flag = AtomicFlag::raised();
assert!(flag.is_raised());
assert!(flag.lower());
}
}
5 changes: 5 additions & 0 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2018-2024 the Deno authors. MIT license.

mod flag;

pub use flag::AtomicFlag;
Loading

0 comments on commit c319409

Please sign in to comment.