| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
package com.atlassian.core.util; |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
|
|
|
| 0% |
Uncovered Elements: 63 (63) |
Complexity: 18 |
Complexity Density: 0.46 |
|
| 45 |
|
public class Semaphore |
| 46 |
|
{ |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
protected long permits; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 57 |
0
|
public Semaphore(long initialPermits)... |
| 58 |
|
{ |
| 59 |
0
|
permits = initialPermits; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 66 |
0
|
public void acquire() throws InterruptedException... |
| 67 |
|
{ |
| 68 |
0
|
if (Thread.interrupted()) |
| 69 |
|
{ |
| 70 |
0
|
throw new InterruptedException(); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
0
|
synchronized (this) |
| 74 |
|
{ |
| 75 |
0
|
try |
| 76 |
|
{ |
| 77 |
0
|
while (permits <= 0) |
| 78 |
|
{ |
| 79 |
0
|
wait(); |
| 80 |
|
} |
| 81 |
0
|
--permits; |
| 82 |
|
} |
| 83 |
|
catch (InterruptedException ex) |
| 84 |
|
{ |
| 85 |
0
|
notify(); |
| 86 |
0
|
throw ex; |
| 87 |
|
} |
| 88 |
|
} |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 8 |
Complexity Density: 0.38 |
|
| 94 |
0
|
public boolean attempt(long msecs) throws InterruptedException... |
| 95 |
|
{ |
| 96 |
0
|
if (Thread.interrupted()) |
| 97 |
|
{ |
| 98 |
0
|
throw new InterruptedException(); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
0
|
synchronized (this) |
| 102 |
|
{ |
| 103 |
0
|
if (permits > 0) |
| 104 |
|
{ |
| 105 |
0
|
--permits; |
| 106 |
0
|
return true; |
| 107 |
|
} |
| 108 |
0
|
else if (msecs <= 0) |
| 109 |
|
{ |
| 110 |
0
|
return false; |
| 111 |
|
} |
| 112 |
|
else |
| 113 |
|
{ |
| 114 |
0
|
try |
| 115 |
|
{ |
| 116 |
0
|
long startTime = System.currentTimeMillis(); |
| 117 |
0
|
long waitTime = msecs; |
| 118 |
|
|
| 119 |
0
|
for (; ;) |
| 120 |
|
{ |
| 121 |
0
|
wait(waitTime); |
| 122 |
0
|
if (permits > 0) |
| 123 |
|
{ |
| 124 |
0
|
--permits; |
| 125 |
0
|
return true; |
| 126 |
|
} |
| 127 |
|
else |
| 128 |
|
{ |
| 129 |
0
|
waitTime = msecs - (System.currentTimeMillis() - startTime); |
| 130 |
0
|
if (waitTime <= 0) |
| 131 |
|
{ |
| 132 |
0
|
return false; |
| 133 |
|
} |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
catch (InterruptedException ex) |
| 138 |
|
{ |
| 139 |
0
|
notify(); |
| 140 |
0
|
throw ex; |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 149 |
0
|
public synchronized void release()... |
| 150 |
|
{ |
| 151 |
0
|
++permits; |
| 152 |
0
|
notify(); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
@exception |
| 166 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 167 |
0
|
public synchronized void release(long n)... |
| 168 |
|
{ |
| 169 |
0
|
if (n < 0) |
| 170 |
|
{ |
| 171 |
0
|
throw new IllegalArgumentException("Negative argument"); |
| 172 |
|
} |
| 173 |
|
|
| 174 |
0
|
permits += n; |
| 175 |
|
|
| 176 |
0
|
for (long i = 0; i < n; ++i) |
| 177 |
|
{ |
| 178 |
0
|
notify(); |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 187 |
0
|
public synchronized long permits()... |
| 188 |
|
{ |
| 189 |
0
|
return permits; |
| 190 |
|
} |
| 191 |
|
} |