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

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