Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Port from PR 91579 and PR 20684 in Python #7

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
shell: bash
run: |
source ./.github/workflows/install-conda.sh
if [[ `uname` == "Darwin" ]]; then
brew reinstall gcc@10
fi

- name: Deploy packages
if: startsWith(github.ref, 'refs/tags/') && matrix.no-deploy != '1'
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
shell: bash
run: |
source ./.github/workflows/install-conda.sh
if [[ `uname` == "Darwin" ]]; then
brew reinstall gcc@10
fi
- name: Build extensions
shell: bash
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
],
libraries=["rt"] if sys.platform == 'linux' else [],
sources=["shared_memory/posixshmem.c"],
extra_compile_args=["-Wno-implicit-function-declaration"],
)
win_shm_mod = Extension(
"shared_memory._winshmem",
Expand Down
13 changes: 8 additions & 5 deletions shared_memory/shared_memory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provides shared memory for direct access across processes.

The API of this package is currently provisional. Refer to the
documentation for details.
"""
Expand All @@ -22,6 +23,7 @@
from . import _posixshmem
_USE_POSIX = True

from . import resource_tracker

_O_CREX = os.O_CREAT | os.O_EXCL

Expand Down Expand Up @@ -113,8 +115,7 @@ def __init__(self, name=None, create=False, size=0):
self.unlink()
raise

from .resource_tracker import register
register(self._name, "shared_memory")
resource_tracker.register(self._name, "shared_memory")

else:

Expand Down Expand Up @@ -170,7 +171,10 @@ def __init__(self, name=None, create=False, size=0):
)
finally:
_winapi.CloseHandle(h_map)
size = _winshmem.VirtualQuerySize(p_buf)
try:
size = _winshmem.VirtualQuerySize(p_buf)
finally:
_winshmem.UnmapViewOfFile(p_buf)
self._mmap = mmap.mmap(-1, size, tagname=name)

self._size = size
Expand Down Expand Up @@ -233,9 +237,8 @@ def unlink(self):
called once (and only once) across all processes which have access
to the shared memory block."""
if _USE_POSIX and self._name:
from .resource_tracker import unregister
_posixshmem.shm_unlink(self._name)
unregister(self._name, "shared_memory")
resource_tracker.unregister(self._name, "shared_memory")


_encoding = "utf8"
Expand Down
24 changes: 24 additions & 0 deletions shared_memory/winshmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,37 @@ _winshmem_VirtualQuerySize_impl(PyObject *module, LPCVOID address)
return region_size;
}

/*[clinic input]
_winshmem.UnmapViewOfFile
address: LPCVOID
/
[clinic start generated code]*/

static PyObject *
_winshmem_UnmapViewOfFile_impl(PyObject *module, LPCVOID address)
/*[clinic end generated code: output=0c5e521bc21e44f6 input=094db9950e24bbbe]*/
{
BOOL success;

Py_BEGIN_ALLOW_THREADS
success = UnmapViewOfFile(address);
Py_END_ALLOW_THREADS

if (!success) {
return PyErr_SetFromWindowsErr(0);
}

Py_RETURN_NONE;
}

#include "clinic/winshmem.c.h"

static PyMethodDef module_methods[ ] = {
_WINSHMEM_CREATEFILEMAPPING_METHODDEF
_WINSHMEM_MAPVIEWOFFILE_METHODDEF
_WINSHMEM_OPENFILEMAPPING_METHODDEF
_WINSHMEM_VIRTUALQUERYSIZE_METHODDEF
_WINSHMEM_UNMAPVIEWOFFILE_METHODDEF
{NULL} /* Sentinel */
};

Expand Down