blob: 4e079f08b5e868cea97e2419188bdd0dd672260d (
plain)
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
|
/*
* Copyright (C) 2016, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.util.time;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
/**
* A {@link org.eclipse.jgit.util.time.MonotonicClock} based on
* {@code System.currentTimeMillis}.
*
* @since 4.6
*/
public class MonotonicSystemClock implements MonotonicClock {
private static final AtomicLong before = new AtomicLong();
private static long nowMicros() {
long now = MILLISECONDS.toMicros(System.currentTimeMillis());
for (;;) {
long o = before.get();
long n = Math.max(o + 1, now);
if (before.compareAndSet(o, n)) {
return n;
}
}
}
@Override
public ProposedTimestamp propose() {
final long u = nowMicros();
return new ProposedTimestamp() {
@Override
public long read(TimeUnit unit) {
return unit.convert(u, MICROSECONDS);
}
@Override
public void blockUntil(Duration maxWait) {
// Assume system clock never goes backwards.
}
};
}
}
|