From 2e7dbef16ea1e0b5940b5f10f0b9a38833509d22 Mon Sep 17 00:00:00 2001 From: Wenjun Si Date: Fri, 15 Sep 2023 11:02:58 +0800 Subject: [PATCH 1/2] Port from PR 91579 and PR 20684 in Python --- shared_memory/shared_memory.py | 13 ++++++++----- shared_memory/winshmem.c | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/shared_memory/shared_memory.py b/shared_memory/shared_memory.py index f2f78b7..6574570 100644 --- a/shared_memory/shared_memory.py +++ b/shared_memory/shared_memory.py @@ -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. """ @@ -22,6 +23,7 @@ from . import _posixshmem _USE_POSIX = True +from . import resource_tracker _O_CREX = os.O_CREAT | os.O_EXCL @@ -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: @@ -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 @@ -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" diff --git a/shared_memory/winshmem.c b/shared_memory/winshmem.c index 29637be..2b6379f 100644 --- a/shared_memory/winshmem.c +++ b/shared_memory/winshmem.c @@ -241,6 +241,29 @@ _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[ ] = { @@ -248,6 +271,7 @@ static PyMethodDef module_methods[ ] = { _WINSHMEM_MAPVIEWOFFILE_METHODDEF _WINSHMEM_OPENFILEMAPPING_METHODDEF _WINSHMEM_VIRTUALQUERYSIZE_METHODDEF + _WINSHMEM_UNMAPVIEWOFFILE_METHODDEF {NULL} /* Sentinel */ }; From 8dc36aee036f528b7021f694bfd6bd98020b764c Mon Sep 17 00:00:00 2001 From: wjsi Date: Fri, 15 Sep 2023 22:31:50 +0800 Subject: [PATCH 2/2] Reinstall gcc on macos --- .github/workflows/cd.yml | 3 +++ .github/workflows/ci.yml | 3 +++ setup.py | 1 + 3 files changed, 7 insertions(+) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 01c0cb8..03eaa8e 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -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' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6c92d7c..cc08489 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/setup.py b/setup.py index 76a6a1d..c943f67 100644 --- a/setup.py +++ b/setup.py @@ -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",