8000 MVar, a non-blocking Task-based implementation by alexandru · Pull Request #301 · monix/monix · GitHub
[go: up one dir, main page]

Skip to content
10000

MVar, a non-blocking Task-based implementation - #301

Merged
alexandru merged 2 commits into
masterfrom
mvar
Jan 21, 2017
Merged

alexandru merged 2 commits into
masterfrom
mvar

Conversation

@alexandru
@alexandru alexandru commented Jan 20, 2017 •
Copy link
Copy Markdown
Member

Inspired by Control.Concurrent.MVar from Haskell and by scalaz.concurrent.MVar.

An MVar is mutable location, that is either empty or contains a value of type A with these operations:

abstract class MVar[A] {
  def put(a: A): Task[Unit]
  def take: Task[A]
  def read: Task[A]
}

The put and take operations are atomic and their behavior is similar to usage of a BlockingQueue(capacity = 1). Or in other words take blocks (asynchronously, with no threads getting abused) until there is a value available and put blocks (asynchronously) until the var is empty.

The read operation is provided for convenience and is equivalent with:

for (a <- v.take; _ <- v.put(a)) yield a

It's not atomic, so in case atomicity is needed, it can be combined with extra synchronization primitives, like TaskSemaphore.

Given its non-blocking nature, this implementation is cross-compiled and works on top of Javascript.

Work in progress: must add tests !!!

@alexandru alexandru changed the title MVar implementation MVar, a non-blocking Task-based implementation Jan 20, 2017
@codecov-io
codecov-io commented Jan 20, 2017 •
Copy link
Copy Markdown

Current coverage is 87.21% (diff: 82.89%)

Merging #301 into master will increase coverage by 0.08%

@@             master       #301   diff @@
==========================================
  Files           265        268     +3   
  Lines          7883       7960    +77   
  Methods        6301       6361    +60   
  Messages          0          0          
  Branches       1580       1596    +16   
==========================================
+ Hits           6868       6942    +74   
- Misses         1015       1018     +3   
  Partials          0          0          

Powered by Codecov. Last update 7972c7e...8753eaf

@alexandru
alexandru merged commit b1bea62 into master Jan 21, 2017
@alexandru
alexandru deleted the mvar branch January 21, 2017 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0