Git Product home page Git Product logo

swift-stm's Introduction

swift-stm

Twitter: @lbrndnr License

About

swift-stm is an optimistic and lock free software transactional memory written in Swift. It's very rudimental and really only a draft as of now. I wouldn't be using it in production code if I were you. Feel free to try it out though :)

What does it do exactly?

An STM allows you to write thread safe code by making blocks of code seem to be executed atomically. This means that the transaction is either executed in a given point in time or not at all. In reality, however, it just executes the transaction and notices potential collisions. The idea behind this is that in real life, collisions are rare and locks a bit of an overhead. This means that (technically) STMs are a lot easier to use (no deadlocks, better performance yada yada) than traditional locks. Note that this is just a draft, I can't guarantee mutex nor performance!

So how do I use it?

So instead of using Dispatch...

func transfer(from: Account, to: Account, amount: Int) -> Bool {
    var res = false
    
    queue.async {            
        let i = from.balance
        
        guard i >= amount else {
            return
        }
        
        from.balance = i - amount
       	to.balance = to.balance + amount
        
        res = true
    }
    
   return res
}

... you'd write something like this

func transfer(from: Account, to: Account, amount: Int) -> Bool {
    var res = false
    
    atomic {            
        let i = try from.balance.get()
        
        guard i >= amount else {
            return
        }
        
        try from.balance.set(i - amount)
        try to.balance.set(to.balance.get() + amount)
        
        res = true
    }
    
    return res
}

I must admit that the syntax is a bit clumsy as of now...

License

swift-stm is licensed under the MIT License.

swift-stm's People

Contributors

lbrndnr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.