github.com/google/grumpy@v0.0.0-20171122020858-3ec87959189c/lib/thread.py (about)

     1  from '__go__/grumpy' import NewTryableMutex, StartThread, ThreadCount
     2  
     3  
     4  class error(Exception):
     5    pass
     6  
     7  
     8  def get_ident():
     9    f = __frame__()
    10    while f.f_back:
    11      f = f.f_back
    12    return id(f)
    13  
    14  
    15  class LockType(object):
    16    def __init__(self):
    17      self._mutex = NewTryableMutex()
    18  
    19    def acquire(self, waitflag=1):
    20      if waitflag:
    21        self._mutex.Lock()
    22        return True
    23      return self._mutex.TryLock()
    24  
    25    def release(self):
    26      self._mutex.Unlock()
    27  
    28    def __enter__(self):
    29      self.acquire()
    30  
    31    def __exit__(self, *args):
    32      self.release()
    33  
    34  
    35  def allocate_lock():
    36    """Dummy implementation of thread.allocate_lock()."""
    37    return LockType()
    38  
    39  
    40  def start_new_thread(func, args, kwargs=None):
    41    if kwargs is None:
    42      kwargs = {}
    43    l = allocate_lock()
    44    ident = []
    45    def thread_func():
    46      ident.append(get_ident())
    47      l.release()
    48      func(*args, **kwargs)
    49    l.acquire()
    50    StartThread(thread_func)
    51    l.acquire()
    52    return ident[0]
    53  
    54  
    55  def stack_size(n=0):
    56    if n:
    57      raise error('grumpy does not support setting stack size')
    58    return 0
    59  
    60  
    61  def _count():
    62    return ThreadCount