~mht/cmr

ca4e418292c86f4708403654d7ee932d1a7db89f — Martin Hafskjold Thoresen 3 years ago a964674
Fix the alloc lock. Closes #10

It's crazy that this actually ever worked: ever since we added the padding to
the thread bool signaling if the thread is allocating the lock have been broken:
instead of having threads write to the bool, they would write to the first byte
in the padding instead of the actual `AtomicBool` in the padded struct.
The locking would look, correctly, at the `AtomicBool`, which now would always
be `false`.
2 files changed, 6 insertions(+), 10 deletions(-)

M Cargo.toml
M src/lib.rs
M Cargo.toml => Cargo.toml +1 -1
@@ 5,7 5,7 @@ authors = ["Martin Hafskjold Thoresen <martinhath@gmail.com>"]

[dependencies]
lazy_static = "*"
memmap = "*"
memmap = "=0.7.0"
byteorder = "*"
tid = "*"
parking_lot = { version = "*", features = ["nightly"] }

M src/lib.rs => src/lib.rs +5 -9
@@ 746,7 746,7 @@ fn background_thread(recv: Receiver<BackgroundThreadInfo>) {
                let mut thread_bools = THREAD_BOOLS.lock();
                assert!(thread_bools.len() < MAX_THREADS);
                thread_bools.push(PaddedAtomicBool::new(false));
                let addr = &thread_bools[thread_bools.len() - 1] as *const _ as *mut AtomicBool;
                let addr = &thread_bools[thread_bools.len() - 1].inner as *const _ as *mut AtomicBool;
                let atomic: &AtomicPtr<AtomicBool> = unsafe { &*ptr };
                atomic.store(addr, SeqCst);
                continue;


@@ 761,20 761,16 @@ fn background_thread(recv: Receiver<BackgroundThreadInfo>) {
        };

        // timed!("free thread",
        let marker = {
            let r: &[u8] = &map;
            let f: &u8 = &r[0];
            f as *const u8 as *const u64
        };
        let mut cursor: Cursor<&mut [u8]> = Cursor::new(&mut map);
        // A MARKER value we use for signaling that the child process isn't done yet.
        // Wait until the child thread has written all ptrs. We know this when it has written the
        // number of pointers to free to position `allocs_pos`, which now contains `MARKER`.
        let mut ptr_count = MARKER;
        while ptr_count == MARKER {
            cursor.set_position(0);
            ptr_count = cursor.read_u64::<NativeEndian>().unwrap();
            let bytes = map.get(0..8).unwrap();
            ptr_count = Cursor::new(bytes).read_u64::<NativeEndian>().unwrap();
        }
        let mut cursor: Cursor<&mut [u8]> = Cursor::new(&mut map);
        cursor.set_position(8);

        let reachable = {
            let a = 8;