| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (PhasedLatch.scala)

This example Scala source code file (PhasedLatch.scala) is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

abstractqueuedsynchronizer, int, long, phasedlatch, phasedlatches, queuedsynchronizer, threading, threads, timeunit

The PhasedLatch.scala Scala example source code

package scalaz
package concurrent

import effect.IO

import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.AbstractQueuedSynchronizer

sealed abstract class PhasedLatch {
  /** Release the current phase. */
  def release(): IO[Unit]

  /** Await the current phase. */
  final def await() = currentPhase flatMap awaitPhase

  /** Await the current phase for the specified period. */
  final def awaitFor(time: Long, unit: TimeUnit) = currentPhase flatMap { awaitPhaseFor(_, time, unit) }

  /** Await for the specified phase.*/
  def awaitPhase(phase: Int): IO[Unit]

  /** Await the specified phase for the specified period.*/
  def awaitPhaseFor(phase: Int, period: Long, unit: TimeUnit): IO[Boolean]

  def currentPhase: IO[Int]

object PhasedLatch extends PhasedLatches

trait PhasedLatches {
  private[this] val phaseOrder = Order.order[Int] { (a, b) =>
    import Ordering._
    (b - a) match {
      case 0 => EQ
      case x if x > 0 => GT
      case y if y < 0 => LT

  def newPhasedLatch: IO[PhasedLatch] = IO(new PhasedLatch {
    /** This sync implements Phasing. The state represents the current phase as
     *  an integer that continually increases. The phase can wrap around past
     *  Int#MaxValue
    class QueuedSynchronizer extends AbstractQueuedSynchronizer {
      def currentPhase = getState

      override def tryAcquireShared(waitingFor: Int) =
        if (phaseOrder.lessThan(currentPhase, waitingFor)) 1
        else -1

      override final def tryReleaseShared(ignore: Int) = {
        val phase = currentPhase
        if (compareAndSetState(phase, phase + 1)) true
        else tryReleaseShared(ignore)

    val sync = new QueuedSynchronizer

    /** Release the current phase. */
    def release = IO { sync releaseShared 1 }

    /** Await for the specified phase.*/
    def awaitPhase(phase: Int) = IO { sync acquireSharedInterruptibly phase }

    def awaitPhaseFor(phase: Int, period: Long, unit: TimeUnit) = IO {
      sync.tryAcquireSharedNanos(phase, unit.toNanos(period))

    def currentPhase = IO(sync.currentPhase)

Other Scala examples (source code examples)

Here is a short list of links related to this Scala PhasedLatch.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.