-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutex.java
More file actions
129 lines (111 loc) · 3.57 KB
/
Mutex.java
File metadata and controls
129 lines (111 loc) · 3.57 KB
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
/*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify as you wish.
* Copyright (c) 2010 Uwe B. Meding <uwe@uwemeding.com>
*/
public class Mutex {
/**
* Thread that owns this lock status
*/
private Thread owner;
/**
* Constructor
*/
public Mutex() {
this.owner = null;
}
/**
* Acquire this mutex, blocking indefinitely
* @return true once the mutex has been acquired
*/
public boolean acquire() throws InterruptedException {
if (Thread.interrupted()) {
// we got interrupted
throw new InterruptedException();
}
synchronized (this) {
if (owner == Thread.currentThread()) {
// thread already owns this mutex
return true;
}
try {
while (owner != null) {
this.wait();
}
// acquired mutex
owner = Thread.currentThread();
} catch (InterruptedException ex) {
// we were interrupeted while acquiring mutex
notify();
throw ex;
}
return true;
}
}
/**
* Attempt to acquire this mutex by waiting up to the given millisec
* @return true if granted, false otherwise
*/
public boolean attempt(long msecs) throws InterruptedException {
if (Thread.interrupted()) {
// we got interrupted
throw new InterruptedException();
}
synchronized (this) {
if (owner == null) {
// acquiring mutex
owner = Thread.currentThread();
return true;
} else if (owner == Thread.currentThread()) {
// we already own the mutex
return true;
} else if (msecs <= 0) {
// we did not get the mutex, the other thread did
// not release it
return false;
} else {
// waiting for the mutex to become available
long waitTime = msecs;
long start = System.currentTimeMillis();
try {
while (true) {
this.wait(waitTime);
if (owner == null) {
owner = Thread.currentThread();
// we acquired it after waiting
return true;
} else {
waitTime = msecs - (System.currentTimeMillis() - start);
if (waitTime <= 0) {
// failed to get mutex in time
return false;
}
}
}
} catch (InterruptedException ex) {
// we got interrupted while acquiring the mutex
notify();
throw ex;
}
}
}
}
/**
* Release this mutex
* @throw IllegalStateException if calling thread does not own it.
*/
public synchronized void release() {
Thread thread = Thread.currentThread();
if (owner == null) {
// trying to release unowned mutex
return;
}
if (thread == owner) {
// releasing mutex
owner = null;
this.notify();
return;
}
}
}