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

Commit 53c43ee

Browse files
authored
Port from PR 91579 and PR 20684 in Python (#7)
1 parent 4d45943 commit 53c43ee

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

.github/workflows/cd.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
shell: bash
2929
run: |
3030
source ./.github/workflows/install-conda.sh
31+
if [[ `uname` == "Darwin" ]]; then
32+
brew reinstall gcc@10
33+
fi
3134
3235
- name: Deploy packages
3336
if: startsWith(github.ref, 'refs/tags/') && matrix.no-deploy != '1'

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ jobs:
2323
shell: bash
2424
run: |
2525
source ./.github/workflows/install-conda.sh
26+
if [[ `uname` == "Darwin" ]]; then
27+
brew reinstall gcc@10
28+
fi
2629
2730
- name: Build extensions
2831
shell: bash

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
],
2929
libraries=["rt"] if sys.platform == 'linux' else [],
3030
sources=["shared_memory/posixshmem.c"],
31+
extra_compile_args=["-Wno-implicit-function-declaration"],
3132
)
3233
win_shm_mod = Extension(
3334
"shared_memory._winshmem",

shared_memory/shared_memory.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Provides shared memory for direct access across processes.
2+
23
The API of this package is currently provisional. Refer to the
34
documentation for details.
45
"""
@@ -22,6 +23,7 @@
2223
from . import _posixshmem
2324
_USE_POSIX = True
2425

26+
from . import resource_tracker
2527

2628
_O_CREX = os.O_CREAT | os.O_EXCL
2729

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

116-
from .resource_tracker import register
117-
register(self._name, "shared_memory")
118+
resource_tracker.register(self._name, "shared_memory")
118119

119120
else:
120121

@@ -170,7 +171,10 @@ def __init__(self, name=None, create=False, size=0):
170171
)
171172
finally:
172173
_winapi.CloseHandle(h_map)
173-
size = _winshmem.VirtualQuerySize(p_buf)
174+
try:
175+
size = _winshmem.VirtualQuerySize(p_buf)
176+
finally:
177+
_winshmem.UnmapViewOfFile(p_buf)
174178
self._mmap = mmap.mmap(-1, size, tagname=name)
175179

176180
self._size = size
@@ -233,9 +237,8 @@ def unlink(self):
233237
called once (and only once) across all processes which have access
234238
to the shared memory block."""
235239
if _USE_POSIX and self._name:
236-
from .resource_tracker import unregister
237240
_posixshmem.shm_unlink(self._name)
238-
unregister(self._name, "shared_memory")
241+
resource_tracker.unregister(self._name, "shared_memory")
239242

240243

241244
_encoding = "utf8"

shared_memory/winshmem.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,37 @@ _winshmem_VirtualQuerySize_impl(PyObject *module, LPCVOID address)
241241
return region_size;
242242
}
243243

244+
/*[clinic input]
245+
_winshmem.UnmapViewOfFile
246+
address: LPCVOID
247+
/
248+
[clinic start generated code]*/
249+
250+
static PyObject *
251+
_winshmem_UnmapViewOfFile_impl(PyObject *module, LPCVOID address)
252+
/*[clinic end generated code: output=0c5e521bc21e44f6 input=094db9950e24bbbe]*/
253+
{
254+
BOOL success;
255+
256+
Py_BEGIN_ALLOW_THREADS
257+
success = UnmapViewOfFile(address);
258+
Py_END_ALLOW_THREADS
259+
260+
if (!success) {
261+
return PyErr_SetFromWindowsErr(0);
262+
}
263+
264+
Py_RETURN_NONE;
265+
}
266+
244267
#include "clinic/winshmem.c.h"
245268

246269
static PyMethodDef module_methods[ ] = {
247270
_WINSHMEM_CREATEFILEMAPPING_METHODDEF
248271
_WINSHMEM_MAPVIEWOFFILE_METHODDEF
249272
_WINSHMEM_OPENFILEMAPPING_METHODDEF
250273
_WINSHMEM_VIRTUALQUERYSIZE_METHODDEF
274+
_WINSHMEM_UNMAPVIEWOFFILE_METHODDEF
251275
{NULL} /* Sentinel */
252276
};
253277

0 commit comments

Comments
 (0)