ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - whitfin/sleeplocks: BEAM friendly spinlocks for Elixir/Erlang
BEAM friendly spinlocks for Elixir/Erlang. Contribute to whitfin/sleeplocks development by creating an account on GitHub.
Visit Site

GitHub - whitfin/sleeplocks: BEAM friendly spinlocks for Elixir/Erlang

GitHub - whitfin/sleeplocks: BEAM friendly spinlocks for Elixir/Erlang

sleeplocks

Build Status Hex.pm Version Documentation

This library is designed to provide simple locking mechanisms in Erlang/Elixir, similar to how spinlocks work in other languages - except using messages to communicate locking.

This is useful for libraries which require lock synchronization, without having to roll your own (however simple). Locks can be held by arbitrary numbers of process, making it possible to implement various throttling mechanisms.

Best of all, this library is tiny! It builds upon basic OTP principles to implement lock behaviour via simple processes and message passing.

Installation

Rebar

Follow the instructons found here to configure your Rebar setup to use Hex as a dependency source, then you can grab it directly:

{deps,[
  % pulls the latest version
  sleeplocks,
  % to pull the latest version from github
  {sleeplocks, {git, "git://github.com/whitfin/sleeplocks.git"}}
]}.

Mix

To install it for your project, you can pull it directly from Hex. Rather than use the version shown below, you can use the latest version from Hex (shown at the top of this README).

def deps do
  [{:sleeplocks, "~> 1.0"}]
end

Usage

Snippets below contain sample usage in both Erlang and Elixir, and cover most of the small API space offered by sleeplocks. For a more complete example, scroll down!

Erlang

% create a new single lock (with a name)
1> sleeplocks:new(1, [{name, {local, my_lock}}]).
{ok,<0.179.0>}

% take ownership of the lock
2> sleeplocks:acquire(my_lock).
ok

% release the current hold on a lock
3> sleeplocks:release(my_lock).
ok

% attempt to acquire a lock (which will succeed)
4> sleeplocks:attempt(my_lock).
ok

% now that it's taken, other attempts will fail
5> sleeplocks:attempt(my_lock).
{error,unavailable}

% release the lock again
6> sleeplocks:release(my_lock).
ok

% handle acquisition and locking automatically
7> sleeplocks:execute(my_lock, fun() ->
7>   3
7> end).
3

Elixir

# create a new single lock (with a name)
iex(1)> :sleeplocks.new(1, [ name: :my_lock ])
{:ok, #PID<0.179.0>}

# take ownership of the lock
iex(2)> :sleeplocks.acquire(:my_lock)
:ok

# release the current hold on a lock
iex(3)> :sleeplocks.release(:my_lock)
:ok

# attempt to acquire a lock (which will succeed)
iex(4)> :sleeplocks.attempt(:my_lock)
:ok

# now that it's taken, other attempts will fail
iex(5)> :sleeplocks.attempt(:my_lock)
{:error, :unavailable}

# release the lock again
iex(6)> :sleeplocks.release(:my_lock)
:ok

# handle acquisition and locking automatically
iex(7)> :sleeplocks.execute(:my_lock, fn ->
iex(7)>   3
iex(7)> end)
3

Examples

This example is in Elixir, but it should be fairly understandable for those coming from both languages. It simply spawns 6 processes which each attempt to hold a lock for 10 seconds. As the lock is created with only 2 slots, this runs for 30 seconds and 2 of our spawned tasks can hold the lock at any given time.

# First create a new lock, with 2 slots only
{:ok, ref} = :sleeplocks.new(2)

# Then spawn 6 tasks, which each just sleep for 10 seconds
# after acquiring the lock. This means that 2 processes will
# acquire a lock and then release after 10 seconds. This
# will repeat 3 times (6 / 2) until 30 seconds are up.
for idx <- 1..6 do
  Task.start(fn ->
    :sleeplocks.execute(ref, fn ->
      IO.puts("Locked #{idx}")
      Process.sleep(10_000)
      IO.puts("Releasing #{idx}")
    end)
  end)
end

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory