forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfinedSwiftMemorySession.java
More file actions
98 lines (79 loc) · 3 KB
/
ConfinedSwiftMemorySession.java
File metadata and controls
98 lines (79 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
package org.swift.swiftkit;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
final class ConfinedSwiftMemorySession implements ClosableSwiftArena {
final static int CLOSED = 0;
final static int ACTIVE = 1;
final Thread owner;
final AtomicInteger state;
final Arena arena;
final ConfinedResourceList resources;
public ConfinedSwiftMemorySession(Thread owner) {
this.owner = owner;
this.state = new AtomicInteger(ACTIVE);
this.resources = new ConfinedResourceList();
this.arena = Arena.ofConfined();
}
public void checkValid() throws RuntimeException {
if (this.owner != null && this.owner != Thread.currentThread()) {
throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!"
.formatted(this.owner, Thread.currentThread()));
} else if (this.state.get() < ACTIVE) {
throw new RuntimeException("SwiftArena is already closed!");
}
}
@Override
public void close() {
checkValid();
// Cleanup all resources
if (this.state.compareAndExchange(ACTIVE, CLOSED) == ACTIVE) {
this.resources.runCleanup();
} // else, was already closed; do nothing
this.arena.close();
}
@Override
public void register(SwiftInstance instance) {
checkValid();
var statusDestroyedFlag = instance.$statusDestroyedFlag();
Runnable markAsDestroyed = () -> statusDestroyedFlag.set(true);
var cleanup = new SwiftInstanceCleanup(
instance.$memorySegment(),
instance.$swiftType(),
markAsDestroyed);
this.resources.add(cleanup);
}
@Override
public MemorySegment allocate(long byteSize, long byteAlignment) {
return arena.allocate(byteSize, byteAlignment);
}
static final class ConfinedResourceList implements SwiftResourceList {
// TODO: Could use intrusive linked list to avoid one indirection here
final List<SwiftInstanceCleanup> resourceCleanups = new LinkedList<>();
void add(SwiftInstanceCleanup cleanup) {
resourceCleanups.add(cleanup);
}
@Override
public void runCleanup() {
for (SwiftInstanceCleanup cleanup : resourceCleanups) {
cleanup.run();
}
resourceCleanups.clear();
}
}
}