1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/// A generic map based on a singly-linked list data structure that is lock-free 
/// and uses `AtomicPtr`s to ensure safety in the face of multithreaded access.
/// Each node remains in the same spot in memory once it is allocated,
/// and will not be reallocated,
/// which allows an external thread to maintain a reference to it safely.
///
/// Currently we do not allow nodes to be deleted, so it's only useful for certain purposes.
/// Later on, once deletions are supported, it will not be safe to maintain out-of-band references
/// to items in the data structure, rather only weak references. 
///
/// New elements are inserted at the head of the list, and then the head's next pointer 
/// is set up to the point to the node that was previously the head. 
/// Thus, the head always points to the most recently added node. 


use alloc::boxed::Box;
use core::sync::atomic::{AtomicPtr, Ordering};

#[derive(Debug)]
struct Node<K, V> where K: PartialEq {
    key: K,
	value: V,
    next: AtomicPtr<Node<K, V>>,
}
impl<K, V> Node<K, V> where K: PartialEq {
    fn new(k: K, v: V) -> Node<K, V> {
        Node { 
            key: k,
			value: v,
            next: AtomicPtr::default(), // null ptr by default
        }
    }
}

#[derive(Debug)]
pub struct AtomicMap<K, V> where K: PartialEq {
    head: AtomicPtr<Node<K, V>>,
}

impl<K, V> AtomicMap<K, V> where K: PartialEq {
    /// Create a new empty AtomicMap.
    /// 
    /// Does not perform any allocation until a new node is created.
    pub const fn new() -> AtomicMap<K, V> {
        AtomicMap {
            head: AtomicPtr::new(core::ptr::null_mut()), // null ptr
        }
    }

    /// Adds a new key-value pair to the map. 
	/// If the given key is already present, its corresponding value will be overwritten.
    pub fn insert(&self, key: K, value: V) -> Option<V> {
        let res = self.insert_timeout(key, value, u64::max_value());
        res.unwrap_or(None) // return the Option<V> in Ok(), or None if Err()
    }

    
	/// Adds a new key-value pair to the map. 
	/// If the given key is already present, its corresponding value will be overwritten.
	/// If it fails to do so atomically after the given number of attempts, it will abort and return Err.
    pub fn insert_timeout(&self, key: K, value: V, max_attempts: u64) -> Result<Option<V>, V> {

		// first, we check to see if the key exists in the list already.
		// if it does, simply update the corresponding value.
		for pair in self.iter_mut() {
			if key == *pair.0 {
                let old_val = ::core::mem::replace(&mut *pair.1, value);
				return Ok(Some(old_val));
			}
		}
        
		// Here, the key did not exist, so we must add a new node to hold that key-value pair.
        let node_ptr = Box::into_raw(Box::new(Node::new(key, value)));
        let max_attempts = core::cmp::max(max_attempts, 1); // ensure we try at least once 

        // start the first attempt by obtaining the current head pointer
        let mut orig_head_ptr = self.head.load(Ordering::Acquire);
        for _attempt in 0..max_attempts {

            // the new "node" will become the new head, so set the node's `next` pointer to `orig_head_ptr`
            // SAFE: we know the node_ptr is valid since we just created it above.
            unsafe {
                (*node_ptr).next = AtomicPtr::new(orig_head_ptr);
            }

            // now try to atomically swap the new `node_ptr` into the current `head` ptr
            match self.head.compare_exchange_weak(orig_head_ptr, node_ptr, Ordering::AcqRel, Ordering::Acquire) {
                // If compare_exchange succeeds, then the `head` ptr was properly updated, i.e.,
                // no other thread was interleaved and snuck in to change `head` since we last loaded it.
                Ok(_old_head_ptr) => return Ok(None),
                Err(changed_head_ptr) => orig_head_ptr = changed_head_ptr,
            }
            
            // Here, it didn't work, the head value wasn't updated, meaning that another process updated it before we could
            // so we need to start over by reading the head ptr again and trying to swap it in again
            #[cfg(test)] 
            println!("        attempt {}", _attempt);
        }

        // Here, we exceeded the number of max attempts, so we failed. 
        // Reclaim the Boxed `Node`, drop the Box, and return the inner data of type `V`.
        // SAFE: no one has touched this node except for us when we created it above.
        let reclaimed_node = unsafe {
            Box::from_raw(node_ptr)
        };

        Err(reclaimed_node.value)
    }

	/// Returns a reference to the value matching the given key, if present. 
	/// Otherwise, returns None. 
	pub fn get(&self, key: &K) -> Option<&V> {
		for pair in self.iter() {
			if key == pair.0 {
				return Some(pair.1);
			}
		}
		None
	}


    /// Returns a mutable reference to the value matching the given key, if present.
    /// Otherwise, returns None.
    /// In order to maintain memory safety (to ensure atomicity), getting a value as mutable
    /// requires `self` (this `AtomicMap` instance) to be borrowed mutably. 
    pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
        for pair in self.iter_mut() {
                if key == *pair.0 {
                        return Some(pair.1);
                }
        }
        None
    }


    /// Returns a forward iterator through this map. 
    pub fn iter(&self) -> AtomicMapIter<K, V> {
        AtomicMapIter {
            curr: &self.head, //load(Ordering::Acquire),
            // _phantom: PhantomData,
        }
    }

    /// This should only be used internally, as we don't want outside entities
    /// holding mutable references to data here.
    /// Returns a forward iterator through this map,
    /// allowing mutation of inner values but not keys.
    /// This is safe because we do not permit deletion from this map type.
    fn iter_mut(&self) -> AtomicMapIterMut<K, V> {
        AtomicMapIterMut {
            curr: &self.head, //load(Ordering::Acquire),
            // _phantom: PhantomData,
        }
    }
}

impl<K, V> Drop for AtomicMap<K, V> where K: PartialEq {
    fn drop(&mut self) {
        let mut curr_ptr = self.head.load(Ordering::Acquire);
        while !curr_ptr.is_null() {
            // SAFE: checked for null above
            let next_ptr = unsafe {&*curr_ptr}.next.load(Ordering::Acquire);
            let _ = unsafe { Box::from_raw(curr_ptr) }; // drop the actual Node
            curr_ptr = next_ptr;
        }
    }
}




pub struct AtomicMapIter<'a, K: PartialEq + 'a, V: 'a> {
    curr: &'a AtomicPtr<Node<K, V>>,
    // _phantom: PhantomData<&'a K, V>, // we don't need this with the &'a above
}
impl<'a, K: PartialEq + 'a, V: 'a> Iterator for AtomicMapIter<'a, K, V> {
    type Item = (&'a K, &'a V); 

    fn next(&mut self) -> Option<(&'a K, &'a V)> {
        let curr_ptr = self.curr.load(Ordering::Acquire);
        if curr_ptr == (0 as *mut _) {
            return None;
        }
        // SAFE: curr_ptr was checked for null
        let curr_node: &Node<K, V> = unsafe { &*curr_ptr };
        self.curr = &curr_node.next; // advance the iterator
        Some((&curr_node.key, &curr_node.value))
    }
}



pub struct AtomicMapIterMut<'a, K: PartialEq + 'a, V: 'a> {
    curr: &'a AtomicPtr<Node<K, V>>,
    // _phantom: PhantomData<&'a K, V>, // we don't need this with the &'a above
}
impl<'a, K: PartialEq + 'a, V: 'a> Iterator for AtomicMapIterMut<'a, K, V> {
    type Item = (&'a K, &'a mut V); 

    fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
        let curr_ptr = self.curr.load(Ordering::Acquire);
        if curr_ptr == (0 as *mut _) {
            return None;
        }
        // SAFE: curr_ptr was checked for null
        let curr_node: &mut Node<K, V> = unsafe { &mut *curr_ptr };
        self.curr = &curr_node.next; // advance the iterator
        Some((&curr_node.key, &mut curr_node.value))
    }
}


#[test]
/// To run this test, execute: `cargo test test_map -- --nocapture`
fn test_map() {
    let map: AtomicMap<&'static str, u64> = AtomicMap::new();
    
	let should_be_none = map.get(&"yo");
	println!("should_be_none = {:?}", should_be_none);

	map.insert("yo", 2); 

	println!("after yo 2");
    for i in map.iter() {
        println!("    {:?}", i);
    }

	map.insert("hi", 45);
	let old_yo = map.insert("yo", 1234); 
    println!("old_yo = {:?}", old_yo);

	println!("after yo 4");
    for i in map.iter() {
        println!("    {:?}", i);
    }

	let should_be_45 = map.get(&"hi");
	println!("should_be_45 = {:?}", should_be_45);

    let should_now_be_1234 = map.get(&"yo");
    println!("should_now_be_1234 = {:?}", should_now_be_1234);
    
}