Adjust the package revision; no actual code changes
[packages/trusty/python-eventlet.git] / eventlet / eventlet / green / thread.py
1 """Implements the standard thread module, using greenthreads."""
2 from eventlet.support.six.moves import _thread as __thread
3 from eventlet.support import greenlets as greenlet
4 from eventlet import greenthread
5 from eventlet.semaphore import Semaphore as LockType
6
7
8 __patched__ = ['get_ident', 'start_new_thread', 'start_new', 'allocate_lock',
9                'allocate', 'exit', 'interrupt_main', 'stack_size', '_local',
10                'LockType', '_count']
11
12 error = __thread.error
13 __threadcount = 0
14
15
16 def _count():
17     return __threadcount
18
19
20 def get_ident(gr=None):
21     if gr is None:
22         return id(greenlet.getcurrent())
23     else:
24         return id(gr)
25
26
27 def __thread_body(func, args, kwargs):
28     global __threadcount
29     __threadcount += 1
30     try:
31         func(*args, **kwargs)
32     finally:
33         __threadcount -= 1
34
35
36 def start_new_thread(function, args=(), kwargs=None):
37     kwargs = kwargs or {}
38     g = greenthread.spawn_n(__thread_body, function, args, kwargs)
39     return get_ident(g)
40
41
42 start_new = start_new_thread
43
44
45 def allocate_lock(*a):
46     return LockType(1)
47
48
49 allocate = allocate_lock
50
51
52 def exit():
53     raise greenlet.GreenletExit
54
55
56 exit_thread = __thread.exit_thread
57
58
59 def interrupt_main():
60     curr = greenlet.getcurrent()
61     if curr.parent and not curr.parent.dead:
62         curr.parent.throw(KeyboardInterrupt())
63     else:
64         raise KeyboardInterrupt()
65
66
67 if hasattr(__thread, 'stack_size'):
68     __original_stack_size__ = __thread.stack_size
69
70     def stack_size(size=None):
71         if size is None:
72             return __original_stack_size__()
73         if size > __original_stack_size__():
74             return __original_stack_size__(size)
75         else:
76             pass
77             # not going to decrease stack_size, because otherwise other greenlets in this thread will suffer
78
79 from eventlet.corolocal import local as _local