-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
repo-panama
As the confinement properties of a memory segment are now orthogonal from lifetime (e.g. a MemorySegment.Scope is not about confinement), we might add a method on MemorySegment to create a confined view of an existing shared segment:
MemorySegment asThreadConfined(Thread);
This creates a segment view that can only be accessed by the specified thread. Rules:
* If the original segment can be accessed by any thread, then we can specify any thread as parameter;
* If the original segment is already confined, we can only re-assert thread-confinement (e.g. we cannot change the confinement thread).
This method would be very useful for divide-and-conquer algorithms:
```
MemorySegment segment = sharedArena.allocate(...);
Thread[] threads = ...
for (int i = 0 ; i < threads.length ; i++) {
process(segment.asSlice(0, CHUNK_SIZE)
.asThreadConfined(threads[i]));
}
```
MemorySegment asThreadConfined(Thread);
This creates a segment view that can only be accessed by the specified thread. Rules:
* If the original segment can be accessed by any thread, then we can specify any thread as parameter;
* If the original segment is already confined, we can only re-assert thread-confinement (e.g. we cannot change the confinement thread).
This method would be very useful for divide-and-conquer algorithms:
```
MemorySegment segment = sharedArena.allocate(...);
Thread[] threads = ...
for (int i = 0 ; i < threads.length ; i++) {
process(segment.asSlice(0, CHUNK_SIZE)
.asThreadConfined(threads[i]));
}
```