knitting 0.1.46 → 0.1.50
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/README.md +274 -77
- package/map.md +9 -3
- package/package.json +2 -1
- package/prebuilds/darwin-arm64-node-127/knitting_shared_memory.node +0 -0
- package/prebuilds/darwin-arm64-node-137/knitting_shared_memory.node +0 -0
- package/prebuilds/darwin-x64-node-127/knitting_shared_memory.node +0 -0
- package/prebuilds/darwin-x64-node-137/knitting_shared_memory.node +0 -0
- package/prebuilds/linux-x64-node-127/knitting_shared_memory.node +0 -0
- package/prebuilds/linux-x64-node-137/knitting_shared_memory.node +0 -0
- package/prebuilds/win32-x64/knitting_windows_shared_memory.dll +0 -0
- package/prebuilds/win32-x64-node-127/knitting_shared_memory.node +0 -0
- package/prebuilds/win32-x64-node-127/knitting_shm.node +0 -0
- package/prebuilds/win32-x64-node-137/knitting_shared_memory.node +0 -0
- package/prebuilds/win32-x64-node-137/knitting_shm.node +0 -0
- package/scripts/build-native-addons.ts +31 -5
- package/src/api.js +37 -13
- package/src/common/task-source.d.ts +1 -0
- package/src/common/task-source.js +1 -0
- package/src/connections/bun.d.ts +2 -0
- package/src/connections/bun.js +64 -9
- package/src/connections/deno.d.ts +2 -0
- package/src/connections/deno.js +64 -9
- package/src/connections/file-descriptor.d.ts +2 -0
- package/src/connections/file-descriptor.js +24 -2
- package/src/connections/node.d.ts +2 -1
- package/src/connections/node.js +17 -14
- package/src/connections/posix.d.ts +1 -0
- package/src/connections/posix.js +6 -0
- package/src/connections/process-shared-buffer.d.ts +3 -1
- package/src/connections/process-shared-buffer.js +17 -13
- package/src/connections/types.d.ts +2 -0
- package/src/connections/windows.d.ts +28 -0
- package/src/connections/windows.js +224 -0
- package/src/knitting_shared_memory.cc +310 -24
- package/src/knitting_windows_shared_memory.cc +156 -0
- package/src/memory/payloadCodec.js +1 -0
- package/src/runtime/pool.d.ts +1 -13
- package/src/runtime/pool.js +10 -542
- package/src/runtime/process-worker.d.ts +92 -0
- package/src/runtime/process-worker.js +670 -0
- package/src/runtime/tx-queue.d.ts +1 -1
- package/src/runtime/tx-queue.js +3 -1
- package/src/shared/abortSignal.d.ts +1 -1
- package/src/shared/abortSignal.js +10 -2
- package/src/types.d.ts +49 -3
- package/src/worker/bootstrap.d.ts +5 -0
- package/src/worker/bootstrap.js +78 -0
- package/src/worker/composable-runners.js +0 -8
- package/src/worker/loop.js +19 -154
- package/src/worker/process-worker-bootstrap.d.ts +8 -0
- package/src/worker/process-worker-bootstrap.js +159 -0
- package/src/worker/timers.js +19 -5
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
#if !defined(_WIN32)
|
|
2
|
+
#error "knitting_windows_shared_memory.cc only supports Windows."
|
|
3
|
+
#endif
|
|
4
|
+
|
|
5
|
+
#define WIN32_LEAN_AND_MEAN
|
|
6
|
+
#include <windows.h>
|
|
7
|
+
|
|
8
|
+
#include <cstdint>
|
|
9
|
+
|
|
10
|
+
namespace {
|
|
11
|
+
|
|
12
|
+
constexpr uint32_t MODE_CREATE = 1;
|
|
13
|
+
constexpr uint32_t MODE_OPEN = 2;
|
|
14
|
+
constexpr size_t CACHE_LINE_SIZE = 64;
|
|
15
|
+
|
|
16
|
+
struct KnittingWindowsSharedMemory {
|
|
17
|
+
void* address;
|
|
18
|
+
uintptr_t handle;
|
|
19
|
+
uint64_t size;
|
|
20
|
+
uint32_t base_address_mod64;
|
|
21
|
+
uint32_t reserved;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static_assert(sizeof(KnittingWindowsSharedMemory) == 32);
|
|
25
|
+
|
|
26
|
+
bool HasName(const wchar_t* name) {
|
|
27
|
+
return name != nullptr && name[0] != L'\0';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
uint32_t LastErrorOrInvalidParameter() {
|
|
31
|
+
DWORD error = GetLastError();
|
|
32
|
+
return error == ERROR_SUCCESS ? ERROR_INVALID_PARAMETER : error;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
uint32_t FillMapping(
|
|
36
|
+
HANDLE handle,
|
|
37
|
+
uint64_t size,
|
|
38
|
+
KnittingWindowsSharedMemory* out
|
|
39
|
+
) {
|
|
40
|
+
if (handle == nullptr || out == nullptr || size == 0) {
|
|
41
|
+
if (handle != nullptr) CloseHandle(handle);
|
|
42
|
+
return ERROR_INVALID_PARAMETER;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
void* address = MapViewOfFile(
|
|
46
|
+
handle,
|
|
47
|
+
FILE_MAP_ALL_ACCESS,
|
|
48
|
+
0,
|
|
49
|
+
0,
|
|
50
|
+
static_cast<SIZE_T>(size)
|
|
51
|
+
);
|
|
52
|
+
if (address == nullptr) {
|
|
53
|
+
DWORD error = GetLastError();
|
|
54
|
+
CloseHandle(handle);
|
|
55
|
+
return error == ERROR_SUCCESS ? ERROR_INVALID_PARAMETER : error;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
out->address = address;
|
|
59
|
+
out->handle = reinterpret_cast<uintptr_t>(handle);
|
|
60
|
+
out->size = size;
|
|
61
|
+
out->base_address_mod64 =
|
|
62
|
+
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(address) % CACHE_LINE_SIZE);
|
|
63
|
+
out->reserved = 0;
|
|
64
|
+
return ERROR_SUCCESS;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
} // namespace
|
|
68
|
+
|
|
69
|
+
extern "C" __declspec(dllexport) uint32_t
|
|
70
|
+
knitting_windows_create_shared_memory(
|
|
71
|
+
uint64_t size,
|
|
72
|
+
const wchar_t* name,
|
|
73
|
+
uint32_t mode,
|
|
74
|
+
KnittingWindowsSharedMemory* out
|
|
75
|
+
) {
|
|
76
|
+
if (!HasName(name) || size == 0 || out == nullptr) {
|
|
77
|
+
return ERROR_INVALID_PARAMETER;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
HANDLE handle = nullptr;
|
|
81
|
+
if (mode == MODE_OPEN) {
|
|
82
|
+
handle = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, name);
|
|
83
|
+
if (handle == nullptr) return LastErrorOrInvalidParameter();
|
|
84
|
+
} else if (mode == MODE_CREATE) {
|
|
85
|
+
handle = CreateFileMappingW(
|
|
86
|
+
INVALID_HANDLE_VALUE,
|
|
87
|
+
nullptr,
|
|
88
|
+
PAGE_READWRITE,
|
|
89
|
+
static_cast<DWORD>(size >> 32),
|
|
90
|
+
static_cast<DWORD>(size & 0xffffffffULL),
|
|
91
|
+
name
|
|
92
|
+
);
|
|
93
|
+
if (handle == nullptr) return LastErrorOrInvalidParameter();
|
|
94
|
+
if (GetLastError() == ERROR_ALREADY_EXISTS) {
|
|
95
|
+
CloseHandle(handle);
|
|
96
|
+
return ERROR_ALREADY_EXISTS;
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
return ERROR_INVALID_PARAMETER;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return FillMapping(handle, size, out);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
extern "C" __declspec(dllexport) uint32_t
|
|
106
|
+
knitting_windows_map_shared_memory(
|
|
107
|
+
uintptr_t handle_value,
|
|
108
|
+
uint64_t size,
|
|
109
|
+
const wchar_t* name,
|
|
110
|
+
KnittingWindowsSharedMemory* out
|
|
111
|
+
) {
|
|
112
|
+
HANDLE handle = nullptr;
|
|
113
|
+
if (HasName(name)) {
|
|
114
|
+
handle = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, name);
|
|
115
|
+
if (handle == nullptr) return LastErrorOrInvalidParameter();
|
|
116
|
+
} else if (handle_value != 0) {
|
|
117
|
+
BOOL ok = DuplicateHandle(
|
|
118
|
+
GetCurrentProcess(),
|
|
119
|
+
reinterpret_cast<HANDLE>(handle_value),
|
|
120
|
+
GetCurrentProcess(),
|
|
121
|
+
&handle,
|
|
122
|
+
0,
|
|
123
|
+
FALSE,
|
|
124
|
+
DUPLICATE_SAME_ACCESS
|
|
125
|
+
);
|
|
126
|
+
if (!ok) return LastErrorOrInvalidParameter();
|
|
127
|
+
} else {
|
|
128
|
+
return ERROR_INVALID_PARAMETER;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return FillMapping(handle, size, out);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
extern "C" __declspec(dllexport) uint32_t
|
|
135
|
+
knitting_windows_close_shared_memory(KnittingWindowsSharedMemory* mapping) {
|
|
136
|
+
if (mapping == nullptr) return ERROR_INVALID_PARAMETER;
|
|
137
|
+
|
|
138
|
+
uint32_t result = ERROR_SUCCESS;
|
|
139
|
+
if (mapping->address != nullptr && !UnmapViewOfFile(mapping->address)) {
|
|
140
|
+
result = LastErrorOrInvalidParameter();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (mapping->handle != 0) {
|
|
144
|
+
HANDLE handle = reinterpret_cast<HANDLE>(mapping->handle);
|
|
145
|
+
if (!CloseHandle(handle) && result == ERROR_SUCCESS) {
|
|
146
|
+
result = LastErrorOrInvalidParameter();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
mapping->address = nullptr;
|
|
151
|
+
mapping->handle = 0;
|
|
152
|
+
mapping->size = 0;
|
|
153
|
+
mapping->base_address_mod64 = 0;
|
|
154
|
+
mapping->reserved = 0;
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
@@ -531,6 +531,7 @@ export const encodePayload = ({ lockSector, payload, sab, payloadConfig, headers
|
|
|
531
531
|
const tryEncodeProcessSharedBufferNumeric = (task, slotIndex, value) => {
|
|
532
532
|
const descriptor = value.descriptor;
|
|
533
533
|
if (descriptor === undefined ||
|
|
534
|
+
descriptor.name !== undefined ||
|
|
534
535
|
!isU32(descriptor.fd) ||
|
|
535
536
|
!isU32(descriptor.size) ||
|
|
536
537
|
!isU32(descriptor.byteLength) ||
|
package/src/runtime/pool.d.ts
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
|
+
import { type ProcessSharedMemoryBacking } from "./process-worker.js";
|
|
1
2
|
import { type Sab } from "../ipc/transport/shared-memory.js";
|
|
2
3
|
import { lock2 } from "../memory/lock.js";
|
|
3
4
|
import type { DebugOptions, DispatcherSettings, WorkerContext, WorkerData, WorkerSettings } from "../types.js";
|
|
4
5
|
import "../worker/loop.js";
|
|
5
6
|
import { type PayloadBufferOptions } from "../memory/payload-config.js";
|
|
6
|
-
type ProcessSharedMemoryNativeMapping = {
|
|
7
|
-
sab: SharedArrayBuffer;
|
|
8
|
-
fd: number;
|
|
9
|
-
size: number;
|
|
10
|
-
baseAddressMod64?: number;
|
|
11
|
-
};
|
|
12
|
-
type ProcessSharedMemoryBacking = ProcessSharedMemoryNativeMapping & {
|
|
13
|
-
runtime: "node";
|
|
14
|
-
buffer: SharedArrayBuffer;
|
|
15
|
-
kind: "shared-array-buffer";
|
|
16
|
-
byteLength: number;
|
|
17
|
-
};
|
|
18
7
|
export declare const spawnWorkerContext: ({ list, ids, sab, thread, debug, totalNumberOfThread, source, at, workerOptions, workerExecArgv, permission, host, payload, payloadInitialBytes, payloadMaxBytes, bufferMode, maxPayloadBytes, abortSignalCapacity, usesAbortSignal, }: {
|
|
19
8
|
list: string[];
|
|
20
9
|
ids: number[];
|
|
@@ -40,4 +29,3 @@ export declare const spawnWorkerContext: ({ list, ids, sab, thread, debug, total
|
|
|
40
29
|
processSharedMemoryBackings?: readonly ProcessSharedMemoryBacking[];
|
|
41
30
|
};
|
|
42
31
|
export type CreateContext = WorkerContext;
|
|
43
|
-
export {};
|