All Projects → slide-rs → Atom

slide-rs / Atom

Licence: apache-2.0
Safe atomic pointers

Programming Languages

rust
11053 projects

Labels

Projects that are alternatives of or similar to Atom

Atom Modular Snippets
:atom: A modular solution to snippets in @Atom.
Stars: ✭ 8 (-77.14%)
Mutual labels:  atom
Atom Svgo
Minify SVG with SVGO.
Stars: ✭ 12 (-65.71%)
Mutual labels:  atom
Atom Vim Mode Plus Ex Mode
Experiment to implement ex-mode for vim-mode-plus
Stars: ✭ 30 (-14.29%)
Mutual labels:  atom
Atom Mocha Test Runner
Run your Atom package tests using Mocha
Stars: ✭ 10 (-71.43%)
Mutual labels:  atom
Hfeed2atom
Python functions to convert a h-feed to Atom 1.0
Stars: ✭ 11 (-68.57%)
Mutual labels:  atom
Atom Annotations
Atom package that shows annotations (e.g. for overriden methods interface implementations) in your PHP source code.
Stars: ✭ 14 (-60%)
Mutual labels:  atom
Atom Korean Spell Daum
Daum 맞춤법 검사기로 텍스트를 보내 손쉽게 교정 가능한 Atom package
Stars: ✭ 24 (-31.43%)
Mutual labels:  atom
Envy
Text editing supercharger
Stars: ✭ 35 (+0%)
Mutual labels:  atom
Language Dot
Dot (Graphviz) package for Atom
Stars: ✭ 11 (-68.57%)
Mutual labels:  atom
Fizzy
🍇 🍓 🍊 A lovely, bright and lively syntax theme for Atom.
Stars: ✭ 29 (-17.14%)
Mutual labels:  atom
Seti Ui
A subtle dark colored UI theme for Atom.
Stars: ✭ 856 (+2345.71%)
Mutual labels:  atom
Color Indent
Use colors to show in a non intrusive manner different indentation levels.
Stars: ✭ 11 (-68.57%)
Mutual labels:  atom
Atcon
atom condtions
Stars: ✭ 27 (-22.86%)
Mutual labels:  atom
Atom Check Updates
A CLI for easily updating Atom to the latest version on RPM or Debian-based systems.
Stars: ✭ 8 (-77.14%)
Mutual labels:  atom
Youtube Fetcher
📺 Youtube Podcasting 🎧
Stars: ✭ 31 (-11.43%)
Mutual labels:  atom
Atomic Chrome
Edit Chrome textareas in Atom
Stars: ✭ 930 (+2557.14%)
Mutual labels:  atom
Github
Git and GitHub integration for Atom
Stars: ✭ 880 (+2414.29%)
Mutual labels:  atom
Atom Stylefmt
Format your CSS using stylefmt.
Stars: ✭ 35 (+0%)
Mutual labels:  atom
Feeds2imap.clj
Pull RSS/Atom feeds to your IMAP folders with Clojure on JVM.
Stars: ✭ 31 (-11.43%)
Mutual labels:  atom
Language Liquid
Liquid language support for Atom.
Stars: ✭ 28 (-20%)
Mutual labels:  atom

Atom

Build Status Atom

Atom is a simple abstraction around Rust's AtomicPtr. It provides a simple, wait-free way to exchange data between threads safely. Atom is built around the principle that an atomic swap can be used to safely emulate Rust's ownership.

store

Using store to set a shared atomic pointer is unsafe in rust (or any language) because the contents of the pointer can be overwritten at any point in time causing the contents of the pointer to be lost. This can cause your system to leak memory, and if you are expecting that memory to do something useful (like wake a sleeping thread), you are in trouble.

load

Similarly, load is unsafe since there is no guarantee that that pointer will live for even a cycle after you have read it. Another thread may modify the pointer, or free it. For load to be safe you need to have some outside contract to preserve the correct ownership semantics.

swap

A swap is special as it allows a reference to be exchanged without the risk of that pointer being freed, or stomped on. When a thread swaps an AtomicPtr the old pointer ownership is moved to the caller, and the AtomicPtr takes ownership of the new pointer.

Using Atom

Add atom your Cargo.toml

[dependencies]
atom="*"

A short example:

extern crate atom;

use std::sync::Arc;
use std::thread;
use atom::*;

fn main() {
    // Create an empty atom
    let shared_atom = Arc::new(Atom::empty());

    // set the value 75 
    shared_atom.swap(Box::new(75));

    // Spawn a bunch of thread that will try and take the value
    let threads: Vec<thread::JoinHandle<()>> = (0..8).map(|_| {
        let shared_atom = shared_atom.clone();
        thread::spawn(move || {
            // Take the contents of the atom, only one will win the race
            if let Some(v) = shared_atom.take() {
                println!("I got it: {:?} :D", v);
            } else {
                println!("I did not get it :(");
            }
        })
    }).collect();

    // join the threads
    for t in threads { t.join().unwrap(); }

The result will look something like this:

I did not get it :(
I got it: 75 :D
I did not get it :(
I did not get it :(
I did not get it :(
I did not get it :(
I did not get it :(
I did not get it :(

Using an Atom has some advantages over using a raw AtomicPtr. First, you don't need any unsafe code in order to convert the Box<T> to and from a Box the library handles that for you. Secondly, Atom implements drop so you won't accidentally leak a pointer when dropping your data structure.

AtomSetOnce

This is an additional bit of abstraction around an Atom. Recall that I said load was unsafe unless you have an additional restrictions. AtomSetOnce as the name indicates may only be set once, and then it may never be unset. We know that if the Atom is set the pointer will be valid for the lifetime of the Atom. This means we can implement Deref in a safe way.

Take a look at the fifo example to see how this can be used to write a lock-free linked list.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].