def write(self, value): thread_id = thread_get_ident() self._ensure_thread_buffer(thread_id) if PY2 and isinstance(value, unicode): # pylint: disable=undefined-variable value = value.encode() return self._buffers[thread_id].write( value.decode() if is_bytes(value) else value )
def release(self): tid_gid = (thread_get_ident(), greenlet_get_ident()) if tid_gid != self._owner: raise RuntimeError("cannot release un-acquired lock") self._count -= 1 if not self._count: self._owner = None gid = self._wait_queue.pop(0) assert gid == tid_gid[1] self._thread_local.greenlet_lock.release()
def release(self): tid_gid = (thread_get_ident(), greenlet_get_ident()) if tid_gid != self._owner: raise RuntimeError("cannot release un-acquired lock") self._count -= 1 if not self._count: self._owner = None gid = self._wait_queue.pop(0) assert gid == tid_gid[1] self._thread_local.greenlet_lock.release()
def acquire(self, blocking=1): tid = thread_get_ident() gid = greenlet_get_ident() tid_gid = (tid, gid) # We trust the GIL here so we can do this comparison w/o locking. if tid_gid == self._owner: self._count += 1 return True greenlet_lock = self._get_greenlet_lock() self._wait_queue.append(gid) # this is a safety in case an exception is raised somewhere # and we must make sure we're not in the queue # otherwise it'll get stuck forever. remove_from_queue_on_return = True try: while True: if not greenlet_lock.acquire(blocking): return False # non-blocking and failed to acquire lock if self._wait_queue[0] == gid: # Hurray, we can have the lock. self._owner = tid_gid self._count = 1 # don't remove us from the queue remove_from_queue_on_return = False return True else: # we already hold the greenlet lock so obviously # the owner is not in our thread. greenlet_lock.release() if blocking: # 500 us -> initial delay of 1 ms gevent.sleep(0.0005) else: return False finally: if remove_from_queue_on_return: self._wait_queue.remove(gid)
def acquire(self, blocking=1): tid = thread_get_ident() gid = greenlet_get_ident() tid_gid = (tid, gid) # We trust the GIL here so we can do this comparison w/o locking. if tid_gid == self._owner: self._count += 1 return True greenlet_lock = self._get_greenlet_lock() self._wait_queue.append(gid) # this is a safety in case an exception is raised somewhere # and we must make sure we're not in the queue # otherwise it'll get stuck forever. remove_from_queue_on_return = True try: while True: if not greenlet_lock.acquire(blocking): return False # non-blocking and failed to acquire lock if self._wait_queue[0] == gid: # Hurray, we can have the lock. self._owner = tid_gid self._count = 1 # don't remove us from the queue remove_from_queue_on_return = False return True else: # we already hold the greenlet lock so obviously # the owner is not in our thread. greenlet_lock.release() if blocking: # 500 us -> initial delay of 1 ms gevent.sleep(0.0005) else: return False finally: if remove_from_queue_on_return: self._wait_queue.remove(gid)
def _is_owned(self): return self._owner == (thread_get_ident(), greenlet_get_ident())
def write(self, value): thread_id = thread_get_ident() self._ensure_thread_buffer(thread_id) return self._buffers[thread_id].write( value.decode() if is_bytes(value) else value)
def __getattr__(self, name): thread_id = thread_get_ident() self._ensure_thread_buffer(thread_id) return getattr(self._buffers[thread_id], name)
def __init__(self, parent_stream): self._buffers = {thread_get_ident(): parent_stream}
def _is_owned(self): return self._owner == (thread_get_ident(), greenlet_get_ident())