summaryrefslogtreecommitdiff
path: root/test/disabled/presentation/akka/src/akka/util/LockUtil.scala
blob: 4aaefadc4a93865497bbb2cb8ed9b3016d79697d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
 */

package akka.util

import java.util.concurrent.locks.{ ReentrantReadWriteLock, ReentrantLock }
import java.util.concurrent.atomic.{ AtomicBoolean }
import akka.event.EventHandler

/**
 * @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
 */
final class ReentrantGuard {
  val lock = new ReentrantLock

  final def withGuard[T](body: => T): T = {
    lock.lock
    try {
      body
    } finally {
      lock.unlock
    }
  }

  final def tryWithGuard[T](body: => T): T = {
    while (!lock.tryLock) { Thread.sleep(10) } // wait on the monitor to be unlocked
    try {
      body
    } finally {
      lock.unlock
    }
  }
}

/**
 * @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
 */
class ReadWriteGuard {
  private val rwl = new ReentrantReadWriteLock
  val readLock = rwl.readLock
  val writeLock = rwl.writeLock

  def withWriteGuard[T](body: => T): T = {
    writeLock.lock
    try {
      body
    } finally {
      writeLock.unlock
    }
  }

  def withReadGuard[T](body: => T): T = {
    readLock.lock
    try {
      body
    } finally {
      readLock.unlock
    }
  }
}

/**
 * A very simple lock that uses CCAS (Compare Compare-And-Swap)
 * Does not keep track of the owner and isn't Reentrant, so don't nest and try to stick to the if*-methods
 */
class SimpleLock {
  val acquired = new AtomicBoolean(false)

  def ifPossible(perform: () => Unit): Boolean = {
    if (tryLock()) {
      try {
        perform
      } finally {
        unlock()
      }
      true
    } else false
  }

  def ifPossibleYield[T](perform: () => T): Option[T] = {
    if (tryLock()) {
      try {
        Some(perform())
      } finally {
        unlock()
      }
    } else None
  }

  def ifPossibleApply[T, R](value: T)(function: (T) => R): Option[R] = {
    if (tryLock()) {
      try {
        Some(function(value))
      } finally {
        unlock()
      }
    } else None
  }

  def tryLock() = {
    if (acquired.get) false
    else acquired.compareAndSet(false, true)
  }

  def tryUnlock() = {
    acquired.compareAndSet(true, false)
  }

  def locked = acquired.get

  def unlock() {
    acquired.set(false)
  }
}

/**
 * An atomic switch that can be either on or off
 */
class Switch(startAsOn: Boolean = false) {
  private val switch = new AtomicBoolean(startAsOn)

  protected def transcend(from: Boolean, action: => Unit): Boolean = synchronized {
    if (switch.compareAndSet(from, !from)) {
      try {
        action
      } catch {
        case e: Throwable =>
          EventHandler.error(e, this, e.getMessage)
          switch.compareAndSet(!from, from) // revert status
          throw e
      }
      true
    } else false
  }

  def switchOff(action: => Unit): Boolean = transcend(from = true, action)
  def switchOn(action: => Unit): Boolean = transcend(from = false, action)

  def switchOff: Boolean = synchronized { switch.compareAndSet(true, false) }
  def switchOn: Boolean = synchronized { switch.compareAndSet(false, true) }

  def ifOnYield[T](action: => T): Option[T] = {
    if (switch.get) Some(action)
    else None
  }

  def ifOffYield[T](action: => T): Option[T] = {
    if (!switch.get) Some(action)
    else None
  }

  def ifOn(action: => Unit): Boolean = {
    if (switch.get) {
      action
      true
    } else false
  }

  def ifOff(action: => Unit): Boolean = {
    if (!switch.get) {
      action
      true
    } else false
  }

  def whileOnYield[T](action: => T): Option[T] = synchronized {
    if (switch.get) Some(action)
    else None
  }

  def whileOffYield[T](action: => T): Option[T] = synchronized {
    if (!switch.get) Some(action)
    else None
  }

  def whileOn(action: => Unit): Boolean = synchronized {
    if (switch.get) {
      action
      true
    } else false
  }

  def whileOff(action: => Unit): Boolean = synchronized {
    if (switch.get) {
      action
      true
    } else false
  }

  def ifElseYield[T](on: => T)(off: => T) = synchronized {
    if (switch.get) on else off
  }

  def isOn = switch.get
  def isOff = !isOn
}