| 1 | 'POSIX shared memory module' |
| 2 | |
| 3 | from _posixshmem_cffi import lib, ffi |
| 4 | |
| 5 | import errno |
| 6 | import os |
| 7 | |
| 8 | def shm_open(path, flags, mode=0o777): |
| 9 | 'Open a shared memory object. Returns a file descriptor (integer).' |
| 10 | path_utf8 = path.encode("utf-8") |
| 11 | if b'\x00' in path_utf8: |
| 12 | raise ValueError('embedded null character') |
| 13 | while 1: |
| 14 | fd = lib.shm_open(path_utf8, flags, mode) |
| 15 | if fd < 0: |
| 16 | e = ffi.errno |
| 17 | if e != errno.EINTR: |
| 18 | raise OSError(e, os.strerror(e)) |
| 19 | else: |
| 20 | return fd |
| 21 | |
| 22 | def shm_unlink(path): |
| 23 | '''Remove a shared memory object (similar to unlink()). |
| 24 | |
| 25 | Remove a shared memory object name, and, once all processes have unmapped |
| 26 | the object, de-allocates and destroys the contents of the associated memory |
| 27 | region. |
| 28 | ''' |
| 29 | |
| 30 | path_utf8 = path.encode("utf-8") |
| 31 | if b'\x00' in path_utf8: |
| 32 | raise ValueError('embedded null character') |
| 33 | while 1: |
| 34 | rv = lib.shm_unlink(path_utf8) |
| 35 | if rv < 0: |
| 36 | e = ffi.errno |
| 37 | if e != errno.EINTR: |
| 38 | raise OSError(e, os.strerror(e)) |
| 39 | else: |
| 40 | return |