def to_handle(cffi_ptr) -> Pointer[NativeFunction]: pointer_int = int(ffi.cast('ssize_t', cffi_ptr)) # TODO we're just making up a memory mapping that this pointer is inside; # we should figure out the actual mapping, and the size for that matter. mapping = MemoryMapping(task, near.MemoryMapping(pointer_int, 0, 1), far.File()) return Pointer(mapping, NullGateway(), NativeFunctionSerializer(), StaticAllocation())
async def malloc( self, size: int, alignment: int) -> t.Tuple[MemoryMapping, AllocationInterface]: self.allocations.append((size, alignment)) return ( MemoryMapping(t.cast(Task, None), near.MemoryMapping(0, size, 4096), far.File()), NullAllocation(size), )
async def setup_shared_memory_robust_futex( parent: Thread, parent_memfd: FileDescriptor, child: Thread, child_memfd: FileDescriptor, ) -> t.Tuple[WrittenPointer[FutexNode], WrittenPointer[FutexNode]]: """Setup a robust futex in `child` which is in shared memory with `parent` Since it's in shared memory, that means parent can do a futex_wait on it. To achieve this shared memory behavior, we need to be passed two memfds which should point to the same memory. We map the memfds in the parent and child, set up the robust futex in the child, translate the resulting pointer to the parent's address space, and return (parent futex pointer, child futex pointer) """ # resize memfd appropriately size = 4096 await parent_memfd.ftruncate(size) file = far.File() # set up parent mapping parent_mapping = await parent_memfd.mmap(size, PROT.READ | PROT.WRITE, MAP.SHARED, file=file) await parent_memfd.close() # set up child mapping child_mapping = await child_memfd.mmap(size, PROT.READ | PROT.WRITE, MAP.SHARED, file=file) await child_memfd.close() # setup the child task's futex list child_futex_node = await set_singleton_robust_futex( child.task, child.ram, memory.Arena(child_mapping)) # translate the futex from the child's address space to the parent's address space parent_futex_node = child_futex_node._with_mapping(parent_mapping) return parent_futex_node, child_futex_node