Giter VIP home page Giter VIP logo

reagents.jl's Introduction

Reagents.jl: Towards composable and extensible nonblocking programming for Julia

docs dev

Reagents.jl implements and extends reagents by Turon (2012). It provides higher-order concurrency primitives for expressing nonblocking¹ algorithms and synchronizations of concurrent tasks in a composable manner.

For example, op1 | op2 is the choice combinator that combines the "lazy" representation of operations (called reagents) and expresses that only one of the operations take place. This is similar to the select statement popularized by Go as a mechanism for expressing rich concurrency patterns. This is a form of the selective communication that is implemented by numerous other languages (and libraries) such as Erlang (receive expression), occam (ALT statement), and Concurrent ML² (select expression), to name a few. However, unlike Go that only supports synchronization of channels in select³ or Erlang that only supports selecting incoming messages, Reagents.jl's choice combinator supports arbitrary user-defined data structures. Furthermore, it provides other combinators such as and & for declaring atomicity of the operations, similar to the software transactional memory mechanism.

Reagents.jl is a foundation of Julio.jl, an implementation of structured concurrency for Julia. For supporting this, Reagents.jl extends the original description of reagents (Turon, 2012) by adding more primitives such as WithNack from Concurrent ML (which is a natural extension due to the influence of Concurrent ML on reagents, as Turon (2012) noted).


¹ Due to the simplistic implementation of the k-CAS, Reagents.jl is not yet nonblocking in the strict sense. However, as discussed in Turon (2012), it should be straightforward to switch to a k-CAS algorithm with more strict guarantee.

² This includes other languages such as Racket and GNU Guile that implemented the Concurrent ML primitives.

³ But perhaps for good reasons for Go.

Example: Treiber stack

Let us implement Treiber stack which can be represented as an atomic reference to an immutable list:

using Reagents

struct Node{T}
    head::T
    tail::Union{Node{T},Nothing}
end

const List{T} = Union{Node{T},Nothing}

struct TreiberStack{T,Ref<:Reagents.Ref{List{T}}}
    head::Ref
end

TreiberStack{T}() where {T} = TreiberStack(Reagents.Ref{List{T}}(nothing))

The push and pop operations can be expressed as reagents:

pushing(stack::TreiberStack) =
    Reagents.Update((xs, x) -> (Node(x, xs), nothing), stack.head)

popping(stack::TreiberStack) =
    Reagents.Update(stack.head) do xs, _
        if xs === nothing
            return (nothing, nothing)
        else
            return (xs.tail, xs.head)
        end
    end

The execution ("reaction") of the reagent can be invoked by just calling the reagent object. So, it's straightforward to wrap it in the standard function API:

Base.push!(stack::TreiberStack, value) = pushing(stack)(value)
Base.pop!(stack::TreiberStack) = popping(stack)()

These user-defined reagents can be composed just like pre-defined reagents. For example, we can move an element from one stack to another by using the sequencing combinator :

s1 = TreiberStack{Int}()
s2 = TreiberStack{Int}()
push!(s1, 1)
(popping(s1)  pushing(s2))()
@assert pop!(s2) == 1

Here, the element in the stack s1 is popped and then pushed to the stack s2 atomically. Similar code works with arbitrary pair of containers, possibly of different types.

For more examples, read the documentation or see the examples directory.

Resources

reagents.jl's People

Contributors

tkf avatar c42f avatar

Stargazers

ebigram avatar Jesper Stemann Andersen avatar Massimo Nocentini avatar  avatar Dominique Luna avatar Marco Matthies avatar kolia avatar Jayesh K. Gupta avatar Lasse Peters avatar Ejaaz Merali avatar Eric Hanson avatar Chad Scherrer avatar Felipe Noronha avatar kai avatar Johnny Chen avatar Julian Samaroo avatar Yue Yang avatar Elias Carvalho avatar Robert Schwarz avatar Sebastian Pfitzner avatar Andrey Oskin avatar Peter avatar  avatar

Watchers

 avatar  avatar

Forkers

c42f

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.