ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LongMutex.java
Revision: 1.4
Committed: Wed Dec 31 17:00:58 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +2 -2 lines
Log Message:
lexicographic import order

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.3 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7 jsr166 1.4 import java.io.*;
8 dl 1.1 import java.util.*;
9     import java.util.concurrent.*;
10 jsr166 1.4 import java.util.concurrent.atomic.*;
11 dl 1.1 import java.util.concurrent.locks.*;
12    
13     /**
14 jsr166 1.2 * A sample user extension of AbstractQueuedLongSynchronizer.
15 dl 1.1 */
16     public final class LongMutex extends AbstractQueuedLongSynchronizer implements Lock, java.io.Serializable {
17     static final long LOCKED = -1L;
18     public boolean isHeldExclusively() { return getState() == LOCKED; }
19 jsr166 1.2
20 dl 1.1 public boolean tryAcquire(long acquires) {
21     return compareAndSetState(0, LOCKED);
22     }
23 jsr166 1.2
24 dl 1.1 public boolean tryRelease(long releases) {
25     setState(0);
26     return true;
27     }
28     public Condition newCondition() { return new ConditionObject(); }
29 jsr166 1.2
30 dl 1.1 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
31     s.defaultReadObject();
32     setState(0); // reset to unlocked state
33     }
34    
35 jsr166 1.2 public void lock() {
36     acquire(LOCKED);
37 dl 1.1 }
38 jsr166 1.2 public boolean tryLock() {
39 dl 1.1 return tryAcquire(LOCKED);
40     }
41 jsr166 1.2 public void lockInterruptibly() throws InterruptedException {
42 dl 1.1 acquireInterruptibly(LOCKED);
43     }
44     public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
45     return tryAcquireNanos(LOCKED, unit.toNanos(timeout));
46     }
47     public void unlock() { release(LOCKED); }
48     }