GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
Clock.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020-2023 Gothel Software e.K.
4 * Copyright (c) 2020-2023 JogAmp Community.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25package com.jogamp.common.os;
26
27import java.time.Instant;
28
29public class Clock {
30 private static final Instant t0;
31 static {
32 Platform.initSingleton(); // loads native gluegen_rt library
33 {
34 final long[/*2*/] val = { 0, 0 };
35 if( getMonotonicStartupTimeImpl(val) ) {
36 t0 = Instant.ofEpochSecond(val[0], val[1]);
37 } else {
38 t0 = Instant.EPOCH;
39 }
40 }
41 }
42
43 /**
44 * Returns current monotonic time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
45 * <p>
46 * Returned timespec is passing machine precision and range of the underlying native API.
47 * </p>
48 * <p>
49 * Monotonic time shall be used for high-performance measurements of durations,
50 * since the underlying OS shall support fast calls.
51 * </p>
52 * <p>
53 * Note that {@link #currentNanos()} and {@link #getMonotonicNanos()}
54 * perform much better than this method, since they only return one long nanosecond value
55 * since module startup. <br/>
56 * The implementation of this method needs to write two long values into an array.
57 * </p>
58 * @see #getMonotonicStartupTime()
59 * @see #currentNanos()
60 * @see #getMonotonicNanos()
61 * @see #getWallClockTime()
62 */
63 public static Instant getMonotonicTime() {
64 final long[/*2*/] val = { 0, 0 };
65 if( getMonotonicTimeImpl(val) ) {
66 return Instant.ofEpochSecond(val[0], val[1]);
67 } else {
68 return Instant.EPOCH;
69 }
70 }
71 private static native boolean getMonotonicTimeImpl(final long[/*2*/] val);
72
73 /**
74 * Returns current wall-clock real-time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
75 * <p>
76 * Returned Instant is passing machine precision and range of the underlying native API.
77 * </p>
78 * <p>
79 * Wall-Clock time shall be used for accurate measurements of the actual time only,
80 * since the underlying OS unlikely supports fast calls.
81 * </p>
82 * @see #getMonotonicStartupTime()
83 * @see #currentNanos()
84 * @see #getMonotonicNanos()
85 * @see #getMonotonicTime()
86 */
87 public static Instant getWallClockTime() {
88 final long[/*2*/] val = { 0, 0 };
89 if( getWallClockTimeImpl(val) ) {
90 return Instant.ofEpochSecond(val[0], val[1]);
91 } else {
92 return Instant.EPOCH;
93 }
94 }
95 private static native boolean getWallClockTimeImpl(final long[/*2*/] val);
96
97 /**
98 * Returns the monotonic startup time since module startup as used in {@link #currentNanos()} and {@link #getMonotonicNanos()}.
99 * @see #currentNanos()
100 * @see #getMonotonicNanos()
101 */
102 public static Instant getMonotonicStartupTime() { return t0; }
103 private static native boolean getMonotonicStartupTimeImpl(final long[/*2*/] val);
104
105 /**
106 * Returns current monotonic nanoseconds since start of this application.
107 * <p>
108 * Monotonic time shall be used for high-performance measurements of durations,
109 * since the underlying OS shall support fast calls.
110 * </p>
111 * <p>
112 * The returned nanoseconds are counted not from Unix Epoch but start of this module,
113 * hence it lasts for 9'223'372'036 seconds or 292 years using the 64-bit type `long`.
114 * </p>
115 * <p>
116 * Method name doesn't include the term `Time` intentionally,
117 * since the returned value represent the nanoseconds duration since module start.
118 * </p>
119 * @see #getMonotonicStartupTime()
120 * @see #getMonotonicNanos()
121 */
122 public static native long currentNanos();
123
124 /**
125 * Returns the Instant presentation of monotonic {@link #currentNanos()}.
126 * <p>
127 * Monotonic time shall be used for high-performance measurements of durations,
128 * since the underlying OS shall support fast calls.
129 * </p>
130 * <p>
131 * The returned nanoseconds are counted not from Unix Epoch but start of this module,
132 * hence it lasts for 9'223'372'036 seconds or 292 years using the 64-bit type `long`.
133 * </p>
134 * <p>
135 * Method name doesn't include the term `Time` intentionally,
136 * since the returned value represent the nanoseconds duration since module start.
137 * </p>
138 * @see #getMonotonicStartupTime()
139 * @see #currentNanos()
140 */
141 public static Instant getMonotonicNanos() {
142 final long nanos = currentNanos();
143 return Instant.ofEpochSecond(nanos/1000000000L, nanos%1000000000L);
144 }
145
146 /**
147 * Returns current monotonic milliseconds since start of this application.
148 * <p>
149 * Monotonic time shall be used for high-performance measurements of durations,
150 * since the underlying OS shall support fast calls.
151 * </p>
152 * @see #getMonotonicStartupTime()
153 * @see #currentNanos()
154 * @see #getMonotonicNanos()
155 */
156 public static native long currentMillis();
157
158 /**
159 * Returns the unix based current monotonic time in milliseconds.
160 * <p>
161 * Monotonic time shall be used for high-performance measurements of durations,
162 * since the underlying OS shall support fast calls.
163 * </p>
164 * @see #getMonotonicStartupTime()
165 * @see #currentNanos()
166 * @see #getMonotonicNanos()
167 */
168 public static native long currentTimeMillis();
169
170 /**
171 * Returns current wall-clock system `time of day` in seconds since Unix Epoch
172 * `00:00:00 UTC on 1 January 1970`.
173 *
174 * @see #getWallClockTime()
175 * @see #getMonotonicTime()
176 * @see #currentNanos()
177 * @see #getMonotonicNanos()
178 */
179 public static native long wallClockSeconds();
180}
static native long currentNanos()
Returns current monotonic nanoseconds since start of this application.
static Instant getMonotonicStartupTime()
Returns the monotonic startup time since module startup as used in currentNanos() and getMonotonicNan...
Definition: Clock.java:102
static Instant getMonotonicTime()
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
Definition: Clock.java:63
static Instant getMonotonicNanos()
Returns the Instant presentation of monotonic currentNanos().
Definition: Clock.java:141
static Instant getWallClockTime()
Returns current wall-clock real-time since Unix Epoch 00:00:00 UTC on 1970-01-01.
Definition: Clock.java:87
static native long currentTimeMillis()
Returns the unix based current monotonic time in milliseconds.
static native long wallClockSeconds()
Returns current wall-clock system time of day in seconds since Unix Epoch 00:00:00 UTC on 1 January 1...
static native long currentMillis()
Returns current monotonic milliseconds since start of this application.
Utility class for querying platform specific properties.
Definition: Platform.java:58
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359