Package elisa :: Package core :: Package utils :: Module threadsafe_list
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.utils.threadsafe_list

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  __maintainer__ = 'Lionel Martin <lionel@fluendo.com>' 
 18  __maintainer2__ = 'Florian Boucault <florian@fluendo.com>' 
 19   
 20  import threading 
 21   
22 -class ThreadsafeList(list):
23 """ 24 Thread-safe subclass of the built-in type list. 25 """ 26
27 - def __init__(self, *args, **kw):
28 self._lock = threading.Lock() 29 list.__init__(self, *args, **kw)
30
31 - def __eq__(self, other):
32 try: 33 self._lock.acquire() 34 r = (self == other) 35 finally: 36 self._lock.release() 37 return r
38
39 - def __len__(self):
40 try: 41 self._lock.acquire() 42 r = list.__len__(self) 43 finally: 44 self._lock.release() 45 return r
46
47 - def __getitem__(self, rank):
48 try: 49 self._lock.acquire() 50 r = list.__getitem__(self, rank) 51 finally: 52 self._lock.release() 53 return r
54
55 - def __setitem__(self, rank, value):
56 try: 57 self._lock.acquire() 58 list.__setitem__(self, rank, value) 59 finally: 60 self._lock.release()
61
62 - def __delitem__(self, rank):
63 try: 64 self._lock.acquire() 65 list.__delitem__(self, rank) 66 finally: 67 self._lock.release()
68
69 - def __iter__(self):
70 # The returned iterator is not thread-safe itself 71 # http://mail.python.org/pipermail/python-list/2005-April/315764.html 72 # has interesting ideas. 73 try: 74 self._lock.acquire() 75 r = list.__iter__(self) 76 finally: 77 self._lock.release() 78 return r
79
80 - def __contains__(self, item):
81 try: 82 self._lock.acquire() 83 r = list.__contains__(self, item) 84 finally: 85 self._lock.release() 86 return r
87
88 - def append(self, element):
89 try: 90 self._lock.acquire() 91 list.append(self, element) 92 finally: 93 self._lock.release()
94
95 - def count(self, element):
96 try: 97 self._lock.acquire() 98 r = list.count(self, element) 99 finally: 100 self._lock.release() 101 return r
102
103 - def index(self, element):
104 try: 105 self._lock.acquire() 106 r = list.index(self, element) 107 finally: 108 self._lock.release() 109 return r
110
111 - def extend(self, elements):
112 try: 113 self._lock.acquire() 114 list.extend(self, elements) 115 finally: 116 self._lock.release()
117
118 - def insert(self, position, element):
119 try: 120 self._lock.acquire() 121 list.insert(self, position, element) 122 finally: 123 self._lock.release()
124
125 - def pop(self, position=-1):
126 try: 127 self._lock.acquire() 128 r = list.pop(self, position) 129 finally: 130 self._lock.release() 131 return r
132
133 - def remove(self, element):
134 try: 135 self._lock.acquire() 136 list.remove(self, element) 137 finally: 138 self._lock.release()
139
140 - def reverse(self):
141 try: 142 self._lock.acquire() 143 list.reverse(self) 144 finally: 145 self._lock.release()
146
147 - def sort(self, key=None):
148 try: 149 self._lock.acquire() 150 list.sort(self,key=key) 151 finally: 152 self._lock.release()
153
154 - def __add__(self, operand):
155 try: 156 self._lock.acquire() 157 r = list.__add__(self, operand) 158 finally: 159 self._lock.release() 160 return r
161
162 - def __iadd__(self, operand):
163 try: 164 self._lock.acquire() 165 r = list.__iadd__(self, operand) 166 finally: 167 self._lock.release() 168 return r
169
170 - def __mul__(self, coefficient):
171 try: 172 self._lock.acquire() 173 r = list.__mul__(self, coefficient) 174 finally: 175 self._lock.release() 176 return r
177
178 - def __imul__(self, coefficient):
179 try: 180 self._lock.acquire() 181 r = list.__imul__(self, coefficient) 182 finally: 183 self._lock.release() 184 return r
185
186 - def __getslice__(self, i, j):
187 try: 188 self._lock.acquire() 189 r = list.__getslice__(self, i, j) 190 finally: 191 self._lock.release() 192 return r
193
194 - def __setslice__(self, i, j, sequence):
195 try: 196 self._lock.acquire() 197 list.__setslice__(self, i, j, sequence) 198 finally: 199 self._lock.release()
200
201 - def __delslice__(self, i, j):
202 try: 203 self._lock.acquire() 204 list.__delslice__(self, i, j) 205 finally: 206 self._lock.release()
207
208 - def __copy__(self):
209 # this method is a bit hard to implement 210 raise NotImplementedError
211
212 - def __deepcopy__(self, memento):
213 # this method is a bit hard to implement 214 raise NotImplementedError
215