Skip to content

Commit 0638186

Browse files
committed
WIP
Signed-off-by: Boqun Feng <[email protected]>
1 parent 5ab9311 commit 0638186

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

rust/kernel/thread.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@ macro_rules! thread_try_new {
126126
}};
127127
}
128128

129+
/*
130+
pub trait ThreadArg<A: Send> {
131+
fn from_raw(ptr: *mut c_types::c_void) -> A;
132+
fn into_raw(arg: A) -> *mut c_types::c_void;
133+
}
134+
pub trait ThreadFunc<A: Send> {
135+
fn func(arg: A) -> KernelResult<()>;
136+
}
137+
138+
extern "C" fn bridge<A, T: ThreadFunc<A>>(data: *mut c_types::c_void) -> i32
139+
where A : Send
140+
{
141+
let arg = unsafe { core::intrinsics::transmute(data) };
142+
143+
match T::func(arg) {
144+
Ok(_) => 0,
145+
Err(e) => e.to_kernel_errno(),
146+
}
147+
}
148+
*/
149+
129150
/// Function passed to `kthread_create_on_node` as the thread function pointer.
130151
#[no_mangle]
131152
unsafe extern "C" fn rust_thread_func(data: *mut c_types::c_void) -> c_types::c_int {
@@ -200,6 +221,33 @@ impl Thread {
200221
Ok(Thread { task })
201222
}
202223

224+
/*
225+
pub fn try_new_thread_func<A, T: ThreadFunc<A>>(name: CStr, arg: A) -> KernelResult<Self>
226+
where A: Send
227+
{
228+
let data = unsafe { core::intrinsics::transmute(arg) };
229+
230+
let result = unsafe { Self::try_new_c_style(name, bridge::<A,T>, data) };
231+
232+
if let Err(e) = result {
233+
// Creation fails, we need to `transmute` back the `arg` because
234+
// there is no new thread to own it, we should let the current
235+
// thread own it.
236+
//
237+
// SAFETY: We `transmute` back waht we just `transmute`, and since
238+
// the new thread is not created, so no one touches `data`.
239+
unsafe {
240+
core::intrinsics::transmute::<_, A>(data);
241+
}
242+
243+
Err(e)
244+
} else {
245+
result
246+
}
247+
248+
}
249+
*/
250+
203251
/// Creates a new thread.
204252
///
205253
/// # Examples

0 commit comments

Comments
 (0)