| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # Module which supports allocation of ctypes objects from shared memory | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # multiprocessing/sharedctypes.py | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2010-12-14 01:41:07 +00:00
										 |  |  | # Copyright (c) 2006-2008, R Oudkerk | 
					
						
							| 
									
										
										
										
											2012-04-30 12:13:55 +01:00
										 |  |  | # Licensed to PSF under a Contributor Agreement. | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ctypes | 
					
						
							|  |  |  | import weakref | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  | from . import heap | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  | from . import get_context | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 18:03:10 -05:00
										 |  |  | from .context import reduction, assert_spawning | 
					
						
							|  |  |  | _ForkingPickler = reduction.ForkingPickler | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | __all__ = ['RawValue', 'RawArray', 'Value', 'Array', 'copy', 'synchronized'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typecode_to_type = { | 
					
						
							| 
									
										
										
										
											2017-07-21 11:35:33 +01:00
										 |  |  |     'c': ctypes.c_char,     'u': ctypes.c_wchar, | 
					
						
							|  |  |  |     'b': ctypes.c_byte,     'B': ctypes.c_ubyte, | 
					
						
							|  |  |  |     'h': ctypes.c_short,    'H': ctypes.c_ushort, | 
					
						
							|  |  |  |     'i': ctypes.c_int,      'I': ctypes.c_uint, | 
					
						
							|  |  |  |     'l': ctypes.c_long,     'L': ctypes.c_ulong, | 
					
						
							|  |  |  |     'q': ctypes.c_longlong, 'Q': ctypes.c_ulonglong, | 
					
						
							|  |  |  |     'f': ctypes.c_float,    'd': ctypes.c_double | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _new_value(type_): | 
					
						
							|  |  |  |     size = ctypes.sizeof(type_) | 
					
						
							|  |  |  |     wrapper = heap.BufferWrapper(size) | 
					
						
							|  |  |  |     return rebuild_ctype(type_, wrapper, None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def RawValue(typecode_or_type, *args): | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     Returns a ctypes object allocated from shared memory | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) | 
					
						
							|  |  |  |     obj = _new_value(type_) | 
					
						
							|  |  |  |     ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) | 
					
						
							|  |  |  |     obj.__init__(*args) | 
					
						
							|  |  |  |     return obj | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def RawArray(typecode_or_type, size_or_initializer): | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     Returns a ctypes array allocated from shared memory | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) | 
					
						
							|  |  |  |     if isinstance(size_or_initializer, int): | 
					
						
							|  |  |  |         type_ = type_ * size_or_initializer | 
					
						
							| 
									
										
										
										
											2011-03-26 10:19:03 +00:00
										 |  |  |         obj = _new_value(type_) | 
					
						
							|  |  |  |         ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) | 
					
						
							|  |  |  |         return obj | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         type_ = type_ * len(size_or_initializer) | 
					
						
							|  |  |  |         result = _new_value(type_) | 
					
						
							|  |  |  |         result.__init__(*size_or_initializer) | 
					
						
							|  |  |  |         return result | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  | def Value(typecode_or_type, *args, lock=True, ctx=None): | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     '''
 | 
					
						
							|  |  |  |     Return a synchronization wrapper for a Value | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     obj = RawValue(typecode_or_type, *args) | 
					
						
							| 
									
										
										
										
											2009-01-18 03:11:38 +00:00
										 |  |  |     if lock is False: | 
					
						
							|  |  |  |         return obj | 
					
						
							|  |  |  |     if lock in (True, None): | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |         ctx = ctx or get_context() | 
					
						
							|  |  |  |         lock = ctx.RLock() | 
					
						
							| 
									
										
										
										
											2009-01-18 03:11:38 +00:00
										 |  |  |     if not hasattr(lock, 'acquire'): | 
					
						
							| 
									
										
										
										
											2017-11-28 22:54:42 +02:00
										 |  |  |         raise AttributeError("%r has no method 'acquire'" % lock) | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |     return synchronized(obj, lock, ctx=ctx) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  | def Array(typecode_or_type, size_or_initializer, *, lock=True, ctx=None): | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     '''
 | 
					
						
							|  |  |  |     Return a synchronization wrapper for a RawArray | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     obj = RawArray(typecode_or_type, size_or_initializer) | 
					
						
							| 
									
										
										
										
											2009-01-18 03:11:38 +00:00
										 |  |  |     if lock is False: | 
					
						
							|  |  |  |         return obj | 
					
						
							|  |  |  |     if lock in (True, None): | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |         ctx = ctx or get_context() | 
					
						
							|  |  |  |         lock = ctx.RLock() | 
					
						
							| 
									
										
										
										
											2009-01-18 03:11:38 +00:00
										 |  |  |     if not hasattr(lock, 'acquire'): | 
					
						
							| 
									
										
										
										
											2017-11-28 22:54:42 +02:00
										 |  |  |         raise AttributeError("%r has no method 'acquire'" % lock) | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |     return synchronized(obj, lock, ctx=ctx) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def copy(obj): | 
					
						
							|  |  |  |     new_obj = _new_value(type(obj)) | 
					
						
							|  |  |  |     ctypes.pointer(new_obj)[0] = obj | 
					
						
							|  |  |  |     return new_obj | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  | def synchronized(obj, lock=None, ctx=None): | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     assert not isinstance(obj, SynchronizedBase), 'object already synchronized' | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |     ctx = ctx or get_context() | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if isinstance(obj, ctypes._SimpleCData): | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |         return Synchronized(obj, lock, ctx) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     elif isinstance(obj, ctypes.Array): | 
					
						
							|  |  |  |         if obj._type_ is ctypes.c_char: | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |             return SynchronizedString(obj, lock, ctx) | 
					
						
							|  |  |  |         return SynchronizedArray(obj, lock, ctx) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         cls = type(obj) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             scls = class_cache[cls] | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             names = [field[0] for field in cls._fields_] | 
					
						
							| 
									
										
										
										
											2017-05-18 07:35:54 -07:00
										 |  |  |             d = {name: make_property(name) for name in names} | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |             classname = 'Synchronized' + cls.__name__ | 
					
						
							|  |  |  |             scls = class_cache[cls] = type(classname, (SynchronizedBase,), d) | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |         return scls(obj, lock, ctx) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Functions for pickling/unpickling | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def reduce_ctype(obj): | 
					
						
							|  |  |  |     assert_spawning(obj) | 
					
						
							|  |  |  |     if isinstance(obj, ctypes.Array): | 
					
						
							|  |  |  |         return rebuild_ctype, (obj._type_, obj._wrapper, obj._length_) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return rebuild_ctype, (type(obj), obj._wrapper, None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def rebuild_ctype(type_, wrapper, length): | 
					
						
							|  |  |  |     if length is not None: | 
					
						
							|  |  |  |         type_ = type_ * length | 
					
						
							| 
									
										
										
										
											2016-09-09 18:03:10 -05:00
										 |  |  |     _ForkingPickler.register(type_, reduce_ctype) | 
					
						
							| 
									
										
										
										
											2012-05-26 22:09:59 +01:00
										 |  |  |     buf = wrapper.create_memoryview() | 
					
						
							|  |  |  |     obj = type_.from_buffer(buf) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     obj._wrapper = wrapper | 
					
						
							|  |  |  |     return obj | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Function to create properties | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def make_property(name): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         return prop_cache[name] | 
					
						
							|  |  |  |     except KeyError: | 
					
						
							|  |  |  |         d = {} | 
					
						
							|  |  |  |         exec(template % ((name,)*7), d) | 
					
						
							|  |  |  |         prop_cache[name] = d[name] | 
					
						
							|  |  |  |         return d[name] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template = '''
 | 
					
						
							|  |  |  | def get%s(self): | 
					
						
							|  |  |  |     self.acquire() | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         return self._obj.%s | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         self.release() | 
					
						
							|  |  |  | def set%s(self, value): | 
					
						
							|  |  |  |     self.acquire() | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         self._obj.%s = value | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         self.release() | 
					
						
							|  |  |  | %s = property(get%s, set%s) | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | prop_cache = {} | 
					
						
							|  |  |  | class_cache = weakref.WeakKeyDictionary() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Synchronized wrappers | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SynchronizedBase(object): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |     def __init__(self, obj, lock=None, ctx=None): | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |         self._obj = obj | 
					
						
							| 
									
										
										
										
											2013-10-16 16:41:56 +01:00
										 |  |  |         if lock: | 
					
						
							|  |  |  |             self._lock = lock | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             ctx = ctx or get_context(force=True) | 
					
						
							|  |  |  |             self._lock = ctx.RLock() | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |         self.acquire = self._lock.acquire | 
					
						
							|  |  |  |         self.release = self._lock.release | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-25 14:12:12 +01:00
										 |  |  |     def __enter__(self): | 
					
						
							|  |  |  |         return self._lock.__enter__() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __exit__(self, *args): | 
					
						
							|  |  |  |         return self._lock.__exit__(*args) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |     def __reduce__(self): | 
					
						
							|  |  |  |         assert_spawning(self) | 
					
						
							|  |  |  |         return synchronized, (self._obj, self._lock) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_obj(self): | 
					
						
							|  |  |  |         return self._obj | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_lock(self): | 
					
						
							|  |  |  |         return self._lock | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         return '<%s wrapper for %s>' % (type(self).__name__, self._obj) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Synchronized(SynchronizedBase): | 
					
						
							|  |  |  |     value = make_property('value') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SynchronizedArray(SynchronizedBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __len__(self): | 
					
						
							|  |  |  |         return len(self._obj) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __getitem__(self, i): | 
					
						
							| 
									
										
										
										
											2014-05-25 14:12:12 +01:00
										 |  |  |         with self: | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |             return self._obj[i] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __setitem__(self, i, value): | 
					
						
							| 
									
										
										
										
											2014-05-25 14:12:12 +01:00
										 |  |  |         with self: | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |             self._obj[i] = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __getslice__(self, start, stop): | 
					
						
							| 
									
										
										
										
											2014-05-25 14:12:12 +01:00
										 |  |  |         with self: | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |             return self._obj[start:stop] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __setslice__(self, start, stop, values): | 
					
						
							| 
									
										
										
										
											2014-05-25 14:12:12 +01:00
										 |  |  |         with self: | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |             self._obj[start:stop] = values | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SynchronizedString(SynchronizedArray): | 
					
						
							|  |  |  |     value = make_property('value') | 
					
						
							|  |  |  |     raw = make_property('raw') |