-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththread.py
More file actions
95 lines (69 loc) · 2.32 KB
/
thread.py
File metadata and controls
95 lines (69 loc) · 2.32 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#slthread
# A replacement for the thread module for those uninformed souls that use
# "thread" instead of "threading. Also a base unit used by
# stacklesslib.replacements.threading.py
from __future__ import absolute_import
#we want the "real" thread and threading modules to work too, so we must
#import them here before hiding them away
import traceback
import stackless
import stacklesslib.locks
class error(RuntimeError): pass
def _count():
return Thread.thread_count
class Thread(stackless.tasklet):
# Some tests need this
__slots__ = ["__dict__"]
thread_count = 0
def __new__(cls, function, args, kwargs):
# compatibility with old stackless. New stackless does
# the function binding from init.
return stackless.tasklet.__new__(cls, cls.thread_main)
def __init__(self, function, args, kwargs):
super(Thread, self).__init__(self.thread_main)
self(function, args, kwargs)
self.__class__.thread_count += 1
@classmethod
def thread_main(cls, func, args, kwargs):
try:
try:
func(*args, **kwargs)
except SystemExit:
# Unittests raise system exit sometimes. Evil.
raise TaskletExit
except Exception:
traceback.print_exc()
finally:
cls.thread_count -= 1
def start_new_thread(function, args, kwargs={}):
t = Thread(function, args, kwargs)
return id(t)
def interrupt_main():
# Don't know what to do here, just ignore it
pass
def exit():
stackless.getcurrent().kill()
def get_ident():
return id(stackless.getcurrent())
# Provide this as a no-op.
_stack_size = 0
def stack_size(size=None):
global _stack_size
old = _stack_size
if size is not None:
_stack_size = size
return old
def allocate_lock(self=None):
# Need the self because this function is sometimes placed in classes
# and then invoked as a method, by the test suite.
return LockType()
class LockType(stacklesslib.locks.Lock):
"""
Check if the lock is held by someone
"""
def locked(self):
success = self.acquire(False)
if not success:
return True
self.release()
return False