forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Boqun Feng <[email protected]>
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
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
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,44 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
use crate::bindings; | ||
use crate::c_types; | ||
|
||
use alloc::boxed::Box; | ||
use core::ops::FnOnce; | ||
|
||
#[no_mangle] | ||
unsafe extern "C" fn rust_thread_func(data: *mut c_types::c_void) -> c_types::c_int { | ||
Box::from_raw(data as *mut Box<dyn FnOnce()>)(); | ||
0 | ||
} | ||
|
||
pub struct Thread { | ||
task: *mut bindings::task_struct, | ||
} | ||
|
||
impl Thread { | ||
pub fn new<F>(f: F, name: &str) -> Self | ||
where | ||
F: FnOnce(), | ||
F: Send + 'static, | ||
{ | ||
let bf: Box<dyn FnOnce() + 'static> = Box::new(f); | ||
|
||
unsafe { | ||
let task = bindings::kthread_create_on_node( | ||
Some(rust_thread_func), | ||
Box::into_raw(Box::new(bf)) as *mut _, | ||
bindings::NUMA_NO_NODE, | ||
"%s".as_ptr() as *const c_types::c_char, | ||
name.as_ptr(), | ||
); | ||
Thread { task: task } | ||
} | ||
} | ||
|
||
pub fn wake_up(&self) { | ||
unsafe { | ||
bindings::wake_up_process(self.task); | ||
} | ||
} | ||
} |