kaist-cp/cs431

[Question] [HW4] How to store self in target's last in `start_enqueue`?

Closed this issue · 2 comments

The functions swap and compare_exchange on AtomicPtr wants a *mut T. However, in order to enqueue self to target, I need &mut self not &self. How can I swap an AtomicPtr with a value that I have only immutable reference to?

The reference MCS Lock implementation uses an owned value to enqueue:

let node = Node::new();
let prev = self.tail.swap(node, AcqRel);

The return type of 'Node::new()' is '* mut Node'. If you have a '&self', just do 'self as * const T as * mut T'

Yep! Didn't know cast like that is valid. Thanks!