Git Product home page Git Product logo

overlock's Introduction

Overlock

Overlock is a concurrency utility library for Scala. It is currently comprised of three modules: atomicmap, lock, and threadpool.

To add overlock as a dependency to a maven project, add the following dependency and repo definitions.

  <dependency>
     <groupId>com.boundary</groupId>
     <artifactId>overlock-scala_${scala.version}</artifactId>
     <version>0.8.0</version>
  </dependency>

  <repository>
     <id>BoundaryPublicRepo</id>
     <name>Boundary Public Repo</name>
     <url>http://maven.boundary.com/artifactory/repo/</url>
  </repository>

To add overlock as a dependency to an sbt project put the following in your project file:

  val boundaryPublic = "Boundary Public Repo" at "http://maven.boundary.com/artifactory/repo"
  
  val overlock = "com.boundary" %% "overlock" % "0.8.0"

Overlock is currently supported on Scala 2.9.1.

AtomicMap

The AtomicMap trait provides atomic behavior for the getOrElseUpdate method. It does so via a removal memoizer pattern, explained here. The AtomicMap object has factory methods for creating AtomicMap instances backed by ConcurrentHashMap, ConcurrentSkipListMap or NonBlockingHashMap. Use like so:

import overlock.atomicmap._

//ConcurrentSkipListMap
val cslm = AtomicMap.atomicCSLM[String,Any](new CustomComparator)

//ConcurrentHashMap
val chm = AtomicMap.atomicCHM[String,Any](10000, 0.75, 32)

//NonBlockingHashMap
val nbhm = AtomicMap.atomicNBHM[String,Any](10000)

Threadpool

The threadpool module provides Executor instances backed by thread pools. ThreadPool is a factory object with methods for creating a few different kinds of Executors. All of the executors created by ThreadPool are instrumented using Coda Hale's excellent metrics project. The instrumentation tracks the rate at which work is inserted, the rate of work rejection, summary statistics of work latency, and gauge readings on work queue size, thread pool size, and active thread count.

import overlock.threadpool._

val cachedPool = ThreadPool.instrumentedCached("com.myapplication", "RequestPool1")

The instrumented cached thread pool has the same thread creation behavior as Executors.newCachedThreadPool(). The path and name parameters control the JMX path and name respectively.

val fixedPool = ThreadPool.instrumentedFixed("com.myapplication", "RequestPool2", 16)

The instrumented fixed thread pool has the same thread creation behavior as Executors.newFixedThreadPool(int). n controls the fixed thread pool size and path and name are as above.

val elasticPool = ThreadPool.instrumentedElastic("com.myapplication", "RequestPool3", 10, 50)

The instrumented elastic thread pool has an entirely different thread creation behavior from any of the thread pools available in the java.util.concurrent package. The elastic thread pool will favor the creation of new threads up to the given maximum number of threads. After the thread limit has been reached the elastic thread pool will queue incoming work unboundedly. This is in contrast to normal ThreadPoolExecutor behavior, which will favor queueing over thread creation.

Lock

The lock module contains two locking utilities, Lock and SpinLock. Lock is a wrapper for ReentrantReadWriteLock. It provides idiomatic Scala syntax wrappers for locking:

import overlock.lock._

val lock = new Lock

lock.readLock {
  //do work that can be shared by many reader threads
}

lock.writeLock {
  //do work that requires exclusive access by one thread
}

Spinlock is a utility to provide a form of fast locking known as spinlocks. Spinlocks prefer to repeatedly check that a lock is released rather than let the JVM put the thread to sleep. Spinlocks are useful for increasing throughput when it is known in advance that the critical section will be locked for less time than it takes to perform a context switch between threads. Spinlocks, however, will provide no benefit on single core machines and may reduce overall throughput.

import overlock.lock._

val lock = new SpinLock

lock.readLock {
  //do work that can be shared by many reader threads
}

lock.writeLock {
  //do work that requires exclusive access by one thread
}

overlock's People

Contributors

blinsay avatar boundaryjenkins avatar d2fn avatar moonpolysoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

overlock's Issues

Possible race condition in SpinLock

I believe there is a possible race condition in the SpinLock class. The following serialization shows the issue:

count: 0, writer: true

R1 writer.get => true
W1 writer.set(false)
R1 writer.get => false
W2 writer.set(true)
W2 count.get => 0
W2 enters critical section
R1 count.incrementAndGet => 1
R1 enters critical section

It looks like it is possible for a reader and a writer to be in the critical section at the same time.

monitored ScheduledThreadPoolExecutor?

Are you guys interested in a monitored ScheduledThreadPoolExecutor instance?

I only use overlock for the monitored threadpools, and it'd be nice to be able to monitor scheduledthreadpoolexecutors using overlock.

Update copyright to 2011-2013

Hi,

I've seen the Scala files are still pointing to 2011. The script below should upgrade to 2011-2013. Save it somewhere in the path, and run it from the root of the overlock project.


!/bin/bash

for f in $(find . -name *.scala | xargs grep -R -l 'Copyright 2011, Boundary')
do

echo "$f"
sed 's/Copyright 2011/Copyright 2011-2013/' "$f" > out.tmp
mv out.tmp "$f"

done


PS: I tested it and it worked. :)

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.