10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Module which supports allocation of ctypes objects from shared memory 30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# multiprocessing/sharedctypes.py 50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Copyright (c) 2006-2008, R Oudkerk 70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# All rights reserved. 80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Redistribution and use in source and binary forms, with or without 100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# modification, are permitted provided that the following conditions 110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# are met: 120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 1. Redistributions of source code must retain the above copyright 140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# notice, this list of conditions and the following disclaimer. 150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 2. Redistributions in binary form must reproduce the above copyright 160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# notice, this list of conditions and the following disclaimer in the 170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# documentation and/or other materials provided with the distribution. 180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 3. Neither the name of author nor the names of any contributors may be 190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# used to endorse or promote products derived from this software 200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# without specific prior written permission. 210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND 230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# SUCH DAMAGE. 330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport sys 360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport ctypes 370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport weakref 380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom multiprocessing import heap, RLock 400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom multiprocessing.forking import assert_spawning, ForkingPickler 410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi__all__ = ['RawValue', 'RawArray', 'Value', 'Array', 'copy', 'synchronized'] 430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yitypecode_to_type = { 490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'c': ctypes.c_char, 'u': ctypes.c_wchar, 500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'b': ctypes.c_byte, 'B': ctypes.c_ubyte, 510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'h': ctypes.c_short, 'H': ctypes.c_ushort, 520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'i': ctypes.c_int, 'I': ctypes.c_uint, 530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'l': ctypes.c_long, 'L': ctypes.c_ulong, 540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'f': ctypes.c_float, 'd': ctypes.c_double 550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi } 560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef _new_value(type_): 620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi size = ctypes.sizeof(type_) 630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi wrapper = heap.BufferWrapper(size) 640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return rebuild_ctype(type_, wrapper, None) 650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef RawValue(typecode_or_type, *args): 670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi Returns a ctypes object allocated from shared memory 690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) 710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi obj = _new_value(type_) 720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) 730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi obj.__init__(*args) 740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return obj 750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef RawArray(typecode_or_type, size_or_initializer): 770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi Returns a ctypes array allocated from shared memory 790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) 810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if isinstance(size_or_initializer, (int, long)): 820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi type_ = type_ * size_or_initializer 830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi obj = _new_value(type_) 840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) 850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return obj 860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi type_ = type_ * len(size_or_initializer) 880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi result = _new_value(type_) 890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi result.__init__(*size_or_initializer) 900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return result 910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef Value(typecode_or_type, *args, **kwds): 930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi Return a synchronization wrapper for a Value 950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi lock = kwds.pop('lock', None) 970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if kwds: 980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys()) 990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi obj = RawValue(typecode_or_type, *args) 1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if lock is False: 1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return obj 1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if lock in (True, None): 1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi lock = RLock() 1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if not hasattr(lock, 'acquire'): 1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raise AttributeError("'%r' has no method 'acquire'" % lock) 1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return synchronized(obj, lock) 1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef Array(typecode_or_type, size_or_initializer, **kwds): 1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi Return a synchronization wrapper for a RawArray 1110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ''' 1120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi lock = kwds.pop('lock', None) 1130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if kwds: 1140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys()) 1150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi obj = RawArray(typecode_or_type, size_or_initializer) 1160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if lock is False: 1170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return obj 1180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if lock in (True, None): 1190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi lock = RLock() 1200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if not hasattr(lock, 'acquire'): 1210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raise AttributeError("'%r' has no method 'acquire'" % lock) 1220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return synchronized(obj, lock) 1230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef copy(obj): 1250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi new_obj = _new_value(type(obj)) 1260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ctypes.pointer(new_obj)[0] = obj 1270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return new_obj 1280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef synchronized(obj, lock=None): 1300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert not isinstance(obj, SynchronizedBase), 'object already synchronized' 1310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if isinstance(obj, ctypes._SimpleCData): 1330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return Synchronized(obj, lock) 1340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi elif isinstance(obj, ctypes.Array): 1350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if obj._type_ is ctypes.c_char: 1360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return SynchronizedString(obj, lock) 1370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return SynchronizedArray(obj, lock) 1380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 1390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi cls = type(obj) 1400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi scls = class_cache[cls] 1420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except KeyError: 1430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi names = [field[0] for field in cls._fields_] 1440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d = dict((name, make_property(name)) for name in names) 1450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi classname = 'Synchronized' + cls.__name__ 1460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi scls = class_cache[cls] = type(classname, (SynchronizedBase,), d) 1470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return scls(obj, lock) 1480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 1500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Functions for pickling/unpickling 1510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 1520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef reduce_ctype(obj): 1540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert_spawning(obj) 1550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if isinstance(obj, ctypes.Array): 1560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return rebuild_ctype, (obj._type_, obj._wrapper, obj._length_) 1570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 1580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return rebuild_ctype, (type(obj), obj._wrapper, None) 1590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef rebuild_ctype(type_, wrapper, length): 1610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if length is not None: 1620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi type_ = type_ * length 1630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ForkingPickler.register(type_, reduce_ctype) 1640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi obj = type_.from_address(wrapper.get_address()) 1650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi obj._wrapper = wrapper 1660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return obj 1670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 1690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Function to create properties 1700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 1710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef make_property(name): 1730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return prop_cache[name] 1750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except KeyError: 1760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d = {} 1770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec template % ((name,)*7) in d 1780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi prop_cache[name] = d[name] 1790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return d[name] 1800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yitemplate = ''' 1820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef get%s(self): 1830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.acquire() 1840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return self._obj.%s 1860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 1870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.release() 1880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef set%s(self, value): 1890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.acquire() 1900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._obj.%s = value 1920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 1930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.release() 1940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi%s = property(get%s, set%s) 1950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi''' 1960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiprop_cache = {} 1980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass_cache = weakref.WeakKeyDictionary() 1990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 2010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Synchronized wrappers 2020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# 2030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass SynchronizedBase(object): 2050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __init__(self, obj, lock=None): 2070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._obj = obj 2080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._lock = lock or RLock() 2090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.acquire = self._lock.acquire 2100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.release = self._lock.release 2110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __reduce__(self): 2130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert_spawning(self) 2140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return synchronized, (self._obj, self._lock) 2150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def get_obj(self): 2170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return self._obj 2180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def get_lock(self): 2200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return self._lock 2210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __repr__(self): 2230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return '<%s wrapper for %s>' % (type(self).__name__, self._obj) 2240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass Synchronized(SynchronizedBase): 2270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi value = make_property('value') 2280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass SynchronizedArray(SynchronizedBase): 2310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __len__(self): 2330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return len(self._obj) 2340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __getitem__(self, i): 2360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.acquire() 2370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 2380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return self._obj[i] 2390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 2400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.release() 2410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __setitem__(self, i, value): 2430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.acquire() 2440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 2450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._obj[i] = value 2460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 2470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.release() 2480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __getslice__(self, start, stop): 2500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.acquire() 2510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 2520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return self._obj[start:stop] 2530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 2540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.release() 2550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __setslice__(self, start, stop, values): 2570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.acquire() 2580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 2590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._obj[start:stop] = values 2600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 2610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.release() 2620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass SynchronizedString(SynchronizedArray): 2650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi value = make_property('value') 2660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raw = make_property('raw') 267