2 # -*- coding: utf-8 -*-
3 # vim: expandtab:shiftwidth=4:fileencoding=utf-8 :
5 # Copyright ® 2008 Fulvio Satta
7 # If you want contact me, send an email to Yota_VGA@users.sf.net
9 # This file is part of jcd
11 # jcd is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # jcd is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #TODO: Test, test, test
28 ##########################
29 ##### IMPORT SECTION #####
30 ##########################
32 from threading
import RLock
as _RLock
33 from threading
import Condition
as _Condition
34 from threading
import currentThread
as _CurrentThread
36 from inspect
import isfunction
as _isfunction
38 ################################
39 ##### BASE CLASSES SECTION #####
40 ################################
42 #Class usefull for sincronizzation purposes
45 self
.__mutex
= _RLock()
48 def lock(self
, blocking
= 1):
49 return self
.__mutex
.acquire(blocking
)
53 return self
.__mutex
.release()
55 #Class usefull for sincronizzation purposes with rw locks
58 self
.__status
= _Condition()
59 self
.__lock
= _RLock()
61 self
.__writers
= False
62 self
.__usedThreads
= {}
64 def __addToThread(self
):
65 thread
= _CurrentThread()
66 original
= self
.__usedThreads
.get(thread
, 0)
67 self
.__usedThreads
[thread
] = original
+ 1
71 def readLock(self
, wait
= None):
74 if self
.__addToThread
():
79 self
.__status
.wait(wait
)
83 if not self
.__readers
:
84 r
= self
.__lock
.acquire()
89 self
.__status
.release()
92 def writeLock(self
, blocking
= 1):
93 self
.__status
.acquire()
97 return self
.__lock
.acquire(blocking
)
99 self
.__status
.release()
101 #Unlocking the rwlock
103 self
.__status
.acquire()
105 thread
= _CurrentThread()
106 self
.__usedThreads
[thread
] -= 1
107 if self
.__usedThreads
[thread
]:
108 del self
.__usedThreads
[thread
]
112 if not self
.__readers
:
113 self
.__lock
.release()
115 self
.__status
.release()
117 ##############################
118 ##### DECORATORS SECTION #####
119 ##############################
121 #Decorator for function autolock in the classes inerithed from Syncronized
123 from types
import FunctionType
125 def member(self
, *args
, **kwargs
):
128 return param(self
, *args
, **kwargs
)
133 def decorator(*args
, **kwargs
):
134 if hasattr(param
, 'lock'):
139 return f(*args
, **kwargs
)
141 if hasattr(param
, 'unlock'):
147 if type(param
) == FunctionType
:
151 #Decorator for function autolock in the classes inerithed from RWSyncronized
152 def RWAutoLock(param
, write
= False):
153 from types
import FunctionType
155 def member(self
, *args
, **kwargs
):
161 return param(self
, *args
, **kwargs
)
166 def decorator(*args
, **kwargs
):
172 return f(*args
, **kwargs
)
177 if type(param
) == FunctionType
:
181 ########################
182 ##### TEST SECTION #####
183 ########################
185 if __name__
== '__main__':
186 class Try1(Syncronized
):