@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.
- package/LICENSE +674 -0
- package/MIT-LICENSE.txt +21 -0
- package/README.md +28 -2
- package/UNICODE-LICENSE.txt +39 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +269 -0
- package/dist/index.js.map +1 -0
- package/native/oseo_runtime.h +2618 -0
- package/native/runtime_agent.c +1717 -0
- package/native/runtime_arguments.c +379 -0
- package/native/runtime_array.c +5766 -0
- package/native/runtime_array_buffer.c +1748 -0
- package/native/runtime_async_generator.c +972 -0
- package/native/runtime_atomics.c +1283 -0
- package/native/runtime_bigint.c +1387 -0
- package/native/runtime_bigint_object.c +626 -0
- package/native/runtime_binding.c +1029 -0
- package/native/runtime_boolean.c +320 -0
- package/native/runtime_clock.c +181 -0
- package/native/runtime_clock_posix.c +599 -0
- package/native/runtime_core.c +488 -0
- package/native/runtime_data_view.c +1135 -0
- package/native/runtime_date.c +1997 -0
- package/native/runtime_descriptor.c +1104 -0
- package/native/runtime_disposal.c +1667 -0
- package/native/runtime_enumeration.c +636 -0
- package/native/runtime_error.c +997 -0
- package/native/runtime_event_loop.c +653 -0
- package/native/runtime_function.c +3667 -0
- package/native/runtime_generator.c +1120 -0
- package/native/runtime_internal.h +5578 -0
- package/native/runtime_iterator.c +5379 -0
- package/native/runtime_json.c +1992 -0
- package/native/runtime_map.c +1563 -0
- package/native/runtime_math.c +918 -0
- package/native/runtime_memory.c +1365 -0
- package/native/runtime_module.c +884 -0
- package/native/runtime_number.c +2077 -0
- package/native/runtime_object.c +423 -0
- package/native/runtime_object_builtin.c +3901 -0
- package/native/runtime_primitive.c +2268 -0
- package/native/runtime_process.c +744 -0
- package/native/runtime_promise.c +3378 -0
- package/native/runtime_property.c +815 -0
- package/native/runtime_proxy.c +1557 -0
- package/native/runtime_realm.c +435 -0
- package/native/runtime_reflect.c +648 -0
- package/native/runtime_regexp.c +3519 -0
- package/native/runtime_regexp_matcher.c +3559 -0
- package/native/runtime_regexp_symbol.c +1495 -0
- package/native/runtime_set.c +1775 -0
- package/native/runtime_string.c +2886 -0
- package/native/runtime_string_match.c +1185 -0
- package/native/runtime_symbol.c +1010 -0
- package/native/runtime_typed_array.c +5137 -0
- package/native/runtime_unicode_tables.h +14670 -0
- package/native/runtime_uri.c +700 -0
- package/native/runtime_weak_collection.c +1299 -0
- package/package.json +38 -3
|
@@ -0,0 +1,1717 @@
|
|
|
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. Beside
|
|
31
|
+
* runtime_clock_posix.c, this is the one runtime translation unit that
|
|
32
|
+
* asks for operating-system interfaces beyond C11: POSIX threads.
|
|
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 <math.h>
|
|
43
|
+
#include <stdio.h>
|
|
44
|
+
#include <stdlib.h>
|
|
45
|
+
#include <string.h>
|
|
46
|
+
|
|
47
|
+
#if defined(__linux__) || defined(__APPLE__)
|
|
48
|
+
#include <pthread.h>
|
|
49
|
+
#define OSEO_AGENT_THREADS 1
|
|
50
|
+
typedef pthread_mutex_t AgentMutex;
|
|
51
|
+
typedef pthread_t AgentThread;
|
|
52
|
+
#else
|
|
53
|
+
#define OSEO_AGENT_THREADS 0
|
|
54
|
+
typedef int AgentMutex;
|
|
55
|
+
typedef int AgentThread;
|
|
56
|
+
#endif
|
|
57
|
+
|
|
58
|
+
/*
|
|
59
|
+
* Agent clusters and the test262 host object `$262` (ADR 0026).
|
|
60
|
+
*
|
|
61
|
+
* A program compiled for the test262 host makes its realm the main agent
|
|
62
|
+
* of a cluster. `$262.agent.start` evaluates one of the agent programs the
|
|
63
|
+
* compiler built ahead of time from the case's source templates, each in
|
|
64
|
+
* a new agent with its own realm, heap, clock adapter, and event loop, on
|
|
65
|
+
* a POSIX thread of its own. The agents share Shared Data Blocks, the
|
|
66
|
+
* WaiterList store, and the report queue, all of which live here.
|
|
67
|
+
*
|
|
68
|
+
* Agents of one cluster share one executing thread in the sense of
|
|
69
|
+
* ECMA-262's forward progress rules: exactly one agent, the one holding
|
|
70
|
+
* the turn, evaluates at a time. The turn passes only when the holder
|
|
71
|
+
* suspends (Atomics.wait, `$262.agent.sleep`, a broadcast, a start, or an
|
|
72
|
+
* event loop with nothing due), finishes, or reaches a yield point (every
|
|
73
|
+
* Atomics operation and `$262.agent.getReport`), where it goes to the
|
|
74
|
+
* longest-ready agent. One agent therefore always makes progress, and
|
|
75
|
+
* every access to shared memory happens on the thread that holds the turn
|
|
76
|
+
* after the mutex handoff that gave it the turn, so no two threads ever
|
|
77
|
+
* touch a block or any cluster state concurrently. A blocked agent waits
|
|
78
|
+
* in its own clock adapter, and whoever makes it ready wakes that adapter.
|
|
79
|
+
*
|
|
80
|
+
* All cluster state is guarded by `lock`. Heap values never cross agents:
|
|
81
|
+
* a waiter entry names its owner's heap waiter, which only the owner
|
|
82
|
+
* dereferences, a broadcast carries a referenced Shared Data Block and a
|
|
83
|
+
* scalar id, and a report carries copied UTF-16 units.
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
typedef enum {
|
|
87
|
+
AGENT_STARTING = 0,
|
|
88
|
+
AGENT_READY = 1,
|
|
89
|
+
AGENT_RUNNING = 2,
|
|
90
|
+
AGENT_BLOCKED = 3,
|
|
91
|
+
AGENT_FINISHED = 4,
|
|
92
|
+
} AgentState;
|
|
93
|
+
|
|
94
|
+
struct Agent;
|
|
95
|
+
|
|
96
|
+
/* One waiter of the cluster's WaiterList store. */
|
|
97
|
+
typedef struct AgentWaiter {
|
|
98
|
+
struct AgentWaiter *next;
|
|
99
|
+
/* The owner's list of waitAsync waiters another agent notified. */
|
|
100
|
+
struct AgentWaiter *resolved_next;
|
|
101
|
+
struct Agent *agent;
|
|
102
|
+
const OseoSharedBlock *block;
|
|
103
|
+
size_t byte_index;
|
|
104
|
+
/* The owner's OseoAtomicsWaiter for a waitAsync waiter. */
|
|
105
|
+
OseoValue waiter;
|
|
106
|
+
bool asynchronous;
|
|
107
|
+
bool listed;
|
|
108
|
+
bool notified;
|
|
109
|
+
} AgentWaiter;
|
|
110
|
+
|
|
111
|
+
typedef enum {
|
|
112
|
+
AGENT_ID_UNDEFINED = 0,
|
|
113
|
+
AGENT_ID_NUMBER = 1,
|
|
114
|
+
AGENT_ID_BIGINT = 2,
|
|
115
|
+
} AgentIdKind;
|
|
116
|
+
|
|
117
|
+
/* One broadcast waiting in an agent's inbox. */
|
|
118
|
+
typedef struct AgentBroadcast {
|
|
119
|
+
struct AgentBroadcast *next;
|
|
120
|
+
OseoSharedBlock *block;
|
|
121
|
+
AgentIdKind id_kind;
|
|
122
|
+
double id_number;
|
|
123
|
+
/* A BigInt id as its BigInt64 two's-complement bits. */
|
|
124
|
+
uint64_t id_bits;
|
|
125
|
+
} AgentBroadcast;
|
|
126
|
+
|
|
127
|
+
/* One `$262.agent.report` value, copied out of the reporting heap. */
|
|
128
|
+
typedef struct AgentReport {
|
|
129
|
+
struct AgentReport *next;
|
|
130
|
+
size_t length;
|
|
131
|
+
uint16_t units[];
|
|
132
|
+
} AgentReport;
|
|
133
|
+
|
|
134
|
+
/*
|
|
135
|
+
* One matched numeric hole: a decimal integer literal, or a BigInt literal
|
|
136
|
+
* when `bigint` is set, whose digits create the value on each read.
|
|
137
|
+
*/
|
|
138
|
+
typedef struct {
|
|
139
|
+
double number;
|
|
140
|
+
char *digits;
|
|
141
|
+
bool bigint;
|
|
142
|
+
} AgentHole;
|
|
143
|
+
|
|
144
|
+
struct AgentCluster;
|
|
145
|
+
|
|
146
|
+
typedef struct Agent {
|
|
147
|
+
struct AgentCluster *cluster;
|
|
148
|
+
struct Agent *next;
|
|
149
|
+
struct Agent *ready_next;
|
|
150
|
+
/* The agent's realm, NULL until its thread has initialized it. */
|
|
151
|
+
OseoContext *context;
|
|
152
|
+
const OseoAgentProgram *program;
|
|
153
|
+
AgentHole *holes;
|
|
154
|
+
size_t hole_count;
|
|
155
|
+
AgentThread thread;
|
|
156
|
+
AgentState state;
|
|
157
|
+
bool main;
|
|
158
|
+
/* A blocked agent waits for a deadline of its own as well. */
|
|
159
|
+
bool has_deadline;
|
|
160
|
+
/* A blocked agent's event loop is idle, so incoming work ends it. */
|
|
161
|
+
bool wakes_on_work;
|
|
162
|
+
bool leaving;
|
|
163
|
+
size_t listed_async;
|
|
164
|
+
AgentWaiter *resolved_head;
|
|
165
|
+
AgentWaiter *resolved_tail;
|
|
166
|
+
AgentBroadcast *broadcast_head;
|
|
167
|
+
AgentBroadcast *broadcast_tail;
|
|
168
|
+
} Agent;
|
|
169
|
+
|
|
170
|
+
typedef struct AgentCluster {
|
|
171
|
+
AgentMutex lock;
|
|
172
|
+
Agent *main;
|
|
173
|
+
/* Every agent other than the main one, in start order. */
|
|
174
|
+
Agent *agents_head;
|
|
175
|
+
Agent *agents_tail;
|
|
176
|
+
Agent *running;
|
|
177
|
+
Agent *ready_head;
|
|
178
|
+
Agent *ready_tail;
|
|
179
|
+
AgentWaiter *waiters_head;
|
|
180
|
+
AgentWaiter *waiters_tail;
|
|
181
|
+
AgentReport *reports_head;
|
|
182
|
+
AgentReport *reports_tail;
|
|
183
|
+
/* Agents that have not yet retrieved the current broadcast. */
|
|
184
|
+
size_t broadcast_remaining;
|
|
185
|
+
const OseoAgentProgram *programs;
|
|
186
|
+
size_t program_count;
|
|
187
|
+
/* The adapter reading `$262.agent.monotonicNow` counts from. */
|
|
188
|
+
uint64_t origin;
|
|
189
|
+
/* The main context's policy, fixed before any agent starts. */
|
|
190
|
+
OseoUnhandledRejectionPolicy unhandled_rejection_policy;
|
|
191
|
+
} AgentCluster;
|
|
192
|
+
|
|
193
|
+
/* The stack each agent thread gets, which the main thread usually has. */
|
|
194
|
+
#define AGENT_STACK_SIZE ((size_t)8u << 20u)
|
|
195
|
+
#define AGENT_NANOSECONDS_PER_MILLISECOND 1000000.0
|
|
196
|
+
|
|
197
|
+
static Agent *agent_of(const OseoContext *context) {
|
|
198
|
+
return (Agent *)context->agent;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/*
|
|
202
|
+
* The two thread facilities the cluster needs, over POSIX threads where the
|
|
203
|
+
* target has them. A target without them keeps every lock a no-op and
|
|
204
|
+
* refuses to start a thread, so `$262.agent.start` reports an owned failure
|
|
205
|
+
* and no second agent ever exists to need the lock.
|
|
206
|
+
*/
|
|
207
|
+
#if OSEO_AGENT_THREADS
|
|
208
|
+
static bool agent_mutex_init(AgentMutex *mutex) {
|
|
209
|
+
return pthread_mutex_init(mutex, NULL) == 0;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
static void agent_mutex_destroy(AgentMutex *mutex) {
|
|
213
|
+
(void)pthread_mutex_destroy(mutex);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static void agent_mutex_lock(AgentMutex *mutex) {
|
|
217
|
+
(void)pthread_mutex_lock(mutex);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
static void agent_mutex_unlock(AgentMutex *mutex) {
|
|
221
|
+
(void)pthread_mutex_unlock(mutex);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
static bool agent_thread_start(
|
|
225
|
+
AgentThread *thread,
|
|
226
|
+
void *(*run)(void *),
|
|
227
|
+
void *argument
|
|
228
|
+
) {
|
|
229
|
+
pthread_attr_t attributes;
|
|
230
|
+
if (pthread_attr_init(&attributes) != 0) return false;
|
|
231
|
+
(void)pthread_attr_setstacksize(&attributes, AGENT_STACK_SIZE);
|
|
232
|
+
int created = pthread_create(thread, &attributes, run, argument);
|
|
233
|
+
(void)pthread_attr_destroy(&attributes);
|
|
234
|
+
return created == 0;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static void agent_thread_join(AgentThread thread) {
|
|
238
|
+
(void)pthread_join(thread, NULL);
|
|
239
|
+
}
|
|
240
|
+
#else
|
|
241
|
+
static bool agent_mutex_init(AgentMutex *mutex) {
|
|
242
|
+
*mutex = 0;
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
static void agent_mutex_destroy(AgentMutex *mutex) {
|
|
247
|
+
(void)mutex;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
static void agent_mutex_lock(AgentMutex *mutex) {
|
|
251
|
+
(void)mutex;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
static void agent_mutex_unlock(AgentMutex *mutex) {
|
|
255
|
+
(void)mutex;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
static bool agent_thread_start(
|
|
259
|
+
AgentThread *thread,
|
|
260
|
+
void *(*run)(void *),
|
|
261
|
+
void *argument
|
|
262
|
+
) {
|
|
263
|
+
(void)thread;
|
|
264
|
+
(void)run;
|
|
265
|
+
(void)argument;
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static void agent_thread_join(AgentThread thread) {
|
|
270
|
+
(void)thread;
|
|
271
|
+
}
|
|
272
|
+
#endif
|
|
273
|
+
|
|
274
|
+
static void cluster_lock(AgentCluster *cluster) {
|
|
275
|
+
agent_mutex_lock(&cluster->lock);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
static void cluster_unlock(AgentCluster *cluster) {
|
|
279
|
+
agent_mutex_unlock(&cluster->lock);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
static void holes_free(AgentHole *holes, size_t count) {
|
|
283
|
+
if (holes == NULL) return;
|
|
284
|
+
for (size_t index = 0u; index < count; index += 1u) {
|
|
285
|
+
free(holes[index].digits);
|
|
286
|
+
}
|
|
287
|
+
free(holes);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/*
|
|
291
|
+
* Wakes one blocked agent's own clock wait. Only an agent whose thread is
|
|
292
|
+
* blocked is ever signaled, so its realm and adapter are alive.
|
|
293
|
+
*/
|
|
294
|
+
static void agent_signal(Agent *agent) {
|
|
295
|
+
if (agent->context != NULL) (void)oseo_clock_wake(agent->context);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
static void ready_push(AgentCluster *cluster, Agent *agent) {
|
|
299
|
+
agent->ready_next = NULL;
|
|
300
|
+
if (cluster->ready_tail == NULL) {
|
|
301
|
+
cluster->ready_head = agent;
|
|
302
|
+
} else {
|
|
303
|
+
cluster->ready_tail->ready_next = agent;
|
|
304
|
+
}
|
|
305
|
+
cluster->ready_tail = agent;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
static Agent *ready_pop(AgentCluster *cluster) {
|
|
309
|
+
Agent *agent = cluster->ready_head;
|
|
310
|
+
if (agent == NULL) return NULL;
|
|
311
|
+
cluster->ready_head = agent->ready_next;
|
|
312
|
+
if (cluster->ready_head == NULL) cluster->ready_tail = NULL;
|
|
313
|
+
agent->ready_next = NULL;
|
|
314
|
+
return agent;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/* Hands a free turn to the longest-ready agent, if there is one. */
|
|
318
|
+
static void cluster_dispatch(AgentCluster *cluster) {
|
|
319
|
+
if (cluster->running != NULL) return;
|
|
320
|
+
Agent *next = ready_pop(cluster);
|
|
321
|
+
if (next == NULL) return;
|
|
322
|
+
next->state = AGENT_RUNNING;
|
|
323
|
+
cluster->running = next;
|
|
324
|
+
agent_signal(next);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
static void agent_make_ready(Agent *agent) {
|
|
328
|
+
if (agent->state != AGENT_BLOCKED) return;
|
|
329
|
+
agent->state = AGENT_READY;
|
|
330
|
+
agent->has_deadline = false;
|
|
331
|
+
agent->wakes_on_work = false;
|
|
332
|
+
ready_push(agent->cluster, agent);
|
|
333
|
+
cluster_dispatch(agent->cluster);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static bool agent_has_work(const Agent *agent, const OseoContext *context) {
|
|
337
|
+
return agent->resolved_head != NULL ||
|
|
338
|
+
(agent->broadcast_head != NULL &&
|
|
339
|
+
tag_of(context->agent_broadcast_callback) != OSEO_TAG_UNDEFINED);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/*
|
|
343
|
+
* Whether an agent other than `self` can still make progress on its own:
|
|
344
|
+
* it holds or awaits the turn, is starting, waits for a deadline, or has
|
|
345
|
+
* incoming work its idle event loop will take.
|
|
346
|
+
*/
|
|
347
|
+
static bool cluster_other_progresses(AgentCluster *cluster, Agent *self) {
|
|
348
|
+
Agent *main = cluster->main;
|
|
349
|
+
for (Agent *agent = main; agent != NULL;
|
|
350
|
+
agent = agent == main ? cluster->agents_head : agent->next) {
|
|
351
|
+
if (agent == self) continue;
|
|
352
|
+
if (agent->state == AGENT_STARTING ||
|
|
353
|
+
agent->state == AGENT_READY ||
|
|
354
|
+
agent->state == AGENT_RUNNING) {
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
if (agent->state == AGENT_BLOCKED && agent->has_deadline) {
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
static void cluster_print_stall(const Agent *agent) {
|
|
365
|
+
const OseoContext *context = agent->context;
|
|
366
|
+
(void)fflush(stdout);
|
|
367
|
+
if (context != NULL) {
|
|
368
|
+
(void)fwrite(
|
|
369
|
+
context->source_id,
|
|
370
|
+
1u,
|
|
371
|
+
context->source_id_length,
|
|
372
|
+
stderr
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
(void)fprintf(
|
|
376
|
+
stderr,
|
|
377
|
+
": error[OSEO3001]: The agent cluster cannot make progress.\n"
|
|
378
|
+
);
|
|
379
|
+
(void)fflush(stderr);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/*
|
|
383
|
+
* Runs after the turn became free with no agent ready. When every
|
|
384
|
+
* remaining agent waits without a deadline, nothing can wake any of them:
|
|
385
|
+
* an idle main agent is made ready so its event loop can end, and a main
|
|
386
|
+
* agent suspended in a blocking operation is a deadlock the process
|
|
387
|
+
* reports and exits on, rather than hanging.
|
|
388
|
+
*/
|
|
389
|
+
static void cluster_check_progress(AgentCluster *cluster, Agent *self) {
|
|
390
|
+
if (cluster->running != NULL || cluster->ready_head != NULL) return;
|
|
391
|
+
if (cluster_other_progresses(cluster, NULL)) return;
|
|
392
|
+
Agent *main = cluster->main;
|
|
393
|
+
if (main->state == AGENT_FINISHED) return;
|
|
394
|
+
if (main->state == AGENT_BLOCKED && main->wakes_on_work) {
|
|
395
|
+
agent_make_ready(main);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
cluster_print_stall(self);
|
|
399
|
+
exit(EXIT_FAILURE);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/* An infinite or deadline wait on the agent's own clock adapter. */
|
|
403
|
+
static OseoResult agent_clock_wait(
|
|
404
|
+
OseoContext *context,
|
|
405
|
+
bool has_deadline,
|
|
406
|
+
uint64_t deadline
|
|
407
|
+
) {
|
|
408
|
+
if (has_deadline) return oseo_internal_clock_wait_until(context, deadline);
|
|
409
|
+
OseoResult result = oseo_internal_clock_start(context);
|
|
410
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
411
|
+
if (context->clock_adapter->wait(context->clock_state, false, 0u) ==
|
|
412
|
+
OSEO_CLOCK_WAIT_FAILED) {
|
|
413
|
+
return failure(context, "OSEO2001", "The host clock wait failed.");
|
|
414
|
+
}
|
|
415
|
+
return normal(oseo_undefined());
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/*
|
|
419
|
+
* Waits, with the cluster lock held on entry and on return, until this
|
|
420
|
+
* agent holds the turn. A blocked agent whose deadline passes first
|
|
421
|
+
* becomes ready by itself; `timed_out` reports that, and a blocking
|
|
422
|
+
* waiter still listed at that moment leaves the store.
|
|
423
|
+
*/
|
|
424
|
+
static OseoResult agent_await_turn(
|
|
425
|
+
OseoContext *context,
|
|
426
|
+
Agent *self,
|
|
427
|
+
uint64_t deadline,
|
|
428
|
+
AgentWaiter *waiter,
|
|
429
|
+
bool *timed_out
|
|
430
|
+
) {
|
|
431
|
+
AgentCluster *cluster = self->cluster;
|
|
432
|
+
OseoResult result = normal(oseo_undefined());
|
|
433
|
+
while (cluster->running != self) {
|
|
434
|
+
if (self->state == AGENT_BLOCKED && self->has_deadline) {
|
|
435
|
+
uint64_t now = 0u;
|
|
436
|
+
result = oseo_internal_clock_now(context, &now);
|
|
437
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
438
|
+
if (now >= deadline) {
|
|
439
|
+
if (waiter != NULL && waiter->listed) {
|
|
440
|
+
AgentWaiter **link = &cluster->waiters_head;
|
|
441
|
+
AgentWaiter *previous = NULL;
|
|
442
|
+
while (*link != waiter) {
|
|
443
|
+
previous = *link;
|
|
444
|
+
link = &(*link)->next;
|
|
445
|
+
}
|
|
446
|
+
*link = waiter->next;
|
|
447
|
+
if (cluster->waiters_tail == waiter) {
|
|
448
|
+
cluster->waiters_tail = previous;
|
|
449
|
+
}
|
|
450
|
+
waiter->listed = false;
|
|
451
|
+
}
|
|
452
|
+
if (timed_out != NULL) *timed_out = true;
|
|
453
|
+
agent_make_ready(self);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
bool has_deadline =
|
|
458
|
+
self->state == AGENT_BLOCKED && self->has_deadline;
|
|
459
|
+
cluster_unlock(cluster);
|
|
460
|
+
result = agent_clock_wait(context, has_deadline, deadline);
|
|
461
|
+
cluster_lock(cluster);
|
|
462
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
463
|
+
}
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/*
|
|
468
|
+
* Gives up the turn and blocks until another agent makes this one ready
|
|
469
|
+
* or, with `has_deadline`, until the deadline passes, then waits for the
|
|
470
|
+
* turn again. The cluster lock is held on entry and on return.
|
|
471
|
+
*/
|
|
472
|
+
static OseoResult agent_block(
|
|
473
|
+
OseoContext *context,
|
|
474
|
+
Agent *self,
|
|
475
|
+
bool has_deadline,
|
|
476
|
+
uint64_t deadline,
|
|
477
|
+
bool wakes_on_work,
|
|
478
|
+
AgentWaiter *waiter,
|
|
479
|
+
bool *timed_out
|
|
480
|
+
) {
|
|
481
|
+
AgentCluster *cluster = self->cluster;
|
|
482
|
+
self->state = AGENT_BLOCKED;
|
|
483
|
+
self->has_deadline = has_deadline;
|
|
484
|
+
self->wakes_on_work = wakes_on_work;
|
|
485
|
+
cluster->running = NULL;
|
|
486
|
+
cluster_dispatch(cluster);
|
|
487
|
+
if (!has_deadline) cluster_check_progress(cluster, self);
|
|
488
|
+
return agent_await_turn(context, self, deadline, waiter, timed_out);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/*
|
|
492
|
+
* The whole-millisecond deadline `milliseconds` after now. The current
|
|
493
|
+
* whole millisecond is counted as already started, so the wait lasts at
|
|
494
|
+
* least the requested time however far into it the call began, and a
|
|
495
|
+
* fractional request rounds up, which is a permitted additional timeout.
|
|
496
|
+
*/
|
|
497
|
+
static OseoResult agent_deadline(
|
|
498
|
+
OseoContext *context,
|
|
499
|
+
double milliseconds,
|
|
500
|
+
uint64_t *deadline
|
|
501
|
+
) {
|
|
502
|
+
uint64_t now = 0u;
|
|
503
|
+
OseoResult result = oseo_internal_clock_now(context, &now);
|
|
504
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
505
|
+
double rounded = ceil(milliseconds > 0.0 ? milliseconds : 0.0) + 1.0;
|
|
506
|
+
uint64_t delay = rounded >= 18446744073709549568.0
|
|
507
|
+
? UINT64_MAX
|
|
508
|
+
: (uint64_t)rounded;
|
|
509
|
+
*deadline = UINT64_MAX - now < delay ? UINT64_MAX : now + delay;
|
|
510
|
+
return result;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
OseoResult oseo_internal_agent_yield(OseoContext *context) {
|
|
514
|
+
Agent *self = agent_of(context);
|
|
515
|
+
if (self == NULL) return normal(oseo_undefined());
|
|
516
|
+
AgentCluster *cluster = self->cluster;
|
|
517
|
+
cluster_lock(cluster);
|
|
518
|
+
OseoResult result = normal(oseo_undefined());
|
|
519
|
+
if (cluster->ready_head != NULL) {
|
|
520
|
+
self->state = AGENT_READY;
|
|
521
|
+
ready_push(cluster, self);
|
|
522
|
+
cluster->running = NULL;
|
|
523
|
+
cluster_dispatch(cluster);
|
|
524
|
+
result = agent_await_turn(context, self, 0u, NULL, NULL);
|
|
525
|
+
}
|
|
526
|
+
cluster_unlock(cluster);
|
|
527
|
+
return result;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
static void waiter_link(AgentCluster *cluster, AgentWaiter *waiter) {
|
|
531
|
+
waiter->next = NULL;
|
|
532
|
+
if (cluster->waiters_tail == NULL) {
|
|
533
|
+
cluster->waiters_head = waiter;
|
|
534
|
+
} else {
|
|
535
|
+
cluster->waiters_tail->next = waiter;
|
|
536
|
+
}
|
|
537
|
+
cluster->waiters_tail = waiter;
|
|
538
|
+
waiter->listed = true;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
static void waiter_unlink(AgentCluster *cluster, AgentWaiter *waiter) {
|
|
542
|
+
AgentWaiter *previous = NULL;
|
|
543
|
+
AgentWaiter *current = cluster->waiters_head;
|
|
544
|
+
while (current != NULL && current != waiter) {
|
|
545
|
+
previous = current;
|
|
546
|
+
current = current->next;
|
|
547
|
+
}
|
|
548
|
+
if (current == NULL) return;
|
|
549
|
+
if (previous == NULL) {
|
|
550
|
+
cluster->waiters_head = waiter->next;
|
|
551
|
+
} else {
|
|
552
|
+
previous->next = waiter->next;
|
|
553
|
+
}
|
|
554
|
+
if (cluster->waiters_tail == waiter) cluster->waiters_tail = previous;
|
|
555
|
+
waiter->next = NULL;
|
|
556
|
+
waiter->listed = false;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
OseoResult oseo_internal_agent_suspend(
|
|
560
|
+
OseoContext *context,
|
|
561
|
+
const OseoSharedBlock *block,
|
|
562
|
+
size_t byte_index,
|
|
563
|
+
double timeout,
|
|
564
|
+
bool *notified
|
|
565
|
+
) {
|
|
566
|
+
Agent *self = agent_of(context);
|
|
567
|
+
AgentCluster *cluster = self->cluster;
|
|
568
|
+
bool finite = isfinite(timeout);
|
|
569
|
+
uint64_t deadline = 0u;
|
|
570
|
+
if (finite) {
|
|
571
|
+
OseoResult result = agent_deadline(context, timeout, &deadline);
|
|
572
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
573
|
+
}
|
|
574
|
+
AgentWaiter waiter;
|
|
575
|
+
waiter.next = NULL;
|
|
576
|
+
waiter.resolved_next = NULL;
|
|
577
|
+
waiter.agent = self;
|
|
578
|
+
waiter.block = block;
|
|
579
|
+
waiter.byte_index = byte_index;
|
|
580
|
+
waiter.waiter = oseo_undefined();
|
|
581
|
+
waiter.asynchronous = false;
|
|
582
|
+
waiter.listed = false;
|
|
583
|
+
waiter.notified = false;
|
|
584
|
+
cluster_lock(cluster);
|
|
585
|
+
waiter_link(cluster, &waiter);
|
|
586
|
+
OseoResult result = agent_block(
|
|
587
|
+
context,
|
|
588
|
+
self,
|
|
589
|
+
finite,
|
|
590
|
+
deadline,
|
|
591
|
+
false,
|
|
592
|
+
&waiter,
|
|
593
|
+
NULL
|
|
594
|
+
);
|
|
595
|
+
if (waiter.listed) waiter_unlink(cluster, &waiter);
|
|
596
|
+
*notified = waiter.notified;
|
|
597
|
+
cluster_unlock(cluster);
|
|
598
|
+
return result;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
OseoResult oseo_internal_agent_list_waiter(
|
|
602
|
+
OseoContext *context,
|
|
603
|
+
OseoValue waiter
|
|
604
|
+
) {
|
|
605
|
+
Agent *self = agent_of(context);
|
|
606
|
+
OseoAtomicsWaiter *record = atomics_waiter_object(waiter);
|
|
607
|
+
AgentWaiter *entry = malloc(sizeof(*entry));
|
|
608
|
+
if (entry == NULL) {
|
|
609
|
+
return failure(
|
|
610
|
+
context,
|
|
611
|
+
"OSEO2001",
|
|
612
|
+
"Atomics waiter allocation failed."
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
entry->next = NULL;
|
|
616
|
+
entry->resolved_next = NULL;
|
|
617
|
+
entry->agent = self;
|
|
618
|
+
entry->block = record->block;
|
|
619
|
+
entry->byte_index = record->byte_index;
|
|
620
|
+
entry->waiter = waiter;
|
|
621
|
+
entry->asynchronous = true;
|
|
622
|
+
entry->listed = false;
|
|
623
|
+
entry->notified = false;
|
|
624
|
+
record->record = entry;
|
|
625
|
+
cluster_lock(self->cluster);
|
|
626
|
+
waiter_link(self->cluster, entry);
|
|
627
|
+
self->listed_async += 1u;
|
|
628
|
+
cluster_unlock(self->cluster);
|
|
629
|
+
return normal(oseo_undefined());
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
bool oseo_internal_agent_unlist_waiter(OseoContext *context, OseoValue waiter) {
|
|
633
|
+
Agent *self = agent_of(context);
|
|
634
|
+
OseoAtomicsWaiter *record = atomics_waiter_object(waiter);
|
|
635
|
+
AgentWaiter *entry = record->record;
|
|
636
|
+
if (entry == NULL) return false;
|
|
637
|
+
cluster_lock(self->cluster);
|
|
638
|
+
bool listed = entry->listed;
|
|
639
|
+
if (listed) {
|
|
640
|
+
waiter_unlink(self->cluster, entry);
|
|
641
|
+
self->listed_async -= 1u;
|
|
642
|
+
}
|
|
643
|
+
cluster_unlock(self->cluster);
|
|
644
|
+
if (!listed) return false;
|
|
645
|
+
record->record = NULL;
|
|
646
|
+
free(entry);
|
|
647
|
+
return true;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
bool oseo_internal_agent_notify_one(
|
|
651
|
+
OseoContext *context,
|
|
652
|
+
const OseoSharedBlock *block,
|
|
653
|
+
size_t byte_index,
|
|
654
|
+
OseoValue *own_waiter
|
|
655
|
+
) {
|
|
656
|
+
Agent *self = agent_of(context);
|
|
657
|
+
AgentCluster *cluster = self->cluster;
|
|
658
|
+
cluster_lock(cluster);
|
|
659
|
+
AgentWaiter *entry = cluster->waiters_head;
|
|
660
|
+
while (entry != NULL &&
|
|
661
|
+
(entry->block != block || entry->byte_index != byte_index)) {
|
|
662
|
+
entry = entry->next;
|
|
663
|
+
}
|
|
664
|
+
if (entry == NULL) {
|
|
665
|
+
cluster_unlock(cluster);
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
waiter_unlink(cluster, entry);
|
|
669
|
+
entry->notified = true;
|
|
670
|
+
Agent *owner = entry->agent;
|
|
671
|
+
if (!entry->asynchronous) {
|
|
672
|
+
agent_make_ready(owner);
|
|
673
|
+
} else if (owner == self) {
|
|
674
|
+
owner->listed_async -= 1u;
|
|
675
|
+
*own_waiter = entry->waiter;
|
|
676
|
+
atomics_waiter_object(entry->waiter)->record = NULL;
|
|
677
|
+
free(entry);
|
|
678
|
+
} else {
|
|
679
|
+
/* EnqueueResolveInAgentJob: the owner resolves it as a task. */
|
|
680
|
+
owner->listed_async -= 1u;
|
|
681
|
+
entry->resolved_next = NULL;
|
|
682
|
+
if (owner->resolved_tail == NULL) {
|
|
683
|
+
owner->resolved_head = entry;
|
|
684
|
+
} else {
|
|
685
|
+
owner->resolved_tail->resolved_next = entry;
|
|
686
|
+
}
|
|
687
|
+
owner->resolved_tail = entry;
|
|
688
|
+
if (owner->state == AGENT_BLOCKED && owner->wakes_on_work) {
|
|
689
|
+
agent_make_ready(owner);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
cluster_unlock(cluster);
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/*
|
|
697
|
+
* Whether this realm's next broadcast can be taken: a callback is
|
|
698
|
+
* registered and one is waiting. The lock is held.
|
|
699
|
+
*/
|
|
700
|
+
static AgentBroadcast *take_broadcast(OseoContext *context, Agent *self) {
|
|
701
|
+
if (tag_of(context->agent_broadcast_callback) == OSEO_TAG_UNDEFINED) {
|
|
702
|
+
return NULL;
|
|
703
|
+
}
|
|
704
|
+
AgentBroadcast *broadcast = self->broadcast_head;
|
|
705
|
+
if (broadcast == NULL) return NULL;
|
|
706
|
+
self->broadcast_head = broadcast->next;
|
|
707
|
+
if (self->broadcast_head == NULL) self->broadcast_tail = NULL;
|
|
708
|
+
broadcast->next = NULL;
|
|
709
|
+
AgentCluster *cluster = self->cluster;
|
|
710
|
+
if (cluster->broadcast_remaining > 0u) {
|
|
711
|
+
cluster->broadcast_remaining -= 1u;
|
|
712
|
+
if (cluster->broadcast_remaining == 0u) {
|
|
713
|
+
agent_make_ready(cluster->main);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return broadcast;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
static OseoResult broadcast_id(
|
|
720
|
+
OseoContext *context,
|
|
721
|
+
const AgentBroadcast *broadcast
|
|
722
|
+
) {
|
|
723
|
+
if (broadcast->id_kind == AGENT_ID_NUMBER) {
|
|
724
|
+
return normal(oseo_number(broadcast->id_number));
|
|
725
|
+
}
|
|
726
|
+
if (broadcast->id_kind == AGENT_ID_BIGINT) {
|
|
727
|
+
bool negative = (broadcast->id_bits >> 63u) != 0u;
|
|
728
|
+
return oseo_internal_bigint_from_uint64(
|
|
729
|
+
context,
|
|
730
|
+
negative ? UINT64_C(0) - broadcast->id_bits : broadcast->id_bits,
|
|
731
|
+
negative
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
return normal(oseo_undefined());
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/*
|
|
738
|
+
* Turns one retrieved broadcast into a task of this agent: the callback
|
|
739
|
+
* runs as a due timer callback with this realm's own SharedArrayBuffer
|
|
740
|
+
* over the broadcast block and the id, so it follows every timer turn
|
|
741
|
+
* rule, including the checkpoint after it.
|
|
742
|
+
*/
|
|
743
|
+
static OseoResult deliver_broadcast(
|
|
744
|
+
OseoContext *context,
|
|
745
|
+
AgentBroadcast *broadcast
|
|
746
|
+
) {
|
|
747
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
748
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 4u);
|
|
749
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
750
|
+
oseo_internal_shared_block_release(broadcast->block);
|
|
751
|
+
free(broadcast);
|
|
752
|
+
return result;
|
|
753
|
+
}
|
|
754
|
+
frame.slots[0] = context->agent_broadcast_callback;
|
|
755
|
+
frame.slots[1] = oseo_number(0.0);
|
|
756
|
+
result = oseo_internal_shared_array_buffer_from_block(
|
|
757
|
+
context,
|
|
758
|
+
broadcast->block
|
|
759
|
+
);
|
|
760
|
+
frame.slots[2] = result.value;
|
|
761
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
762
|
+
result = broadcast_id(context, broadcast);
|
|
763
|
+
frame.slots[3] = result.value;
|
|
764
|
+
}
|
|
765
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
766
|
+
result = oseo_set_timeout(context, 4u, frame.slots);
|
|
767
|
+
}
|
|
768
|
+
oseo_roots_release(context, &frame);
|
|
769
|
+
oseo_internal_shared_block_release(broadcast->block);
|
|
770
|
+
free(broadcast);
|
|
771
|
+
return result;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
OseoResult oseo_internal_agent_receive(OseoContext *context, bool *ran) {
|
|
775
|
+
*ran = false;
|
|
776
|
+
Agent *self = agent_of(context);
|
|
777
|
+
if (self == NULL) return normal(oseo_undefined());
|
|
778
|
+
AgentCluster *cluster = self->cluster;
|
|
779
|
+
OseoResult result = normal(oseo_undefined());
|
|
780
|
+
for (;;) {
|
|
781
|
+
cluster_lock(cluster);
|
|
782
|
+
AgentWaiter *entry = self->resolved_head;
|
|
783
|
+
if (entry != NULL) {
|
|
784
|
+
self->resolved_head = entry->resolved_next;
|
|
785
|
+
if (self->resolved_head == NULL) self->resolved_tail = NULL;
|
|
786
|
+
}
|
|
787
|
+
AgentBroadcast *broadcast =
|
|
788
|
+
entry == NULL ? take_broadcast(context, self) : NULL;
|
|
789
|
+
cluster_unlock(cluster);
|
|
790
|
+
if (entry == NULL && broadcast == NULL) break;
|
|
791
|
+
*ran = true;
|
|
792
|
+
if (entry != NULL) {
|
|
793
|
+
OseoValue waiter = entry->waiter;
|
|
794
|
+
atomics_waiter_object(waiter)->record = NULL;
|
|
795
|
+
free(entry);
|
|
796
|
+
result = oseo_internal_atomics_waiter_notified(context, waiter);
|
|
797
|
+
} else {
|
|
798
|
+
result = deliver_broadcast(context, broadcast);
|
|
799
|
+
}
|
|
800
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
801
|
+
}
|
|
802
|
+
return result;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
bool oseo_internal_agent_keeps_alive(OseoContext *context) {
|
|
806
|
+
Agent *self = agent_of(context);
|
|
807
|
+
if (self == NULL) return false;
|
|
808
|
+
AgentCluster *cluster = self->cluster;
|
|
809
|
+
cluster_lock(cluster);
|
|
810
|
+
bool alive;
|
|
811
|
+
if (agent_has_work(self, context)) {
|
|
812
|
+
alive = true;
|
|
813
|
+
} else if (!self->main) {
|
|
814
|
+
alive = self->listed_async > 0u ||
|
|
815
|
+
(tag_of(context->agent_broadcast_callback) !=
|
|
816
|
+
OSEO_TAG_UNDEFINED &&
|
|
817
|
+
!self->leaving);
|
|
818
|
+
} else {
|
|
819
|
+
alive = self->listed_async > 0u &&
|
|
820
|
+
cluster_other_progresses(cluster, self);
|
|
821
|
+
}
|
|
822
|
+
cluster_unlock(cluster);
|
|
823
|
+
return alive;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
OseoResult oseo_internal_agent_idle(
|
|
827
|
+
OseoContext *context,
|
|
828
|
+
bool has_deadline,
|
|
829
|
+
uint64_t deadline
|
|
830
|
+
) {
|
|
831
|
+
Agent *self = agent_of(context);
|
|
832
|
+
if (self == NULL) {
|
|
833
|
+
return has_deadline
|
|
834
|
+
? oseo_internal_clock_wait_until(context, deadline)
|
|
835
|
+
: normal(oseo_undefined());
|
|
836
|
+
}
|
|
837
|
+
AgentCluster *cluster = self->cluster;
|
|
838
|
+
cluster_lock(cluster);
|
|
839
|
+
OseoResult result = normal(oseo_undefined());
|
|
840
|
+
if (!agent_has_work(self, context)) {
|
|
841
|
+
result = agent_block(
|
|
842
|
+
context,
|
|
843
|
+
self,
|
|
844
|
+
has_deadline,
|
|
845
|
+
deadline,
|
|
846
|
+
true,
|
|
847
|
+
NULL,
|
|
848
|
+
NULL
|
|
849
|
+
);
|
|
850
|
+
}
|
|
851
|
+
cluster_unlock(cluster);
|
|
852
|
+
return result;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/* Drops everything an agent that stops evaluating still holds. */
|
|
856
|
+
static void agent_discard_work(AgentCluster *cluster, Agent *agent) {
|
|
857
|
+
AgentWaiter *entry = cluster->waiters_head;
|
|
858
|
+
while (entry != NULL) {
|
|
859
|
+
AgentWaiter *next = entry->next;
|
|
860
|
+
if (entry->agent == agent) {
|
|
861
|
+
waiter_unlink(cluster, entry);
|
|
862
|
+
if (entry->asynchronous) free(entry);
|
|
863
|
+
}
|
|
864
|
+
entry = next;
|
|
865
|
+
}
|
|
866
|
+
while (agent->resolved_head != NULL) {
|
|
867
|
+
AgentWaiter *resolved = agent->resolved_head;
|
|
868
|
+
agent->resolved_head = resolved->resolved_next;
|
|
869
|
+
free(resolved);
|
|
870
|
+
}
|
|
871
|
+
agent->resolved_tail = NULL;
|
|
872
|
+
agent->listed_async = 0u;
|
|
873
|
+
while (agent->broadcast_head != NULL) {
|
|
874
|
+
AgentBroadcast *broadcast = agent->broadcast_head;
|
|
875
|
+
agent->broadcast_head = broadcast->next;
|
|
876
|
+
oseo_internal_shared_block_release(broadcast->block);
|
|
877
|
+
free(broadcast);
|
|
878
|
+
if (cluster->broadcast_remaining > 0u) {
|
|
879
|
+
cluster->broadcast_remaining -= 1u;
|
|
880
|
+
if (cluster->broadcast_remaining == 0u) {
|
|
881
|
+
agent_make_ready(cluster->main);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
agent->broadcast_tail = NULL;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
static void cluster_reap(AgentCluster *cluster);
|
|
889
|
+
|
|
890
|
+
static void cluster_free(AgentCluster *cluster) {
|
|
891
|
+
while (cluster->reports_head != NULL) {
|
|
892
|
+
AgentReport *report = cluster->reports_head;
|
|
893
|
+
cluster->reports_head = report->next;
|
|
894
|
+
free(report);
|
|
895
|
+
}
|
|
896
|
+
Agent *agent = cluster->agents_head;
|
|
897
|
+
while (agent != NULL) {
|
|
898
|
+
Agent *next = agent->next;
|
|
899
|
+
holes_free(agent->holes, agent->hole_count);
|
|
900
|
+
free(agent);
|
|
901
|
+
agent = next;
|
|
902
|
+
}
|
|
903
|
+
free(cluster->main);
|
|
904
|
+
agent_mutex_destroy(&cluster->lock);
|
|
905
|
+
free(cluster);
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
void oseo_internal_agent_context_destroy(OseoContext *context) {
|
|
909
|
+
Agent *self = agent_of(context);
|
|
910
|
+
if (self == NULL || !self->main) return;
|
|
911
|
+
AgentCluster *cluster = self->cluster;
|
|
912
|
+
cluster_lock(cluster);
|
|
913
|
+
/*
|
|
914
|
+
* The main agent keeps the turn from here on, so an agent that is
|
|
915
|
+
* still waiting never evaluates again while the process exits. The
|
|
916
|
+
* agents that finished are joined and the cluster is released only
|
|
917
|
+
* when all of them did; a waiting agent's thread still refers to it.
|
|
918
|
+
*/
|
|
919
|
+
agent_discard_work(cluster, self);
|
|
920
|
+
bool finished = true;
|
|
921
|
+
for (Agent *agent = cluster->agents_head; agent != NULL;
|
|
922
|
+
agent = agent->next) {
|
|
923
|
+
if (agent->state != AGENT_FINISHED) finished = false;
|
|
924
|
+
}
|
|
925
|
+
cluster_unlock(cluster);
|
|
926
|
+
context->agent = NULL;
|
|
927
|
+
if (!finished) return;
|
|
928
|
+
cluster_reap(cluster);
|
|
929
|
+
cluster_free(cluster);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/*
|
|
933
|
+
* Matches a start source against one program's template: the literal
|
|
934
|
+
* runs must match exactly, and each hole must be a decimal integer
|
|
935
|
+
* literal without a leading zero, optionally followed by the BigInt
|
|
936
|
+
* suffix `n`. The runs never start or end with a digit or an identifier
|
|
937
|
+
* character, so each hole is the maximal digit run at its position and a
|
|
938
|
+
* following `n` can only be its suffix. `false` with `*failed` set
|
|
939
|
+
* reports an allocation failure rather than a mismatch.
|
|
940
|
+
*/
|
|
941
|
+
static bool program_matches(
|
|
942
|
+
const OseoAgentProgram *program,
|
|
943
|
+
const OseoString *source,
|
|
944
|
+
AgentHole *holes,
|
|
945
|
+
bool *failed
|
|
946
|
+
) {
|
|
947
|
+
size_t position = 0u;
|
|
948
|
+
for (size_t index = 0u; index < program->segment_count; index += 1u) {
|
|
949
|
+
const OseoAgentSegment *segment = &program->segments[index];
|
|
950
|
+
if (index > 0u) {
|
|
951
|
+
size_t start = position;
|
|
952
|
+
while (position < source->length &&
|
|
953
|
+
source->units[position] >= '0' &&
|
|
954
|
+
source->units[position] <= '9') {
|
|
955
|
+
position += 1u;
|
|
956
|
+
}
|
|
957
|
+
size_t length = position - start;
|
|
958
|
+
if (length == 0u) return false;
|
|
959
|
+
if (source->units[start] == '0' && length > 1u) return false;
|
|
960
|
+
AgentHole *hole = &holes[index - 1u];
|
|
961
|
+
hole->bigint = position < source->length &&
|
|
962
|
+
source->units[position] == 'n';
|
|
963
|
+
if (hole->bigint) position += 1u;
|
|
964
|
+
hole->digits = malloc(length + 1u);
|
|
965
|
+
if (hole->digits == NULL) {
|
|
966
|
+
*failed = true;
|
|
967
|
+
return false;
|
|
968
|
+
}
|
|
969
|
+
for (size_t digit = 0u; digit < length; digit += 1u) {
|
|
970
|
+
hole->digits[digit] = (char)source->units[start + digit];
|
|
971
|
+
}
|
|
972
|
+
hole->digits[length] = '\0';
|
|
973
|
+
/* One correctly rounded conversion of the exact literal text. */
|
|
974
|
+
hole->number = strtod(hole->digits, NULL);
|
|
975
|
+
}
|
|
976
|
+
if (source->length - position < segment->length ||
|
|
977
|
+
memcmp(
|
|
978
|
+
source->units + position,
|
|
979
|
+
segment->units,
|
|
980
|
+
segment->length * sizeof(uint16_t)
|
|
981
|
+
) != 0) {
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
position += segment->length;
|
|
985
|
+
}
|
|
986
|
+
return position == source->length;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
static OseoResult install_host(OseoContext *context, bool main);
|
|
990
|
+
|
|
991
|
+
static void agent_report_failure(OseoContext *context, OseoResult result) {
|
|
992
|
+
(void)fflush(stdout);
|
|
993
|
+
oseo_context_print_thrown(context, result.value);
|
|
994
|
+
(void)fflush(stderr);
|
|
995
|
+
exit(EXIT_FAILURE);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/*
|
|
999
|
+
* The body of one agent thread. It initializes the agent's realm, makes
|
|
1000
|
+
* itself ready ahead of the main agent that started it, and once it holds
|
|
1001
|
+
* the turn evaluates its program. An abrupt completion of an agent ends
|
|
1002
|
+
* the process with the agent's error, because a test that lost an agent
|
|
1003
|
+
* could otherwise only hang. A finished agent destroys its realm while it
|
|
1004
|
+
* still holds the turn and then passes the turn on for good.
|
|
1005
|
+
*/
|
|
1006
|
+
static void *agent_thread(void *argument) {
|
|
1007
|
+
Agent *self = argument;
|
|
1008
|
+
AgentCluster *cluster = self->cluster;
|
|
1009
|
+
OseoContext context;
|
|
1010
|
+
oseo_context_init(
|
|
1011
|
+
&context,
|
|
1012
|
+
self->program->source_id,
|
|
1013
|
+
self->program->source_id_length
|
|
1014
|
+
);
|
|
1015
|
+
context.agent = self;
|
|
1016
|
+
context.unhandled_rejection_policy = cluster->unhandled_rejection_policy;
|
|
1017
|
+
if (!oseo_clock_open(&context)) {
|
|
1018
|
+
(void)fflush(stdout);
|
|
1019
|
+
(void)fwrite(
|
|
1020
|
+
context.source_id,
|
|
1021
|
+
1u,
|
|
1022
|
+
context.source_id_length,
|
|
1023
|
+
stderr
|
|
1024
|
+
);
|
|
1025
|
+
(void)fprintf(
|
|
1026
|
+
stderr,
|
|
1027
|
+
": error[OSEO2001]: Cross-agent wakeup is unavailable.\n"
|
|
1028
|
+
);
|
|
1029
|
+
exit(EXIT_FAILURE);
|
|
1030
|
+
}
|
|
1031
|
+
cluster_lock(cluster);
|
|
1032
|
+
self->context = &context;
|
|
1033
|
+
self->state = AGENT_READY;
|
|
1034
|
+
ready_push(cluster, self);
|
|
1035
|
+
agent_make_ready(cluster->main);
|
|
1036
|
+
cluster_dispatch(cluster);
|
|
1037
|
+
OseoResult result = agent_await_turn(&context, self, 0u, NULL, NULL);
|
|
1038
|
+
cluster_unlock(cluster);
|
|
1039
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1040
|
+
result = install_host(&context, false);
|
|
1041
|
+
}
|
|
1042
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1043
|
+
result = self->program->entry(&context);
|
|
1044
|
+
}
|
|
1045
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1046
|
+
agent_report_failure(&context, result);
|
|
1047
|
+
}
|
|
1048
|
+
cluster_lock(cluster);
|
|
1049
|
+
agent_discard_work(cluster, self);
|
|
1050
|
+
cluster_unlock(cluster);
|
|
1051
|
+
context.agent = NULL;
|
|
1052
|
+
oseo_context_destroy(&context);
|
|
1053
|
+
cluster_lock(cluster);
|
|
1054
|
+
self->context = NULL;
|
|
1055
|
+
self->state = AGENT_FINISHED;
|
|
1056
|
+
cluster->running = NULL;
|
|
1057
|
+
cluster_dispatch(cluster);
|
|
1058
|
+
cluster_check_progress(cluster, self);
|
|
1059
|
+
cluster_unlock(cluster);
|
|
1060
|
+
return NULL;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
/*
|
|
1064
|
+
* Joins and releases every agent that has finished. An agent marks itself
|
|
1065
|
+
* finished in its last critical section and touches nothing shared after
|
|
1066
|
+
* it, so the join outside the lock only waits for its thread to return.
|
|
1067
|
+
* The main agent reaps before each start, so a case that starts agents one
|
|
1068
|
+
* after another holds at most the threads of the agents still running.
|
|
1069
|
+
*/
|
|
1070
|
+
static void cluster_reap(AgentCluster *cluster) {
|
|
1071
|
+
Agent *finished = NULL;
|
|
1072
|
+
cluster_lock(cluster);
|
|
1073
|
+
Agent **link = &cluster->agents_head;
|
|
1074
|
+
Agent *previous = NULL;
|
|
1075
|
+
while (*link != NULL) {
|
|
1076
|
+
Agent *agent = *link;
|
|
1077
|
+
if (agent->state != AGENT_FINISHED) {
|
|
1078
|
+
previous = agent;
|
|
1079
|
+
link = &agent->next;
|
|
1080
|
+
continue;
|
|
1081
|
+
}
|
|
1082
|
+
*link = agent->next;
|
|
1083
|
+
if (cluster->agents_tail == agent) cluster->agents_tail = previous;
|
|
1084
|
+
agent->next = finished;
|
|
1085
|
+
finished = agent;
|
|
1086
|
+
}
|
|
1087
|
+
cluster_unlock(cluster);
|
|
1088
|
+
while (finished != NULL) {
|
|
1089
|
+
Agent *agent = finished;
|
|
1090
|
+
finished = agent->next;
|
|
1091
|
+
agent_thread_join(agent->thread);
|
|
1092
|
+
holes_free(agent->holes, agent->hole_count);
|
|
1093
|
+
free(agent);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
static OseoResult agent_start(
|
|
1098
|
+
OseoContext *context,
|
|
1099
|
+
Agent *self,
|
|
1100
|
+
OseoValue source
|
|
1101
|
+
) {
|
|
1102
|
+
AgentCluster *cluster = self->cluster;
|
|
1103
|
+
cluster_reap(cluster);
|
|
1104
|
+
if (!is_string(source)) {
|
|
1105
|
+
return oseo_internal_throw_error(
|
|
1106
|
+
context,
|
|
1107
|
+
OSEO_ERROR_TYPE,
|
|
1108
|
+
"$262.agent.start needs a source string."
|
|
1109
|
+
);
|
|
1110
|
+
}
|
|
1111
|
+
const OseoString *text = string_object(source);
|
|
1112
|
+
const OseoAgentProgram *program = NULL;
|
|
1113
|
+
AgentHole *holes = NULL;
|
|
1114
|
+
for (size_t index = 0u; index < cluster->program_count; index += 1u) {
|
|
1115
|
+
const OseoAgentProgram *candidate = &cluster->programs[index];
|
|
1116
|
+
size_t count = candidate->segment_count - 1u;
|
|
1117
|
+
AgentHole *values =
|
|
1118
|
+
count == 0u ? NULL : calloc(count, sizeof(AgentHole));
|
|
1119
|
+
bool failed = false;
|
|
1120
|
+
if (count > 0u && values == NULL) failed = true;
|
|
1121
|
+
if (!failed && program_matches(candidate, text, values, &failed)) {
|
|
1122
|
+
program = candidate;
|
|
1123
|
+
holes = values;
|
|
1124
|
+
break;
|
|
1125
|
+
}
|
|
1126
|
+
holes_free(values, count);
|
|
1127
|
+
if (failed) {
|
|
1128
|
+
return failure(context, "OSEO2001", "Agent allocation failed.");
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
if (program == NULL) {
|
|
1132
|
+
return failure(
|
|
1133
|
+
context,
|
|
1134
|
+
"OSEO2001",
|
|
1135
|
+
"Agent source text outside the ahead-of-time agent programs "
|
|
1136
|
+
"is not admitted."
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
size_t hole_count = program->segment_count - 1u;
|
|
1140
|
+
if (!oseo_clock_open(context)) {
|
|
1141
|
+
holes_free(holes, hole_count);
|
|
1142
|
+
return failure(
|
|
1143
|
+
context,
|
|
1144
|
+
"OSEO2001",
|
|
1145
|
+
"Cross-agent wakeup is unavailable."
|
|
1146
|
+
);
|
|
1147
|
+
}
|
|
1148
|
+
Agent *agent = calloc(1u, sizeof(*agent));
|
|
1149
|
+
if (agent == NULL) {
|
|
1150
|
+
holes_free(holes, hole_count);
|
|
1151
|
+
return failure(context, "OSEO2001", "Agent allocation failed.");
|
|
1152
|
+
}
|
|
1153
|
+
agent->cluster = cluster;
|
|
1154
|
+
agent->program = program;
|
|
1155
|
+
agent->holes = holes;
|
|
1156
|
+
agent->hole_count = hole_count;
|
|
1157
|
+
agent->state = AGENT_STARTING;
|
|
1158
|
+
cluster_lock(cluster);
|
|
1159
|
+
if (!agent_thread_start(&agent->thread, agent_thread, agent)) {
|
|
1160
|
+
cluster_unlock(cluster);
|
|
1161
|
+
holes_free(holes, hole_count);
|
|
1162
|
+
free(agent);
|
|
1163
|
+
return failure(context, "OSEO2001", "Agent thread creation failed.");
|
|
1164
|
+
}
|
|
1165
|
+
if (cluster->agents_tail == NULL) {
|
|
1166
|
+
cluster->agents_head = agent;
|
|
1167
|
+
} else {
|
|
1168
|
+
cluster->agents_tail->next = agent;
|
|
1169
|
+
}
|
|
1170
|
+
cluster->agents_tail = agent;
|
|
1171
|
+
/* The start blocks until the new agent is running. */
|
|
1172
|
+
OseoResult result = agent_block(
|
|
1173
|
+
context,
|
|
1174
|
+
self,
|
|
1175
|
+
false,
|
|
1176
|
+
0u,
|
|
1177
|
+
false,
|
|
1178
|
+
NULL,
|
|
1179
|
+
NULL
|
|
1180
|
+
);
|
|
1181
|
+
cluster_unlock(cluster);
|
|
1182
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
1183
|
+
? normal(oseo_undefined())
|
|
1184
|
+
: result;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
/*
|
|
1188
|
+
* $262.agent.broadcast(sab, id). Every agent still evaluating receives
|
|
1189
|
+
* the block, and the main agent blocks until each of them has retrieved
|
|
1190
|
+
* it, which an agent does once it has registered a callback.
|
|
1191
|
+
*/
|
|
1192
|
+
static OseoResult agent_broadcast(
|
|
1193
|
+
OseoContext *context,
|
|
1194
|
+
Agent *self,
|
|
1195
|
+
size_t argument_count,
|
|
1196
|
+
const OseoValue *arguments
|
|
1197
|
+
) {
|
|
1198
|
+
OseoValue buffer = argument_count > 0u ? arguments[0] : oseo_undefined();
|
|
1199
|
+
OseoValue id = argument_count > 1u ? arguments[1] : oseo_undefined();
|
|
1200
|
+
if (!is_array_buffer(buffer) || !array_buffer_object(buffer)->shared) {
|
|
1201
|
+
return oseo_internal_throw_error(
|
|
1202
|
+
context,
|
|
1203
|
+
OSEO_ERROR_TYPE,
|
|
1204
|
+
"$262.agent.broadcast needs a SharedArrayBuffer."
|
|
1205
|
+
);
|
|
1206
|
+
}
|
|
1207
|
+
AgentIdKind kind = AGENT_ID_UNDEFINED;
|
|
1208
|
+
double number = 0.0;
|
|
1209
|
+
uint64_t bits = UINT64_C(0);
|
|
1210
|
+
if (is_number(id)) {
|
|
1211
|
+
kind = AGENT_ID_NUMBER;
|
|
1212
|
+
number = number_value(id);
|
|
1213
|
+
} else if (is_bigint(id)) {
|
|
1214
|
+
const OseoBigInt *value = bigint_object(id);
|
|
1215
|
+
uint64_t magnitude = UINT64_C(0);
|
|
1216
|
+
if (value->length > 0u) magnitude = value->limbs[0];
|
|
1217
|
+
if (value->length > 1u) {
|
|
1218
|
+
magnitude |= (uint64_t)value->limbs[1] << 32u;
|
|
1219
|
+
}
|
|
1220
|
+
if (value->length > 2u ||
|
|
1221
|
+
magnitude > (value->negative ? UINT64_C(0x8000000000000000)
|
|
1222
|
+
: UINT64_C(0x7fffffffffffffff))) {
|
|
1223
|
+
return oseo_internal_throw_error(
|
|
1224
|
+
context,
|
|
1225
|
+
OSEO_ERROR_RANGE,
|
|
1226
|
+
"$262.agent.broadcast id is outside the BigInt64 range."
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
kind = AGENT_ID_BIGINT;
|
|
1230
|
+
bits = oseo_internal_bigint_to_raw_uint64(id);
|
|
1231
|
+
} else if (tag_of(id) != OSEO_TAG_UNDEFINED) {
|
|
1232
|
+
return oseo_internal_throw_error(
|
|
1233
|
+
context,
|
|
1234
|
+
OSEO_ERROR_TYPE,
|
|
1235
|
+
"$262.agent.broadcast id must be a Number or a BigInt."
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
1238
|
+
OseoSharedBlock *block = array_buffer_object(buffer)->block;
|
|
1239
|
+
AgentCluster *cluster = self->cluster;
|
|
1240
|
+
cluster_lock(cluster);
|
|
1241
|
+
size_t delivered = 0u;
|
|
1242
|
+
OseoResult result = normal(oseo_undefined());
|
|
1243
|
+
for (Agent *agent = cluster->agents_head; agent != NULL;
|
|
1244
|
+
agent = agent->next) {
|
|
1245
|
+
if (agent->state == AGENT_FINISHED) continue;
|
|
1246
|
+
AgentBroadcast *broadcast = malloc(sizeof(*broadcast));
|
|
1247
|
+
if (broadcast == NULL) {
|
|
1248
|
+
result = failure(
|
|
1249
|
+
context,
|
|
1250
|
+
"OSEO2001",
|
|
1251
|
+
"Agent broadcast allocation failed."
|
|
1252
|
+
);
|
|
1253
|
+
break;
|
|
1254
|
+
}
|
|
1255
|
+
oseo_internal_shared_block_retain(block);
|
|
1256
|
+
broadcast->next = NULL;
|
|
1257
|
+
broadcast->block = block;
|
|
1258
|
+
broadcast->id_kind = kind;
|
|
1259
|
+
broadcast->id_number = number;
|
|
1260
|
+
broadcast->id_bits = bits;
|
|
1261
|
+
if (agent->broadcast_tail == NULL) {
|
|
1262
|
+
agent->broadcast_head = broadcast;
|
|
1263
|
+
} else {
|
|
1264
|
+
agent->broadcast_tail->next = broadcast;
|
|
1265
|
+
}
|
|
1266
|
+
agent->broadcast_tail = broadcast;
|
|
1267
|
+
delivered += 1u;
|
|
1268
|
+
if (agent->state == AGENT_BLOCKED && agent->wakes_on_work) {
|
|
1269
|
+
agent_make_ready(agent);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
if (delivered > 0u) {
|
|
1273
|
+
cluster->broadcast_remaining += delivered;
|
|
1274
|
+
OseoResult blocked = agent_block(
|
|
1275
|
+
context,
|
|
1276
|
+
self,
|
|
1277
|
+
false,
|
|
1278
|
+
0u,
|
|
1279
|
+
false,
|
|
1280
|
+
NULL,
|
|
1281
|
+
NULL
|
|
1282
|
+
);
|
|
1283
|
+
if (result.status == OSEO_STATUS_NORMAL) result = blocked;
|
|
1284
|
+
/* Reaching the turn again means every delivery was retrieved or
|
|
1285
|
+
* dropped by an agent that finished first. */
|
|
1286
|
+
}
|
|
1287
|
+
cluster_unlock(cluster);
|
|
1288
|
+
return result;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
static OseoResult agent_get_report(OseoContext *context, Agent *self) {
|
|
1292
|
+
OseoResult result = oseo_internal_agent_yield(context);
|
|
1293
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1294
|
+
AgentCluster *cluster = self->cluster;
|
|
1295
|
+
cluster_lock(cluster);
|
|
1296
|
+
AgentReport *report = cluster->reports_head;
|
|
1297
|
+
if (report != NULL) {
|
|
1298
|
+
cluster->reports_head = report->next;
|
|
1299
|
+
if (cluster->reports_head == NULL) cluster->reports_tail = NULL;
|
|
1300
|
+
}
|
|
1301
|
+
cluster_unlock(cluster);
|
|
1302
|
+
if (report == NULL) return normal(oseo_null());
|
|
1303
|
+
result = oseo_string_from_units(context, report->units, report->length);
|
|
1304
|
+
free(report);
|
|
1305
|
+
return result;
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
static OseoResult agent_report(
|
|
1309
|
+
OseoContext *context,
|
|
1310
|
+
Agent *self,
|
|
1311
|
+
OseoValue value
|
|
1312
|
+
) {
|
|
1313
|
+
OseoResult result = oseo_to_string(context, value);
|
|
1314
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1315
|
+
const OseoString *text = string_object(result.value);
|
|
1316
|
+
if (text->length > (SIZE_MAX - sizeof(AgentReport)) / sizeof(uint16_t)) {
|
|
1317
|
+
return failure(context, "OSEO2001", "Agent report allocation failed.");
|
|
1318
|
+
}
|
|
1319
|
+
AgentReport *report =
|
|
1320
|
+
malloc(sizeof(*report) + text->length * sizeof(uint16_t));
|
|
1321
|
+
if (report == NULL) {
|
|
1322
|
+
return failure(context, "OSEO2001", "Agent report allocation failed.");
|
|
1323
|
+
}
|
|
1324
|
+
report->next = NULL;
|
|
1325
|
+
report->length = text->length;
|
|
1326
|
+
if (text->length > 0u) {
|
|
1327
|
+
memcpy(report->units, text->units, text->length * sizeof(uint16_t));
|
|
1328
|
+
}
|
|
1329
|
+
AgentCluster *cluster = self->cluster;
|
|
1330
|
+
cluster_lock(cluster);
|
|
1331
|
+
if (cluster->reports_tail == NULL) {
|
|
1332
|
+
cluster->reports_head = report;
|
|
1333
|
+
} else {
|
|
1334
|
+
cluster->reports_tail->next = report;
|
|
1335
|
+
}
|
|
1336
|
+
cluster->reports_tail = report;
|
|
1337
|
+
cluster_unlock(cluster);
|
|
1338
|
+
return normal(oseo_undefined());
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
static OseoResult agent_sleep(
|
|
1342
|
+
OseoContext *context,
|
|
1343
|
+
Agent *self,
|
|
1344
|
+
OseoValue value
|
|
1345
|
+
) {
|
|
1346
|
+
OseoResult result = oseo_internal_to_number(context, value);
|
|
1347
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1348
|
+
double milliseconds = number_value(result.value);
|
|
1349
|
+
if (isnan(milliseconds)) milliseconds = 0.0;
|
|
1350
|
+
if (milliseconds == INFINITY) {
|
|
1351
|
+
return oseo_internal_throw_error(
|
|
1352
|
+
context,
|
|
1353
|
+
OSEO_ERROR_RANGE,
|
|
1354
|
+
"$262.agent.sleep needs a finite time."
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
uint64_t deadline = 0u;
|
|
1358
|
+
result = agent_deadline(context, milliseconds, &deadline);
|
|
1359
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1360
|
+
AgentCluster *cluster = self->cluster;
|
|
1361
|
+
cluster_lock(cluster);
|
|
1362
|
+
result = agent_block(context, self, true, deadline, false, NULL, NULL);
|
|
1363
|
+
cluster_unlock(cluster);
|
|
1364
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
1365
|
+
? normal(oseo_undefined())
|
|
1366
|
+
: result;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
static OseoResult agent_monotonic_now(OseoContext *context, Agent *self) {
|
|
1370
|
+
OseoResult result = oseo_internal_clock_start(context);
|
|
1371
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1372
|
+
uint64_t now = 0u;
|
|
1373
|
+
if (!context->clock_adapter->monotonic(context->clock_state, &now)) {
|
|
1374
|
+
return failure(
|
|
1375
|
+
context,
|
|
1376
|
+
"OSEO2001",
|
|
1377
|
+
"The host monotonic clock is unavailable."
|
|
1378
|
+
);
|
|
1379
|
+
}
|
|
1380
|
+
uint64_t origin = self->cluster->origin;
|
|
1381
|
+
double elapsed = now > origin ? (double)(now - origin) : 0.0;
|
|
1382
|
+
return normal(oseo_number(elapsed / AGENT_NANOSECONDS_PER_MILLISECOND));
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
OseoResult oseo_internal_agent_builtin_dispatch(
|
|
1386
|
+
OseoContext *context,
|
|
1387
|
+
size_t code_id,
|
|
1388
|
+
OseoValue callee,
|
|
1389
|
+
OseoValue receiver,
|
|
1390
|
+
size_t argument_count,
|
|
1391
|
+
const OseoValue *arguments,
|
|
1392
|
+
OseoValue new_target
|
|
1393
|
+
) {
|
|
1394
|
+
(void)callee;
|
|
1395
|
+
(void)receiver;
|
|
1396
|
+
if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
|
|
1397
|
+
return oseo_internal_throw_error(
|
|
1398
|
+
context,
|
|
1399
|
+
OSEO_ERROR_TYPE,
|
|
1400
|
+
"$262.agent function is not a constructor."
|
|
1401
|
+
);
|
|
1402
|
+
}
|
|
1403
|
+
Agent *self = agent_of(context);
|
|
1404
|
+
if (self == NULL) {
|
|
1405
|
+
return failure(
|
|
1406
|
+
context,
|
|
1407
|
+
"OSEO2001",
|
|
1408
|
+
"The test262 agent host is not installed."
|
|
1409
|
+
);
|
|
1410
|
+
}
|
|
1411
|
+
OseoValue first = argument_count > 0u ? arguments[0] : oseo_undefined();
|
|
1412
|
+
switch (code_id) {
|
|
1413
|
+
case OSEO_AGENT_START_CODE_ID:
|
|
1414
|
+
return agent_start(context, self, first);
|
|
1415
|
+
case OSEO_AGENT_BROADCAST_CODE_ID:
|
|
1416
|
+
return agent_broadcast(context, self, argument_count, arguments);
|
|
1417
|
+
case OSEO_AGENT_GET_REPORT_CODE_ID:
|
|
1418
|
+
return agent_get_report(context, self);
|
|
1419
|
+
case OSEO_AGENT_SLEEP_CODE_ID:
|
|
1420
|
+
return agent_sleep(context, self, first);
|
|
1421
|
+
case OSEO_AGENT_MONOTONIC_NOW_CODE_ID:
|
|
1422
|
+
return agent_monotonic_now(context, self);
|
|
1423
|
+
case OSEO_AGENT_RECEIVE_BROADCAST_CODE_ID:
|
|
1424
|
+
if (!is_callable(first)) {
|
|
1425
|
+
return oseo_internal_throw_error(
|
|
1426
|
+
context,
|
|
1427
|
+
OSEO_ERROR_TYPE,
|
|
1428
|
+
"$262.agent.receiveBroadcast needs a function."
|
|
1429
|
+
);
|
|
1430
|
+
}
|
|
1431
|
+
context->agent_broadcast_callback = first;
|
|
1432
|
+
return normal(oseo_undefined());
|
|
1433
|
+
case OSEO_AGENT_REPORT_CODE_ID:
|
|
1434
|
+
return agent_report(context, self, first);
|
|
1435
|
+
case OSEO_AGENT_LEAVING_CODE_ID:
|
|
1436
|
+
self->leaving = true;
|
|
1437
|
+
return normal(oseo_undefined());
|
|
1438
|
+
default:
|
|
1439
|
+
return oseo_unknown_function(context, code_id);
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
typedef struct {
|
|
1445
|
+
const char *name;
|
|
1446
|
+
size_t code_id;
|
|
1447
|
+
size_t length;
|
|
1448
|
+
} AgentFunction;
|
|
1449
|
+
|
|
1450
|
+
static const AgentFunction main_functions[] = {
|
|
1451
|
+
{"start", OSEO_AGENT_START_CODE_ID, 1u},
|
|
1452
|
+
{"broadcast", OSEO_AGENT_BROADCAST_CODE_ID, 2u},
|
|
1453
|
+
{"getReport", OSEO_AGENT_GET_REPORT_CODE_ID, 0u},
|
|
1454
|
+
{"sleep", OSEO_AGENT_SLEEP_CODE_ID, 1u},
|
|
1455
|
+
{"monotonicNow", OSEO_AGENT_MONOTONIC_NOW_CODE_ID, 0u},
|
|
1456
|
+
};
|
|
1457
|
+
|
|
1458
|
+
static const AgentFunction agent_functions[] = {
|
|
1459
|
+
{"receiveBroadcast", OSEO_AGENT_RECEIVE_BROADCAST_CODE_ID, 1u},
|
|
1460
|
+
{"report", OSEO_AGENT_REPORT_CODE_ID, 1u},
|
|
1461
|
+
{"leaving", OSEO_AGENT_LEAVING_CODE_ID, 0u},
|
|
1462
|
+
{"sleep", OSEO_AGENT_SLEEP_CODE_ID, 1u},
|
|
1463
|
+
{"monotonicNow", OSEO_AGENT_MONOTONIC_NOW_CODE_ID, 0u},
|
|
1464
|
+
};
|
|
1465
|
+
|
|
1466
|
+
static OseoResult define_host_property(
|
|
1467
|
+
OseoContext *context,
|
|
1468
|
+
OseoValue object,
|
|
1469
|
+
const char *name,
|
|
1470
|
+
OseoValue value
|
|
1471
|
+
) {
|
|
1472
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
1473
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1474
|
+
oseo_roots_push(context, &frame);
|
|
1475
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
1476
|
+
slots[2] = result.value;
|
|
1477
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1478
|
+
result = oseo_object_define(
|
|
1479
|
+
context,
|
|
1480
|
+
slots[0],
|
|
1481
|
+
slots[2],
|
|
1482
|
+
slots[1],
|
|
1483
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1484
|
+
);
|
|
1485
|
+
}
|
|
1486
|
+
oseo_roots_pop(context, &frame);
|
|
1487
|
+
return result;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
static OseoResult create_host_function(
|
|
1491
|
+
OseoContext *context,
|
|
1492
|
+
const AgentFunction *entry
|
|
1493
|
+
) {
|
|
1494
|
+
size_t name_length = strlen(entry->name);
|
|
1495
|
+
uint16_t units[24];
|
|
1496
|
+
if (name_length > sizeof(units) / sizeof(*units)) {
|
|
1497
|
+
return failure(context, "OSEO2001", "Built-in name is too long.");
|
|
1498
|
+
}
|
|
1499
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
1500
|
+
units[index] = (uint16_t)(unsigned char)entry->name[index];
|
|
1501
|
+
}
|
|
1502
|
+
OseoValue environment = oseo_undefined();
|
|
1503
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
1504
|
+
oseo_roots_push(context, &frame);
|
|
1505
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
1506
|
+
environment = result.value;
|
|
1507
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1508
|
+
result = oseo_function_create(
|
|
1509
|
+
context,
|
|
1510
|
+
entry->code_id,
|
|
1511
|
+
environment,
|
|
1512
|
+
units,
|
|
1513
|
+
name_length,
|
|
1514
|
+
entry->length,
|
|
1515
|
+
OSEO_FUNCTION_INTERNAL,
|
|
1516
|
+
oseo_undefined(),
|
|
1517
|
+
oseo_undefined(),
|
|
1518
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
1519
|
+
);
|
|
1520
|
+
}
|
|
1521
|
+
oseo_roots_pop(context, &frame);
|
|
1522
|
+
return result;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
/*
|
|
1526
|
+
* Installs `$262` on this realm's global object: an ordinary object whose
|
|
1527
|
+
* `agent` object holds the main or the agent function set, and whose
|
|
1528
|
+
* `createRealm` and `global` members come from the realm component. All
|
|
1529
|
+
* are writable and configurable, so a harness include can replace a
|
|
1530
|
+
* member as the upstream atomicsHelper.js replaces `getReport`.
|
|
1531
|
+
*/
|
|
1532
|
+
static OseoResult install_host(OseoContext *context, bool main) {
|
|
1533
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1534
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 4u);
|
|
1535
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1536
|
+
result = oseo_this_value(context, oseo_undefined());
|
|
1537
|
+
frame.slots[0] = result.value;
|
|
1538
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1539
|
+
result = oseo_internal_intrinsic(
|
|
1540
|
+
context,
|
|
1541
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
1542
|
+
);
|
|
1543
|
+
frame.slots[1] = result.value;
|
|
1544
|
+
}
|
|
1545
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1546
|
+
result = oseo_object_create(context, frame.slots[1]);
|
|
1547
|
+
frame.slots[2] = result.value;
|
|
1548
|
+
}
|
|
1549
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1550
|
+
result = oseo_object_create(context, frame.slots[1]);
|
|
1551
|
+
frame.slots[1] = result.value;
|
|
1552
|
+
}
|
|
1553
|
+
const AgentFunction *functions = main ? main_functions : agent_functions;
|
|
1554
|
+
size_t count = main
|
|
1555
|
+
? sizeof(main_functions) / sizeof(*main_functions)
|
|
1556
|
+
: sizeof(agent_functions) / sizeof(*agent_functions);
|
|
1557
|
+
for (size_t index = 0u;
|
|
1558
|
+
result.status == OSEO_STATUS_NORMAL && index < count;
|
|
1559
|
+
index += 1u) {
|
|
1560
|
+
result = create_host_function(context, &functions[index]);
|
|
1561
|
+
frame.slots[3] = result.value;
|
|
1562
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1563
|
+
result = define_host_property(
|
|
1564
|
+
context,
|
|
1565
|
+
frame.slots[1],
|
|
1566
|
+
functions[index].name,
|
|
1567
|
+
frame.slots[3]
|
|
1568
|
+
);
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1572
|
+
result = define_host_property(
|
|
1573
|
+
context,
|
|
1574
|
+
frame.slots[2],
|
|
1575
|
+
"agent",
|
|
1576
|
+
frame.slots[1]
|
|
1577
|
+
);
|
|
1578
|
+
}
|
|
1579
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1580
|
+
result = oseo_internal_realm_host_install(context, frame.slots[2]);
|
|
1581
|
+
}
|
|
1582
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1583
|
+
result = define_host_property(
|
|
1584
|
+
context,
|
|
1585
|
+
frame.slots[0],
|
|
1586
|
+
"$262",
|
|
1587
|
+
frame.slots[2]
|
|
1588
|
+
);
|
|
1589
|
+
}
|
|
1590
|
+
oseo_roots_release(context, &frame);
|
|
1591
|
+
return result;
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
OseoResult oseo_test262_host_install(
|
|
1595
|
+
OseoContext *context,
|
|
1596
|
+
const OseoAgentProgram *programs,
|
|
1597
|
+
size_t program_count
|
|
1598
|
+
) {
|
|
1599
|
+
if (context->agent != NULL) {
|
|
1600
|
+
return failure(
|
|
1601
|
+
context,
|
|
1602
|
+
"OSEO2001",
|
|
1603
|
+
"The test262 agent host is already installed."
|
|
1604
|
+
);
|
|
1605
|
+
}
|
|
1606
|
+
AgentCluster *cluster = calloc(1u, sizeof(*cluster));
|
|
1607
|
+
Agent *main = calloc(1u, sizeof(*main));
|
|
1608
|
+
if (cluster == NULL || main == NULL ||
|
|
1609
|
+
!agent_mutex_init(&cluster->lock)) {
|
|
1610
|
+
free(cluster);
|
|
1611
|
+
free(main);
|
|
1612
|
+
return failure(context, "OSEO2001", "Agent cluster allocation failed.");
|
|
1613
|
+
}
|
|
1614
|
+
OseoResult result = oseo_internal_clock_start(context);
|
|
1615
|
+
uint64_t origin = 0u;
|
|
1616
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1617
|
+
!context->clock_adapter->monotonic(context->clock_state, &origin)) {
|
|
1618
|
+
result = failure(
|
|
1619
|
+
context,
|
|
1620
|
+
"OSEO2001",
|
|
1621
|
+
"The host monotonic clock is unavailable."
|
|
1622
|
+
);
|
|
1623
|
+
}
|
|
1624
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1625
|
+
agent_mutex_destroy(&cluster->lock);
|
|
1626
|
+
free(cluster);
|
|
1627
|
+
free(main);
|
|
1628
|
+
return result;
|
|
1629
|
+
}
|
|
1630
|
+
cluster->origin = origin;
|
|
1631
|
+
cluster->unhandled_rejection_policy = context->unhandled_rejection_policy;
|
|
1632
|
+
cluster->programs = programs;
|
|
1633
|
+
cluster->program_count = program_count;
|
|
1634
|
+
cluster->main = main;
|
|
1635
|
+
cluster->running = main;
|
|
1636
|
+
main->cluster = cluster;
|
|
1637
|
+
main->context = context;
|
|
1638
|
+
main->state = AGENT_RUNNING;
|
|
1639
|
+
main->main = true;
|
|
1640
|
+
context->agent = main;
|
|
1641
|
+
return install_host(context, true);
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
OseoResult oseo_agent_hole(OseoContext *context, size_t index) {
|
|
1645
|
+
const Agent *self = agent_of(context);
|
|
1646
|
+
if (self == NULL || index >= self->hole_count) {
|
|
1647
|
+
return failure(
|
|
1648
|
+
context,
|
|
1649
|
+
"OSEO2001",
|
|
1650
|
+
"The agent program has no such source hole."
|
|
1651
|
+
);
|
|
1652
|
+
}
|
|
1653
|
+
const AgentHole *hole = &self->holes[index];
|
|
1654
|
+
if (hole->bigint) return oseo_bigint_literal(context, hole->digits, 10u);
|
|
1655
|
+
return normal(oseo_number(hole->number));
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
OseoResult oseo_agent_function_set_source(
|
|
1659
|
+
OseoContext *context,
|
|
1660
|
+
OseoValue function_value,
|
|
1661
|
+
const OseoAgentSegment *runs,
|
|
1662
|
+
size_t run_count,
|
|
1663
|
+
const size_t *holes
|
|
1664
|
+
) {
|
|
1665
|
+
const Agent *self = agent_of(context);
|
|
1666
|
+
size_t length = 0u;
|
|
1667
|
+
for (size_t index = 0u; index < run_count; index += 1u) {
|
|
1668
|
+
length += runs[index].length;
|
|
1669
|
+
if (index + 1u == run_count) break;
|
|
1670
|
+
if (self == NULL || holes[index] >= self->hole_count) {
|
|
1671
|
+
return failure(
|
|
1672
|
+
context,
|
|
1673
|
+
"OSEO2001",
|
|
1674
|
+
"The agent program has no such source hole."
|
|
1675
|
+
);
|
|
1676
|
+
}
|
|
1677
|
+
const AgentHole *hole = &self->holes[holes[index]];
|
|
1678
|
+
length += strlen(hole->digits) + (hole->bigint ? 1u : 0u);
|
|
1679
|
+
}
|
|
1680
|
+
uint16_t *units = malloc((length == 0u ? 1u : length) * sizeof(uint16_t));
|
|
1681
|
+
if (units == NULL) {
|
|
1682
|
+
return failure(
|
|
1683
|
+
context,
|
|
1684
|
+
"OSEO2001",
|
|
1685
|
+
"Function source allocation failed."
|
|
1686
|
+
);
|
|
1687
|
+
}
|
|
1688
|
+
size_t position = 0u;
|
|
1689
|
+
for (size_t index = 0u; index < run_count; index += 1u) {
|
|
1690
|
+
if (runs[index].length > 0u) {
|
|
1691
|
+
memcpy(
|
|
1692
|
+
units + position,
|
|
1693
|
+
runs[index].units,
|
|
1694
|
+
runs[index].length * sizeof(uint16_t)
|
|
1695
|
+
);
|
|
1696
|
+
position += runs[index].length;
|
|
1697
|
+
}
|
|
1698
|
+
if (index + 1u == run_count) break;
|
|
1699
|
+
const AgentHole *hole = &self->holes[holes[index]];
|
|
1700
|
+
for (const char *digit = hole->digits; *digit != '\0'; digit += 1) {
|
|
1701
|
+
units[position] = (uint16_t)(unsigned char)*digit;
|
|
1702
|
+
position += 1u;
|
|
1703
|
+
}
|
|
1704
|
+
if (hole->bigint) {
|
|
1705
|
+
units[position] = (uint16_t)'n';
|
|
1706
|
+
position += 1u;
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
OseoResult result = oseo_function_set_source(
|
|
1710
|
+
context,
|
|
1711
|
+
function_value,
|
|
1712
|
+
units,
|
|
1713
|
+
length
|
|
1714
|
+
);
|
|
1715
|
+
free(units);
|
|
1716
|
+
return result;
|
|
1717
|
+
}
|