forked from tokio-rs/tokio-uring
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0156395
commit 8fd2efd
Showing
7 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::io::futex::{UnsubmittedFutexWait, UnsubmittedFutexWake}; | ||
|
||
#[allow(missing_docs)] | ||
pub struct Futex; | ||
impl Futex { | ||
#[allow(missing_docs)] | ||
pub fn wait(futex: *const u32, val: u64, mask: u64, futex_flags: u32) -> UnsubmittedFutexWait { | ||
UnsubmittedFutexWait::wait(futex, val, mask, futex_flags) | ||
} | ||
|
||
#[allow(missing_docs)] | ||
pub fn wake(futex: *const u32, val: u64, mask: u64, futex_flags: u32) -> UnsubmittedFutexWake { | ||
UnsubmittedFutexWake::wake(futex, val, mask, futex_flags) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use io_uring::{cqueue::Entry, opcode}; | ||
|
||
use crate::{OneshotOutputTransform, UnsubmittedOneshot}; | ||
|
||
#[allow(missing_docs)] | ||
pub type UnsubmittedFutexWait = UnsubmittedOneshot<FutexWaitData, FutexWaitTransform>; | ||
|
||
#[allow(missing_docs)] | ||
pub struct FutexWaitData {} | ||
|
||
#[allow(missing_docs)] | ||
pub struct FutexWaitTransform {} | ||
|
||
impl OneshotOutputTransform for FutexWaitTransform { | ||
type Output = Result<(), std::io::Error>; | ||
type StoredData = FutexWaitData; | ||
|
||
fn transform_oneshot_output(self, _data: Self::StoredData, cqe: Entry) -> Self::Output { | ||
let n = cqe.result(); | ||
if n >= 0 { | ||
Ok(()) | ||
} else { | ||
Err(std::io::Error::from_raw_os_error(-n)) | ||
} | ||
} | ||
} | ||
|
||
impl UnsubmittedFutexWait { | ||
pub(crate) fn wait(futex: *const u32, val: u64, mask: u64, futex_flags: u32) -> Self { | ||
Self::new( | ||
FutexWaitData {}, | ||
FutexWaitTransform {}, | ||
opcode::FutexWait::new(futex, val, mask, futex_flags).build(), | ||
) | ||
} | ||
} | ||
|
||
#[allow(missing_docs)] | ||
pub type UnsubmittedFutexWake = UnsubmittedOneshot<FutexWakeData, FutexWakeTransform>; | ||
|
||
#[allow(missing_docs)] | ||
pub struct FutexWakeData {} | ||
|
||
#[allow(missing_docs)] | ||
pub struct FutexWakeTransform {} | ||
|
||
impl OneshotOutputTransform for FutexWakeTransform { | ||
type Output = Result<i32, std::io::Error>; | ||
type StoredData = FutexWakeData; | ||
|
||
fn transform_oneshot_output(self, _data: Self::StoredData, cqe: Entry) -> Self::Output { | ||
let n = cqe.result(); | ||
if n >= 0 { | ||
Ok(n) | ||
} else { | ||
Err(std::io::Error::from_raw_os_error(-n)) | ||
} | ||
} | ||
} | ||
|
||
impl UnsubmittedFutexWake { | ||
pub(crate) fn wake(futex: *const u32, val: u64, mask: u64, futex_flags: u32) -> Self { | ||
Self::new( | ||
FutexWakeData {}, | ||
FutexWakeTransform {}, | ||
opcode::FutexWake::new(futex, val, mask, futex_flags).build(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,5 @@ pub(crate) mod writev; | |
|
||
mod writev_all; | ||
pub(crate) use writev_all::writev_at_all; | ||
|
||
pub mod futex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use libc::FUTEX_BITSET_MATCH_ANY; | ||
use tokio::task::spawn_local; | ||
use tokio_uring::Submit; | ||
|
||
const FUTEX2_SIZE_U32: u32 = 0x2; | ||
|
||
#[test] | ||
fn mutex_test() { | ||
tokio_uring::start(async { | ||
let f = Box::new(0u32); | ||
let ptr: *const u32 = &*f; | ||
|
||
let ptr_clone = ptr.clone(); | ||
let mask = FUTEX_BITSET_MATCH_ANY as u32 as u64; | ||
let handle = spawn_local(async move { | ||
tokio_uring::fs::Futex::wait(ptr_clone, 0, mask, FUTEX2_SIZE_U32) | ||
.submit() | ||
.await | ||
.unwrap(); | ||
}); | ||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; | ||
let res = tokio_uring::fs::Futex::wake(ptr, 1, mask, FUTEX2_SIZE_U32) | ||
.submit() | ||
.await | ||
.unwrap(); | ||
assert!(res == 1); | ||
|
||
handle.await.unwrap(); | ||
}); | ||
} |