Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a race-free allocation using POSIX #10

Open
gnzlbg opened this issue Jan 15, 2018 · 2 comments
Open

Implement a race-free allocation using POSIX #10

gnzlbg opened this issue Jan 15, 2018 · 2 comments
Labels
enhancement New feature or request

Comments

@gnzlbg
Copy link
Owner

gnzlbg commented Jan 15, 2018

See: https://groups.google.com/d/msg/comp.os.linux.development.system/Prx7ExCzsv4/saKCMIeJHhgJ

The algorithm should be:

void* alloc_mirrored(size_t size) {
    assert(size % allocation_granularity() == 0);
    // Create a file and unlink it:
    int fd = shm_open("/unique_name", O_RDWR | O_CREAT | O_EXCL, 0600);
    //    handle_errors(fd);
    shm_unlink("/unique_name");
   // on Linux one might be able to avoid the link/unlink dance
    
    // Resize the file
    ftruncate(fd, size);
    auto addr1 = mmap(0, 2*size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    //  handle_errors(addr1);
    auto addr2 = mmap(addr1+size, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);
    //  handle_errors(addr2);

    close(fd);  // will stay alive until we unmap

    return addr1;
}

void dealloc_mirrored(void* ptr, size_t size) {
    munmap(ptr, 2 * size);
}
@gnzlbg
Copy link
Owner Author

gnzlbg commented Jan 16, 2018

Initial implementation in https://github.com/gnzlbg/slice_deque/tree/posix_race_free

@gnzlbg
Copy link
Owner Author

gnzlbg commented Jan 18, 2018

A race-free algorithm for Linux has been added, but a POSIX-compliant implementation using only /dev/shm remains to be implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant