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, six
4 from eventlet import greenthread
5 from eventlet.semaphore import Semaphore as LockType
8 __patched__ = ['get_ident', 'start_new_thread', 'start_new', 'allocate_lock',
9 'allocate', 'exit', 'interrupt_main', 'stack_size', '_local',
12 error = __thread.error
18 # TODO this is a dummy code, reimplementing this may be needed:
19 # https://hg.python.org/cpython/file/b5e9bc4352e1/Modules/_threadmodule.c#l1203
20 return allocate_lock()
22 TIMEOUT_MAX = __thread.TIMEOUT_MAX
29 def get_ident(gr=None):
31 return id(greenlet.getcurrent())
36 def __thread_body(func, args, kwargs):
45 def start_new_thread(function, args=(), kwargs=None):
47 g = greenthread.spawn_n(__thread_body, function, args, kwargs)
51 start_new = start_new_thread
54 def allocate_lock(*a):
58 allocate = allocate_lock
62 raise greenlet.GreenletExit
65 exit_thread = __thread.exit_thread
69 curr = greenlet.getcurrent()
70 if curr.parent and not curr.parent.dead:
71 curr.parent.throw(KeyboardInterrupt())
73 raise KeyboardInterrupt()
76 if hasattr(__thread, 'stack_size'):
77 __original_stack_size__ = __thread.stack_size
79 def stack_size(size=None):
81 return __original_stack_size__()
82 if size > __original_stack_size__():
83 return __original_stack_size__(size)
86 # not going to decrease stack_size, because otherwise other greenlets in
87 # this thread will suffer
89 from eventlet.corolocal import local as _local