mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-23 19:19:32 +01:00
Added a Synchronized metaclass.
This commit is contained in:
parent
2394005cdd
commit
4b0600a91a
@ -28,6 +28,7 @@
|
|||||||
###
|
###
|
||||||
|
|
||||||
import types
|
import types
|
||||||
|
import threading
|
||||||
|
|
||||||
def changeFunctionName(f, name, doc=None):
|
def changeFunctionName(f, name, doc=None):
|
||||||
if doc is None:
|
if doc is None:
|
||||||
@ -61,4 +62,32 @@ class Acquire(object):
|
|||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
return getattr(self.__parent, attr)
|
return getattr(self.__parent, attr)
|
||||||
|
|
||||||
|
|
||||||
|
class Synchronized(type):
|
||||||
|
def __new__(cls, name, bases, dict):
|
||||||
|
if '__synchronized__' in dict:
|
||||||
|
def synchronized(f):
|
||||||
|
def g(self, *args, **kwargs):
|
||||||
|
self._Synchronized_rlock.acquire()
|
||||||
|
try:
|
||||||
|
f(self, *args, **kwargs)
|
||||||
|
finally:
|
||||||
|
self._Synchronized_rlock.release()
|
||||||
|
return changeFunctionName(g, f.func_name, f.__doc__)
|
||||||
|
for attr in dict['__synchronized__']:
|
||||||
|
dict[attr] = synchronized(dict[attr])
|
||||||
|
original__init__ = dict.get('__init__')
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
if not hasattr(self, '_Synchronized_rlock'):
|
||||||
|
self._Synchronized_rlock = threading.RLock()
|
||||||
|
if original__init__:
|
||||||
|
original__init__(self, *args, **kwargs)
|
||||||
|
else:
|
||||||
|
# newclass is defined below.
|
||||||
|
super(newclass, self).__init__(*args, **kwargs)
|
||||||
|
dict['__init__'] = __init__
|
||||||
|
newclass = super(Synchronized, cls).__new__(cls, name, bases, dict)
|
||||||
|
return newclass
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
Reference in New Issue
Block a user