interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / native / v2 / dev / context.c

119 lines · 5002 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#define _GNU_SOURCE
3#include "omega_native.h"
4#include <assert.h>
5#include <errno.h>
6#include <pthread.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <sys/mman.h>
10#include <unistd.h>
11
12/* WolframLanguageEvaluator: {2^36,2^36+8192,2^40,2^13-1}
13 * -> {68719476736,68719484928,1099511627776,8191}. CPU VA only, no GPU. */
14static const uintptr_t foreign_va = UINT64_C(68719476736);
15static const uintptr_t owned_va = UINT64_C(68719484928);
16static uint64_t handles[4098];
17static uint64_t anon_map(omega_native_context *c)
18{
19 uint64_t h = 0;
20 assert(omega2_ctx_anon(c, &h) == OMEGA_OK && (int64_t)h > 0);
21 return h;
22}
23static void drop(omega_native_context *c, uint64_t h)
24{
25 uint64_t r = 99;
26 assert(omega2_ctx_drop(c, &r, h) == OMEGA_OK && r == 0);
27}
28static void stale(omega_native_context *c, uint64_t h)
29{
30 uint64_t r = 99;
31 assert(omega2_ctx_address(c, &r, h) == 3 && r == 99);
32 assert(omega2_ctx_attach(c, &r, h, (uint64_t)-1) == 3 && r == 99);
33 assert(omega2_ctx_drop(c, &r, h) == 3 && r == 99);
34}
35static void *worker(void *p)
36{
37 omega_native_context *c = p;
38 for (unsigned i = 0; i < 256; i++) {
39 uint64_t h = anon_map(c), r = 0;
40 assert(omega2_ctx_publish(c, &r, h, i) == OMEGA_OK);
41 assert(omega2_ctx_fetch(c, &r, h) == OMEGA_OK && r == i + 7);
42 drop(c, h);
43 }
44 return 0;
45}
46int main(void)
47{
48 omega_native_context *a = (void *)1, *b = 0;
49 assert(omega_native_context_create(0, &a) == OMEGA_BAD_ARG && a == (void *)1);
50 assert(omega_native_context_create(4099, &a) == OMEGA_BAD_ARG && a == (void *)1);
51 assert(omega_native_context_create(1, 0) == OMEGA_BAD_ARG);
52 assert(omega_native_context_destroy(0) == OMEGA_BAD_ARG);
53 assert(omega_native_context_create(4098, &a) == OMEGA_OK);
54 assert(omega_native_context_create(2, &b) == OMEGA_OK);
55 uint64_t r = 99;
56 assert(omega2_ctx_anon(0, &r) == OMEGA_BAD_ARG && r == 99);
57 uint64_t ah = anon_map(a), bh = anon_map(b);
58 assert(ah != bh);
59 stale(a, bh);
60 stale(b, ah);
61 assert(omega_native_context_destroy(a) == OMEGA_CONTEXT_BUSY);
62 assert(omega2_ctx_address(a, &r, ah) == OMEGA_OK && r != 0);
63 drop(a, ah);
64 drop(b, bh);
65 for (unsigned i = 0; i < 4098; i++)
66 handles[i] = anon_map(a);
67 assert(omega2_ctx_anon(a, &r) == OMEGA_OK && (int64_t)r == -EMFILE);
68 uint64_t old = handles[0];
69 drop(a, old);
70 handles[0] = anon_map(a);
71 assert(handles[0] != old && (handles[0] & 8191) == (old & 8191));
72 stale(a, old);
73 for (unsigned i = 0; i < 4098; i++)
74 drop(a, handles[i]);
75 puts("PASS context capacity4098, exhaustion, reuse, cross-context and live-destroy controls");
76
77 int fd = memfd_create("omega-context-fixture", 0);
78 assert(fd >= 0 && ftruncate(fd, 4096) == 0);
79 uint32_t *alias = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
80 assert(alias != MAP_FAILED);
81 unsigned char *foreign = mmap((void *)foreign_va, 4096, PROT_READ | PROT_WRITE,
82 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);
83 assert(foreign == (void *)foreign_va);
84 foreign[0] = 42;
85 r = 99;
86 assert(omega2_ctx_attach(a, &r, foreign_va, fd) == 3 && r == 99 && foreign[0] == 42);
87 assert(omega2_ctx_reserve(a, &r, foreign_va, 4096) == OMEGA_OK && (int64_t)r == -EEXIST);
88 assert(foreign[0] == 42);
89 ah = anon_map(a);
90 assert(omega2_ctx_publish(a, &r, ah, 123) == OMEGA_OK);
91 r = 99;
92 assert(omega2_ctx_attach(a, &r, ah, fd) == 3 && r == 99);
93 assert(omega2_ctx_rd(a, &r, ah) == OMEGA_OK && r == 1);
94 drop(a, ah);
95 assert(omega2_ctx_reserve(a, &r, owned_va, 4096) == OMEGA_OK && (int64_t)r > 0);
96 ah = r;
97 assert(omega2_ctx_address(a, &r, ah) == OMEGA_OK && r == owned_va);
98 assert(omega_native_context_destroy(a) == OMEGA_CONTEXT_BUSY);
99 assert(omega2_ctx_attach(a, &r, ah, fd) == OMEGA_OK && r == 0);
100 alias[0] = 0xabcdefu;
101 assert(omega2_ctx_rd(a, &r, ah) == OMEGA_OK && r == 0xabcdefu);
102 drop(a, ah);
103 assert(omega2_ctx_reserve(a, &r, owned_va, 4096) == OMEGA_OK && (int64_t)r > 0);
104 ah = r;
105 assert(omega2_ctx_attach(a, &r, ah, (uint64_t)-1) == OMEGA_OK && (int64_t)r == -EBADF);
106 stale(a, ah);
107 unsigned char residency;
108 errno = 0;
109 assert(mincore((void *)owned_va, 4096, &residency) == -1 && errno == ENOMEM);
110 assert(munmap(foreign, 4096) == 0 && munmap(alias, 4096) == 0 && close(fd) == 0);
111 puts("PASS owned fixed bind, foreign/data-map refusal, failed-bind retirement and cleanup controls");
112 pthread_t x, y;
113 assert(pthread_create(&x, 0, worker, a) == 0 && pthread_create(&y, 0, worker, b) == 0);
114 assert(pthread_join(x, 0) == 0 && pthread_join(y, 0) == 0);
115 assert(omega_native_context_destroy(a) == OMEGA_OK);
116 assert(omega_native_context_destroy(b) == OMEGA_OK);
117 puts("NATIVE-CONTEXT PASS capacity4098, owned fixed bind, retirement, context ownership and concurrent contexts");
118 return 0;
119}