@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.370

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,599 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ /*
29
+ * Feature-test macros must precede every system header, including the
30
+ * ones runtime_internal.h includes, so they come before it here. This
31
+ * is the one runtime translation unit that asks for operating-system
32
+ * interfaces beyond C11.
33
+ */
34
+ #if defined(__linux__)
35
+ #define _GNU_SOURCE
36
+ #elif defined(__APPLE__)
37
+ #define _DARWIN_C_SOURCE
38
+ #endif
39
+
40
+ #include "runtime_internal.h"
41
+
42
+ #include <errno.h>
43
+ #include <stdlib.h>
44
+ #include <time.h>
45
+
46
+ #if defined(__linux__) || defined(__APPLE__)
47
+ #include <fcntl.h>
48
+ #include <limits.h>
49
+ #include <poll.h>
50
+ #include <unistd.h>
51
+ #endif
52
+ #if defined(__linux__)
53
+ #include <sys/eventfd.h>
54
+ #elif defined(__APPLE__)
55
+ #include <sys/event.h>
56
+ #endif
57
+
58
+ /*
59
+ * The platform clock adapter that ADR 0025 selects for the supported
60
+ * targets.
61
+ *
62
+ * Linux reads CLOCK_MONOTONIC and waits in `ppoll` on an eventfd. macOS
63
+ * reads CLOCK_UPTIME_RAW, which like Linux CLOCK_MONOTONIC does not
64
+ * advance while the system sleeps, and waits in `kevent` on an
65
+ * EVFILT_USER event. Both read epoch real time from CLOCK_REALTIME. When
66
+ * the primary wakeup facility cannot be created, the adapter falls back
67
+ * to a nonblocking self-pipe waited on with `poll`; when no descriptor
68
+ * can be created at all, it sleeps toward the deadline and reports
69
+ * cross-thread wakeup as absent. A real-time failure falls back to the
70
+ * C11 `time` function at whole-second resolution. A missing monotonic
71
+ * clock has no fallback: substituting real time for it would let a
72
+ * wall-clock adjustment move deadlines, so the scheduler reports it as an
73
+ * owned failure instead.
74
+ *
75
+ * The adapter never starts a thread, never polls without blocking, and
76
+ * holds nothing that outlives `close`. Any other operating system gets
77
+ * no monotonic clock and a C11 real-time read, so a timer there fails
78
+ * with an owned diagnostic rather than misbehaving.
79
+ */
80
+
81
+ typedef enum {
82
+ PLATFORM_WAKEUP_NONE = 0,
83
+ PLATFORM_WAKEUP_PRIMARY = 1,
84
+ PLATFORM_WAKEUP_PIPE = 2,
85
+ } PlatformWakeup;
86
+
87
+ typedef struct {
88
+ unsigned restrictions;
89
+ /*
90
+ * Whether real time still comes from CLOCK_REALTIME. It is chosen at
91
+ * open and cleared the first time that read fails, so the capability
92
+ * record names the facility that actually answers. Only the realm's
93
+ * own thread reads real time, so no waker races this field.
94
+ */
95
+ bool real_time_primary;
96
+ PlatformWakeup wakeup;
97
+ int wait_descriptor;
98
+ int wake_descriptor;
99
+ } PlatformClock;
100
+
101
+ #define PLATFORM_RESTRICTION_MASK \
102
+ (OSEO_CLOCK_RESTRICT_MONOTONIC | OSEO_CLOCK_RESTRICT_REAL_TIME | \
103
+ OSEO_CLOCK_RESTRICT_PRIMARY_WAKEUP | OSEO_CLOCK_RESTRICT_PIPE_WAKEUP)
104
+ #define PLATFORM_UNALLOCATED(bits) \
105
+ {(bits), false, PLATFORM_WAKEUP_NONE, -1, -1}
106
+
107
+ /*
108
+ * The states used when the adapter cannot allocate its own, one per
109
+ * restriction set so an allocation failure never lifts a restriction.
110
+ * Each has no wakeup facility, which selects the sleep fallback. They are
111
+ * never written, so every realm that reaches one may share it, and
112
+ * `close` has nothing to release for them.
113
+ */
114
+ static PlatformClock unallocated_clocks[PLATFORM_RESTRICTION_MASK + 1u] = {
115
+ PLATFORM_UNALLOCATED(0u),
116
+ PLATFORM_UNALLOCATED(1u),
117
+ PLATFORM_UNALLOCATED(2u),
118
+ PLATFORM_UNALLOCATED(3u),
119
+ PLATFORM_UNALLOCATED(4u),
120
+ PLATFORM_UNALLOCATED(5u),
121
+ PLATFORM_UNALLOCATED(6u),
122
+ PLATFORM_UNALLOCATED(7u),
123
+ PLATFORM_UNALLOCATED(8u),
124
+ PLATFORM_UNALLOCATED(9u),
125
+ PLATFORM_UNALLOCATED(10u),
126
+ PLATFORM_UNALLOCATED(11u),
127
+ PLATFORM_UNALLOCATED(12u),
128
+ PLATFORM_UNALLOCATED(13u),
129
+ PLATFORM_UNALLOCATED(14u),
130
+ PLATFORM_UNALLOCATED(15u),
131
+ };
132
+
133
+ static bool is_unallocated(const PlatformClock *clock) {
134
+ for (size_t index = 0u; index <= PLATFORM_RESTRICTION_MASK; index += 1u) {
135
+ if (clock == &unallocated_clocks[index]) return true;
136
+ }
137
+ return false;
138
+ }
139
+
140
+ #if defined(__linux__) || defined(__APPLE__)
141
+
142
+ #define PLATFORM_NANOSECONDS_PER_SECOND UINT64_C(1000000000)
143
+ #define PLATFORM_NANOSECONDS_PER_MILLISECOND UINT64_C(1000000)
144
+
145
+ #if defined(__APPLE__)
146
+ #define PLATFORM_MONOTONIC_CLOCK CLOCK_UPTIME_RAW
147
+ #define PLATFORM_MONOTONIC_NAME "clock_gettime(CLOCK_UPTIME_RAW)"
148
+ #define PLATFORM_PRIMARY_WAIT_NAME "kevent"
149
+ #define PLATFORM_PRIMARY_WAKEUP_NAME "EVFILT_USER"
150
+ #define PLATFORM_BACKEND_NAME "macos-kqueue"
151
+ #else
152
+ #define PLATFORM_MONOTONIC_CLOCK CLOCK_MONOTONIC
153
+ #define PLATFORM_MONOTONIC_NAME "clock_gettime(CLOCK_MONOTONIC)"
154
+ #define PLATFORM_PRIMARY_WAIT_NAME "ppoll"
155
+ #define PLATFORM_PRIMARY_WAKEUP_NAME "eventfd"
156
+ #define PLATFORM_BACKEND_NAME "linux-eventfd"
157
+ #endif
158
+
159
+ static PlatformClock *platform_clock(void *state) {
160
+ return state == NULL ? &unallocated_clocks[0] : (PlatformClock *)state;
161
+ }
162
+
163
+ static bool timespec_nanoseconds(const struct timespec *time, uint64_t *out) {
164
+ if (time->tv_sec < 0 || time->tv_nsec < 0 ||
165
+ (uint64_t)time->tv_nsec >= PLATFORM_NANOSECONDS_PER_SECOND ||
166
+ (uint64_t)time->tv_sec >
167
+ (UINT64_MAX - (uint64_t)time->tv_nsec) /
168
+ PLATFORM_NANOSECONDS_PER_SECOND) {
169
+ return false;
170
+ }
171
+ *out = (uint64_t)time->tv_sec * PLATFORM_NANOSECONDS_PER_SECOND +
172
+ (uint64_t)time->tv_nsec;
173
+ return true;
174
+ }
175
+
176
+ static struct timespec nanoseconds_timespec(uint64_t nanoseconds) {
177
+ struct timespec time;
178
+ uint64_t seconds = nanoseconds / PLATFORM_NANOSECONDS_PER_SECOND;
179
+ /*
180
+ * time_t is at least 64 bits wide on every supported target, where
181
+ * UINT64_MAX nanoseconds is about 584 years; the clamp only keeps a
182
+ * narrower time_t from wrapping.
183
+ */
184
+ if (seconds > (uint64_t)INT32_MAX && sizeof(time_t) < 8u) {
185
+ seconds = (uint64_t)INT32_MAX;
186
+ }
187
+ time.tv_sec = (time_t)seconds;
188
+ time.tv_nsec = (long)(nanoseconds % PLATFORM_NANOSECONDS_PER_SECOND);
189
+ return time;
190
+ }
191
+
192
+ static bool platform_monotonic(void *state, uint64_t *nanoseconds) {
193
+ PlatformClock *clock = platform_clock(state);
194
+ if ((clock->restrictions & OSEO_CLOCK_RESTRICT_MONOTONIC) != 0u) {
195
+ return false;
196
+ }
197
+ struct timespec now;
198
+ if (clock_gettime(PLATFORM_MONOTONIC_CLOCK, &now) != 0) return false;
199
+ return timespec_nanoseconds(&now, nanoseconds);
200
+ }
201
+
202
+ /*
203
+ * The shared unallocated states are never written, so for them the selection
204
+ * is observed again on each call instead of being remembered.
205
+ */
206
+ static bool platform_real_time_primary(const PlatformClock *clock) {
207
+ if (!is_unallocated(clock)) return clock->real_time_primary;
208
+ struct timespec now;
209
+ return (clock->restrictions & OSEO_CLOCK_RESTRICT_REAL_TIME) == 0u &&
210
+ clock_gettime(CLOCK_REALTIME, &now) == 0;
211
+ }
212
+
213
+ static bool platform_real_time(void *state, double *milliseconds) {
214
+ PlatformClock *clock = platform_clock(state);
215
+ if (platform_real_time_primary(clock)) {
216
+ struct timespec now;
217
+ if (clock_gettime(CLOCK_REALTIME, &now) == 0) {
218
+ /*
219
+ * Whole milliseconds, rounded toward negative infinity for a
220
+ * reading before the epoch, as a time value requires.
221
+ */
222
+ long long seconds = (long long)now.tv_sec;
223
+ long long millis = (long long)(now.tv_nsec / 1000000L);
224
+ *milliseconds = (double)seconds * 1000.0 + (double)millis;
225
+ return true;
226
+ }
227
+ if (!is_unallocated(clock)) clock->real_time_primary = false;
228
+ }
229
+ time_t seconds = time(NULL);
230
+ if (seconds == (time_t)-1) return false;
231
+ *milliseconds = (double)seconds * 1000.0;
232
+ return true;
233
+ }
234
+
235
+ static bool remaining_until(
236
+ uint64_t deadline,
237
+ uint64_t *remaining,
238
+ bool *expired
239
+ ) {
240
+ uint64_t now = 0u;
241
+ struct timespec reading;
242
+ if (clock_gettime(PLATFORM_MONOTONIC_CLOCK, &reading) != 0 ||
243
+ !timespec_nanoseconds(&reading, &now)) {
244
+ return false;
245
+ }
246
+ *expired = now >= deadline;
247
+ *remaining = *expired ? 0u : deadline - now;
248
+ return true;
249
+ }
250
+
251
+ static void drain_descriptor(int descriptor) {
252
+ unsigned char buffer[64];
253
+ for (;;) {
254
+ ssize_t count = read(descriptor, buffer, sizeof(buffer));
255
+ if (count > 0) continue;
256
+ if (count < 0 && errno == EINTR) continue;
257
+ break;
258
+ }
259
+ }
260
+
261
+ static OseoClockWaitResult wait_primary(
262
+ PlatformClock *clock,
263
+ bool has_deadline,
264
+ uint64_t remaining
265
+ ) {
266
+ struct timespec timeout = nanoseconds_timespec(remaining);
267
+ #if defined(__APPLE__)
268
+ struct kevent event;
269
+ int count = kevent(
270
+ clock->wait_descriptor,
271
+ NULL,
272
+ 0,
273
+ &event,
274
+ 1,
275
+ has_deadline ? &timeout : NULL
276
+ );
277
+ #else
278
+ struct pollfd descriptor = {clock->wait_descriptor, POLLIN, 0};
279
+ int count = ppoll(&descriptor, 1u, has_deadline ? &timeout : NULL, NULL);
280
+ if (count > 0) drain_descriptor(clock->wait_descriptor);
281
+ #endif
282
+ if (count == 0) return OSEO_CLOCK_WAIT_DEADLINE;
283
+ if (count > 0 || errno == EINTR) return OSEO_CLOCK_WAIT_WAKEUP;
284
+ return OSEO_CLOCK_WAIT_FAILED;
285
+ }
286
+
287
+ static OseoClockWaitResult wait_pipe(
288
+ PlatformClock *clock,
289
+ bool has_deadline,
290
+ uint64_t remaining
291
+ ) {
292
+ int timeout = -1;
293
+ if (has_deadline) {
294
+ uint64_t whole = remaining / PLATFORM_NANOSECONDS_PER_MILLISECOND;
295
+ /* Round up so that poll never returns before the deadline. */
296
+ uint64_t milliseconds = whole +
297
+ (remaining % PLATFORM_NANOSECONDS_PER_MILLISECOND == 0u ? 0u : 1u);
298
+ timeout = milliseconds > (uint64_t)INT_MAX
299
+ ? INT_MAX
300
+ : (int)milliseconds;
301
+ }
302
+ struct pollfd descriptor = {clock->wait_descriptor, POLLIN, 0};
303
+ int count = poll(&descriptor, 1u, timeout);
304
+ if (count > 0) drain_descriptor(clock->wait_descriptor);
305
+ if (count == 0) return OSEO_CLOCK_WAIT_DEADLINE;
306
+ if (count > 0 || errno == EINTR) return OSEO_CLOCK_WAIT_WAKEUP;
307
+ return OSEO_CLOCK_WAIT_FAILED;
308
+ }
309
+
310
+ static OseoClockWaitResult wait_sleep(uint64_t deadline, uint64_t remaining) {
311
+ #if defined(__APPLE__)
312
+ (void)deadline;
313
+ struct timespec interval = nanoseconds_timespec(remaining);
314
+ if (nanosleep(&interval, NULL) == 0) return OSEO_CLOCK_WAIT_DEADLINE;
315
+ return errno == EINTR ? OSEO_CLOCK_WAIT_WAKEUP : OSEO_CLOCK_WAIT_FAILED;
316
+ #else
317
+ (void)remaining;
318
+ struct timespec absolute = nanoseconds_timespec(deadline);
319
+ int error = clock_nanosleep(
320
+ PLATFORM_MONOTONIC_CLOCK,
321
+ TIMER_ABSTIME,
322
+ &absolute,
323
+ NULL
324
+ );
325
+ if (error == 0) return OSEO_CLOCK_WAIT_DEADLINE;
326
+ return error == EINTR ? OSEO_CLOCK_WAIT_WAKEUP : OSEO_CLOCK_WAIT_FAILED;
327
+ #endif
328
+ }
329
+
330
+ static OseoClockWaitResult platform_wait(
331
+ void *state,
332
+ bool has_deadline,
333
+ uint64_t deadline
334
+ ) {
335
+ PlatformClock *clock = platform_clock(state);
336
+ uint64_t remaining = 0u;
337
+ if (has_deadline) {
338
+ bool expired = false;
339
+ if ((clock->restrictions & OSEO_CLOCK_RESTRICT_MONOTONIC) != 0u ||
340
+ !remaining_until(deadline, &remaining, &expired)) {
341
+ return OSEO_CLOCK_WAIT_FAILED;
342
+ }
343
+ if (expired) return OSEO_CLOCK_WAIT_DEADLINE;
344
+ }
345
+ switch (clock->wakeup) {
346
+ case PLATFORM_WAKEUP_PRIMARY:
347
+ return wait_primary(clock, has_deadline, remaining);
348
+ case PLATFORM_WAKEUP_PIPE:
349
+ return wait_pipe(clock, has_deadline, remaining);
350
+ case PLATFORM_WAKEUP_NONE:
351
+ default:
352
+ /* Nothing could end an indefinite wait without a wakeup facility. */
353
+ if (!has_deadline) return OSEO_CLOCK_WAIT_FAILED;
354
+ return wait_sleep(deadline, remaining);
355
+ }
356
+ }
357
+
358
+ static bool platform_wake(void *state) {
359
+ PlatformClock *clock = platform_clock(state);
360
+ switch (clock->wakeup) {
361
+ case PLATFORM_WAKEUP_PRIMARY: {
362
+ #if defined(__APPLE__)
363
+ struct kevent event;
364
+ EV_SET(&event, 1u, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
365
+ for (;;) {
366
+ if (kevent(clock->wake_descriptor, &event, 1, NULL, 0, NULL) == 0) {
367
+ return true;
368
+ }
369
+ if (errno != EINTR) return false;
370
+ }
371
+ #else
372
+ uint64_t one = 1u;
373
+ for (;;) {
374
+ ssize_t count = write(clock->wake_descriptor, &one, sizeof(one));
375
+ if (count == (ssize_t)sizeof(one)) return true;
376
+ /* A saturated counter already holds a pending wakeup. */
377
+ if (count < 0 && errno == EAGAIN) return true;
378
+ if (count < 0 && errno == EINTR) continue;
379
+ return false;
380
+ }
381
+ #endif
382
+ }
383
+ case PLATFORM_WAKEUP_PIPE: {
384
+ unsigned char byte = 1u;
385
+ for (;;) {
386
+ ssize_t count = write(clock->wake_descriptor, &byte, 1u);
387
+ if (count == 1) return true;
388
+ /* A full pipe already holds a pending wakeup. */
389
+ if (count < 0 && errno == EAGAIN) return true;
390
+ if (count < 0 && errno == EINTR) continue;
391
+ return false;
392
+ }
393
+ }
394
+ case PLATFORM_WAKEUP_NONE:
395
+ default:
396
+ return false;
397
+ }
398
+ }
399
+
400
+ static void platform_describe(
401
+ void *state,
402
+ OseoClockCapabilities *capabilities
403
+ ) {
404
+ PlatformClock *clock = platform_clock(state);
405
+ capabilities->backend = PLATFORM_BACKEND_NAME;
406
+ capabilities->monotonic =
407
+ (clock->restrictions & OSEO_CLOCK_RESTRICT_MONOTONIC) != 0u
408
+ ? NULL
409
+ : PLATFORM_MONOTONIC_NAME;
410
+ capabilities->real_time = platform_real_time_primary(clock)
411
+ ? "clock_gettime(CLOCK_REALTIME)"
412
+ : "time";
413
+ switch (clock->wakeup) {
414
+ case PLATFORM_WAKEUP_PRIMARY:
415
+ capabilities->wait = PLATFORM_PRIMARY_WAIT_NAME;
416
+ capabilities->wakeup = PLATFORM_PRIMARY_WAKEUP_NAME;
417
+ break;
418
+ case PLATFORM_WAKEUP_PIPE:
419
+ capabilities->wait = "poll";
420
+ capabilities->wakeup = "pipe";
421
+ break;
422
+ case PLATFORM_WAKEUP_NONE:
423
+ default:
424
+ #if defined(__APPLE__)
425
+ capabilities->wait = "nanosleep";
426
+ #else
427
+ capabilities->wait = "clock_nanosleep";
428
+ #endif
429
+ capabilities->wakeup = NULL;
430
+ break;
431
+ }
432
+ capabilities->fallback = clock->wakeup != PLATFORM_WAKEUP_PRIMARY ||
433
+ !platform_real_time_primary(clock);
434
+ }
435
+
436
+ static void close_descriptor(int descriptor) {
437
+ if (descriptor >= 0) (void)close(descriptor);
438
+ }
439
+
440
+ static void platform_close(void *state) {
441
+ PlatformClock *clock = platform_clock(state);
442
+ if (is_unallocated(clock)) return;
443
+ close_descriptor(clock->wait_descriptor);
444
+ if (clock->wake_descriptor != clock->wait_descriptor) {
445
+ close_descriptor(clock->wake_descriptor);
446
+ }
447
+ free(clock);
448
+ }
449
+
450
+ static bool open_primary(PlatformClock *clock) {
451
+ #if defined(__APPLE__)
452
+ int descriptor = kqueue();
453
+ if (descriptor < 0) return false;
454
+ struct kevent event;
455
+ EV_SET(&event, 1u, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, NULL);
456
+ if (kevent(descriptor, &event, 1, NULL, 0, NULL) != 0) {
457
+ (void)close(descriptor);
458
+ return false;
459
+ }
460
+ #else
461
+ int descriptor = eventfd(0u, EFD_CLOEXEC | EFD_NONBLOCK);
462
+ if (descriptor < 0) return false;
463
+ #endif
464
+ clock->wait_descriptor = descriptor;
465
+ clock->wake_descriptor = descriptor;
466
+ return true;
467
+ }
468
+
469
+ static bool configure_pipe_end(int descriptor) {
470
+ int descriptor_flags = fcntl(descriptor, F_GETFD);
471
+ int status_flags = fcntl(descriptor, F_GETFL);
472
+ return descriptor_flags >= 0 && status_flags >= 0 &&
473
+ fcntl(descriptor, F_SETFD, descriptor_flags | FD_CLOEXEC) == 0 &&
474
+ fcntl(descriptor, F_SETFL, status_flags | O_NONBLOCK) == 0;
475
+ }
476
+
477
+ static bool open_pipe(PlatformClock *clock) {
478
+ int descriptors[2];
479
+ if (pipe(descriptors) != 0) return false;
480
+ if (!configure_pipe_end(descriptors[0]) ||
481
+ !configure_pipe_end(descriptors[1])) {
482
+ (void)close(descriptors[0]);
483
+ (void)close(descriptors[1]);
484
+ return false;
485
+ }
486
+ clock->wait_descriptor = descriptors[0];
487
+ clock->wake_descriptor = descriptors[1];
488
+ return true;
489
+ }
490
+
491
+ static const OseoClockAdapter platform_adapter = {
492
+ platform_monotonic,
493
+ platform_real_time,
494
+ platform_wait,
495
+ platform_wake,
496
+ platform_describe,
497
+ platform_close,
498
+ };
499
+
500
+ static void select_facilities(PlatformClock *clock) {
501
+ struct timespec now;
502
+ clock->real_time_primary =
503
+ (clock->restrictions & OSEO_CLOCK_RESTRICT_REAL_TIME) == 0u &&
504
+ clock_gettime(CLOCK_REALTIME, &now) == 0;
505
+ if ((clock->restrictions & OSEO_CLOCK_RESTRICT_PRIMARY_WAKEUP) == 0u &&
506
+ open_primary(clock)) {
507
+ clock->wakeup = PLATFORM_WAKEUP_PRIMARY;
508
+ } else if ((clock->restrictions & OSEO_CLOCK_RESTRICT_PIPE_WAKEUP) == 0u &&
509
+ open_pipe(clock)) {
510
+ clock->wakeup = PLATFORM_WAKEUP_PIPE;
511
+ }
512
+ }
513
+
514
+ #else
515
+
516
+ static bool unsupported_monotonic(void *state, uint64_t *nanoseconds) {
517
+ (void)state;
518
+ (void)nanoseconds;
519
+ return false;
520
+ }
521
+
522
+ /*
523
+ * `time` is already the recorded real-time fallback, so the restriction
524
+ * that selects it on a supported target changes nothing here.
525
+ */
526
+ static bool unsupported_real_time(void *state, double *milliseconds) {
527
+ (void)state;
528
+ time_t seconds = time(NULL);
529
+ if (seconds == (time_t)-1) return false;
530
+ *milliseconds = (double)seconds * 1000.0;
531
+ return true;
532
+ }
533
+
534
+ static OseoClockWaitResult unsupported_wait(
535
+ void *state,
536
+ bool has_deadline,
537
+ uint64_t deadline
538
+ ) {
539
+ (void)state;
540
+ (void)has_deadline;
541
+ (void)deadline;
542
+ return OSEO_CLOCK_WAIT_FAILED;
543
+ }
544
+
545
+ static bool unsupported_wake(void *state) {
546
+ (void)state;
547
+ return false;
548
+ }
549
+
550
+ static void unsupported_describe(
551
+ void *state,
552
+ OseoClockCapabilities *capabilities
553
+ ) {
554
+ (void)state;
555
+ capabilities->backend = "unsupported";
556
+ capabilities->monotonic = NULL;
557
+ capabilities->real_time = "time";
558
+ capabilities->wait = NULL;
559
+ capabilities->wakeup = NULL;
560
+ capabilities->fallback = true;
561
+ }
562
+
563
+ static void unsupported_close(void *state) {
564
+ if (state != NULL && !is_unallocated((PlatformClock *)state)) free(state);
565
+ }
566
+
567
+ static const OseoClockAdapter platform_adapter = {
568
+ unsupported_monotonic,
569
+ unsupported_real_time,
570
+ unsupported_wait,
571
+ unsupported_wake,
572
+ unsupported_describe,
573
+ unsupported_close,
574
+ };
575
+
576
+ static void select_facilities(PlatformClock *clock) {
577
+ (void)clock;
578
+ }
579
+
580
+ #endif
581
+
582
+ const OseoClockAdapter *oseo_internal_platform_clock_open(
583
+ unsigned restrictions,
584
+ void **state
585
+ ) {
586
+ PlatformClock *clock = malloc(sizeof(*clock));
587
+ if (clock == NULL) {
588
+ *state = &unallocated_clocks[restrictions & PLATFORM_RESTRICTION_MASK];
589
+ return &platform_adapter;
590
+ }
591
+ *state = clock;
592
+ clock->restrictions = restrictions;
593
+ clock->real_time_primary = false;
594
+ clock->wakeup = PLATFORM_WAKEUP_NONE;
595
+ clock->wait_descriptor = -1;
596
+ clock->wake_descriptor = -1;
597
+ select_facilities(clock);
598
+ return &platform_adapter;
599
+ }