aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_Clock.h
blob: 35c77fd7894cb4bb930dd884f85bdf420bd53ef0 (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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_Clock.h
 * @ingroup datastructures
 * @brief Generic API to a clock
 *
 * Interface over clock providers.  We provide two system clocks, a Wallclock that tracks the
 * real time clock and a monotonic clock that will not skew or go backwards.
 * @see parcClock_Monotonic()
 * and
 * @see parcClock_Wallclock()
 * Also provided is a counting clock.
 * @see parcClock_Counter()
 *
 * Below is a complete example of a simple custom clock that implements an atomic counter for
 * the time.  Each call to getTime or getTimeval will increment the counter.  This clock is
 * available as parcClock_Counter().
 *
 * @code
 *   typedef struct counter_clock {
 *      uint64_t counter;
 *   } _CounterClock;
 *
 *   parcObject_ExtendPARCObject(_CounterClock, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
 *   static parcObject_ImplementAcquire(_counterClock, _CounterClock);
 *
 *   static PARCClock *
 *   _counterClock_AcquireInterface(const PARCClock *clock)
 *   {
 *      _CounterClock *cc = (_CounterClock *) clock->closure;
 *      _counterClock_Acquire(cc);
 *      return (PARCClock *) clock;
 *   }
 *
 *   static void
 *   _counterClock_ReleaseInterface(PARCClock **clockPtr)
 *   {
 *      PARCClock *clock = *clockPtr;
 *      _CounterClock *cc = (_CounterClock *) clock->closure;
 *
 *      PARCReferenceCount refcount = parcObject_Release((void **) &cc);
 *      if (refcount == 0) {
 *         parcMemory_Deallocate((void **) clockPtr);
 *      } else {
 *         *clockPtr = NULL;
 *      }
 *   }
 *
 *   static void
 *   _counterClock_GetTimeval(const PARCClock *clock, struct timeval *output)
 *   {
 *      _CounterClock *cc = (_CounterClock *) clock->closure;
 *      uint64_t value = parcAtomicInteger_Uint64Increment(&cc->counter);
 *      // put 19 bits in the micro-seconds so it is never larger than 1E+6
 *      output->tv_sec = value >> 19;
 *      output->tv_usec = value & 0x7FFFF;
 *   }
 *
 *   static uint64_t
 *   _counterClock_GetTime(const PARCClock *clock)
 *   {
 *      _CounterClock *cc = (_CounterClock *) clock->closure;
 *      return parcAtomicInteger_Uint64Increment(&cc->counter);
 *   }
 *
 *   PARCClock *
 *   parcClock_Counter(void)
 *   {
 *      _CounterClock *cc = parcObject_CreateInstance(_CounterClock);
 *      cc->counter = 0;
 *
 *      PARCClock *clock = parcMemory_Allocate(sizeof(PARCClock));
 *      clock->closure = cc;
 *      clock->acquire = _counterClock_AcquireInterface;
 *      clock->release = _counterClock_ReleaseInterface;
 *      clock->getTime = _counterClock_GetTime;
 *      clock->getTimeval = _counterClock_GetTimeval;
 *      return clock;
 *   }
 * @encode
 *
 */

#ifndef PARC_parc_Clock_h
#define PARC_parc_Clock_h

#include <inttypes.h>
#include <sys/time.h>

struct parc_clock;
typedef struct parc_clock PARCClock;

struct parc_clock {
    /**
     * Opaque parameter set by the clock provider
     */
    void *closure;

    /**
     * Gets the clock time
     *
     * The resolution and epoch of the clock are determined by the clock provider
     *
     * @param [in] clock An allocated PARCClock
     *
     * @retval number The clock's time as a uint64_t
     *
     * Example:
     * @code
     * {
     *     PARCClock *clock = parcClock_Monotonic();
     *     uint64_t t = parcClock_GetTime(clock);
     *     parcClock_Release(&clock);
     * }
     * @endcode
     */
    uint64_t (*getTime)(const PARCClock *clock);

    /**
     * Gets the clock time as a struct timeval
     *
     * The resolution and epoch of the clock are determined by the clock provider.
     * There may be an arbitrary mapping to the struct timeval as per the clock
     * provider, so the units of 'seconds' and 'micro seconds' need to be interpreted
     * as per the clock provider.
     *
     * @param [in] clock An allocated PARCClock
     *
     * Example:
     * @code
     * <#example#>
     * @endcode
     */
    void (*getTimeval)(const PARCClock *clock, struct timeval *output);

    /**
     * Increase the number of references to a `PARCClock`.
     *
     * Note that new `PARCClock` is not created,
     * only that the given `PARCClock` reference count is incremented.
     * Discard the reference by invoking `parcClock_Release`.
     *
     * @param [in] clock A pointer to a `PARCClock` instance.
     *
     * @return The input `PARCClock` pointer.
     *
     * Example:
     * @code
     * {
     *     PARCClock *clock = parcClock_Counter();
     *     PARCClock *copy = parcClock_Acquire(clock);
     *     parcClock_Release(&copy);
     *     parcClock_Release(&clock);
     *
     * }
     * @endcode
     */
    PARCClock * (*acquire)(const PARCClock *clock);

    /**
     * Release a previously acquired reference to the specified instance,
     * decrementing the reference count for the instance.
     *
     * The pointer to the instance is set to NULL as a side-effect of this function.
     *
     * If the invocation causes the last reference to the instance to be released,
     * the instance is deallocated and the instance's implementation will perform
     * additional cleanup and release other privately held references.
     *
     * @param [in,out] clockPtr A pointer to a pointer to the instance to release.
     *
     * Example:
     * @code
     * {
     *     PARCClock *clock = parcClock_Counter();
     *     PARCClock *copy = parcClock_Acquire(clock);
     *     parcClock_Release(&copy);
     *     parcClock_Release(&clock);
     *
     * }
     * @endcode
     */

    void (*release)(PARCClock **clockPtr);
};

/**
 * A clock provider for the wall clock time
 *
 * This will use clock_gettime(CLOCK_REALTIME_COARSE) on linux or
 * host_get_clock_service with CALENDAR_CLOCK on Mac
 *
 * @retval non-null An allocated PARCClock, which must be released via parcClock_Release.
 *
 * Example:
 * @code
 * {
 *     PARCClock *clock = parcClock_Wallclock();
 *     uint64_t t = parcClock_GetTime(clock);
 *     parcClock_Release(&clock);
 * }
 * @endcode
 */
PARCClock *parcClock_Wallclock(void);

/**
 * A monotonic clock that will not normally adjust for time changes
 *
 * On linux, this uses the CLOCK_MONOTONIC_RAW.  On Darwin, it uses
 * the SYSTEM_CLOCK from <mach/mach_time.h>.
 *
 * @retval non-null An allocated PARCClock, which must be released via parcClock_Release.
 *
 * Example:
 * @code
 * {
 *     PARCClock *clock = parcClock_Monotonic();
 *     uint64_t t = parcClock_GetTime(clock);
 *     parcClock_Release(&clock);
 * }
 * @endcode
 */
PARCClock *parcClock_Monotonic(void);


/**
 * The counter clock begins at 0 and increments for every call to getTime or getTimeval
 *
 * Each allocated counter clock will begin at zero.  Copies made via parcClock_Acquire() will
 * share the same counter and use atomic updates.
 *
 * getTime() will return the counter.
 *
 * getTimeval() will return the lower 19 bits in tv_usec (so it does not overflow the concept
 * of micro-second) and the upper 45 bits are in tv_sec.  On some platforms, that may overflow.
 *
 * @retval non-null An allocated PARCClock, which must be released via parcClock_Release.
 *
 * Example:
 * @code
 * {
 *     PARCClock *clock = parcClock_Counter();
 *     uint64_t t = parcClock_GetTime(clock);
 *     parcClock_Release(&clock);
 * }
 * @endcode
 */
PARCClock *parcClock_Counter(void);

/**
 * Returns the clock provider's idea of the current time as a uint64
 *
 * Returns the clock provider's idea of the current time, which may or
 * may not be a wall clock time.
 *
 * @param [in] clock A clock provider
 *
 * @retval number The current time (depends on provider)
 *
 * Example:
 * @code
 * {
 *     PARCClock *clock = parcClock_Counter();
 *     uint64_t t = parcClock_GetTime(clock);
 *     parcClock_Release(&clock);
 * }
 * @endcode
 */
uint64_t parcClock_GetTime(const PARCClock *clock);

/**
 * Returns the clock provider's idea of the current time as a timeval
 *
 * Returns the clock provider's idea of the current time, which may or
 * may not be a wall clock time.
 *
 * @param [in] clock A clock provider
 * @param [in] output The structure to fill in with the current time
 *
 * Example:
 * @code
 * {
 *     struct timeval t;
 *     PARCClock *clock = parcClock_Counter();
 *     parcClock_GetTimeval(clock, &t);
 *     parcClock_Release(&clock);
 * }
 * @endcode
 */
void parcClock_GetTimeval(const PARCClock *clock, struct timeval *output);

/**
 * Increase the number of references to a `PARCClock`.
 *
 * Note that new `PARCClock` is not created,
 * only that the given `PARCClock` reference count is incremented.
 * Discard the reference by invoking `parcClock_Release`.
 *
 * @param [in] clock A pointer to a `PARCClock` instance.
 *
 * @return The input `PARCClock` pointer.
 *
 * Example:
 * @code
 * {
 *     PARCClock *clock = parcClock_Counter();
 *     PARCClock *copy = parcClock_Acquire(clock);
 *     parcClock_Release(&copy);
 *     parcClock_Release(&clock);
 *
 * }
 * @endcode
 */
PARCClock *parcClock_Acquire(const PARCClock *clock);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] clockPtr A pointer to a pointer to the instance to release.
 *
 * Example:
 * @code
 * {
 *     PARCClock *clock = parcClock_Counter();
 *     PARCClock *copy = parcClock_Acquire(clock);
 *     parcClock_Release(&copy);
 *     parcClock_Release(&clock);
 *
 * }
 * @endcode
 */
void parcClock_Release(PARCClock **clockPtr);
#endif // PARC_parc_Clock_h