Is it possible to use *mut T when I have created an Arc<T>

I'm writting a task control block(tcb_t). I have use Arc<RwLock<>> to lock the struct to allow different task(or thread) to use the task control block, writing some new values and reading new values. But now, I want to use *mut tcb_t. I'm sure it's unsafe but I still want to use the pointer value to get other locations' address. what can I do?

pub struct task_control_block {

pub type tcb_t = task_control_block;
pub type TCB = task_control_block;
pub struct TaskHandle(pub Arc<RwLock<TCB>>);

pub unsafe extern "C" fn getReceiveSlots(thread: *mut tcb_t, buffer: *mut u64) -> Arc<cte_t> {

besides, how can I convert the NULL in C for *tcb_t?

Get a &mut reference and convert it to a pointer. You should keep the Arc and the RwLockWriteGuard alive while performing the actions with the pointer, however (the Arc so the object won't be freed in the middle and the RwLockWriteGuard guard so that RwLock will know the mutable borrow is still in use and will block usages from another threads:

let task_handle = ...;
let guard = task_handler.0.write().unwrap();
let p: *mut tcb_t = &mut *guard;
unsafe {
    // Use `p`...

Do you mean you get a NULL from FFI? Then just declare the type as wanted. Or do you want to create NULL from Rust? Then use std::ptr::null() (or std::ptr::null_mut()).

1 Like

I want to translate
in C

a pointer in C: p
p == NULL

to Rust

TaskHandle == ???(something is Rust, maybe None?)

TaskHandler is an Arc, not a pointer, and thus cannot be null.

1 Like

Arc is not nullable. If you have a raw pointer, *const T or *mut T, then you can use its is_null() method for checking if it is NULL.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.