forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
61 lines (44 loc) · 1.75 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Written by Arno Bakker
# see LICENSE.txt for license information
'''
The Core package contains the core functionalities of the Tribler project
'''
from threading import RLock
import logging
# Written by BitTornado authors and Arno Bakker
# see LICENSE.txt for license information
logger = logging.getLogger(__name__)
def warnIfDispersyThread(func):
"""
We'd rather not be on the Dispersy thread, but if we are lets continue and
hope for the best. This was introduced after the database thread stuffs
caused deadlocks. We weren't sure we got all of them, so we implemented
warnings instead of errors because they probably wouldn't cause a deadlock,
but if they did we would have the warning somewhere.
Niels dixit.
"""
def invoke_func(*args, **kwargs):
from twisted.python.threadable import isInIOThread
from traceback import print_stack
if isInIOThread():
import inspect
caller = inspect.stack()[1]
callerstr = "%s %s:%s" % (caller[3], caller[1], caller[2])
from time import time
logger.error("%d CANNOT BE ON DISPERSYTHREAD %s %s:%s called by %s", long(time()),
func.__name__, func.func_code.co_filename, func.func_code.co_firstlineno, callerstr)
print_stack()
return func(*args, **kwargs)
invoke_func.__name__ = func.__name__
return invoke_func
class NoDispersyRLock():
def __init__(self):
self.lock = RLock()
self.__enter__ = self.lock.__enter__
self.__exit__ = self.lock.__exit__
@warnIfDispersyThread
def acquire(self, blocking=1):
return self.lock.acquire(blocking)
@warnIfDispersyThread
def release(self):
return self.lock.release()