/usr/lib/python2.7/dist-packages/logbook/concurrency.py is in python-logbook 0.12.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | has_gevent = True
use_gevent = False
try:
import gevent
def enable_gevent():
global use_gevent
use_gevent = True
def _disable_gevent(): # for testing
global use_gevent
use_gevent = False
def is_gevent_enabled():
global use_gevent
return use_gevent
except ImportError:
has_gevent = False
def enable_gevent():
pass
def _disable_gevent():
pass
def is_gevent_enabled():
return False
if has_gevent:
from gevent._threading import (Lock as ThreadLock,
RLock as ThreadRLock,
get_ident as thread_get_ident,
local as thread_local)
from gevent.thread import get_ident as greenlet_get_ident
from gevent.local import local as greenlet_local
from gevent.lock import BoundedSemaphore
from gevent.threading import __threading__
def thread_get_name():
return __threading__.currentThread().getName()
class GreenletRLock(object):
def __init__(self):
self._thread_local = thread_local()
self._owner = None
self._wait_queue = []
self._count = 0
def __repr__(self):
owner = self._owner
return "<%s owner=%r count=%d>" % (self.__class__.__name__, owner,
self._count)
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 = 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 release(self):
tid_gid = (thread_get_ident(), greenlet_get_ident())
if tid_gid != self._owner:
raise RuntimeError("cannot release un-acquired lock")
self._count = 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()
__enter__ = acquire
def __exit__(self, t, v, tb):
self.release()
def _get_greenlet_lock(self):
if not hasattr(self._thread_local, 'greenlet_lock'):
greenlet_lock = self._thread_local.greenlet_lock = BoundedSemaphore(1)
else:
greenlet_lock = self._thread_local.greenlet_lock
return greenlet_lock
def _is_owned(self):
return self._owner == (thread_get_ident(), greenlet_get_ident())
else:
from threading import (
Lock as ThreadLock, RLock as ThreadRLock, currentThread)
try:
from thread import (
get_ident as thread_get_ident, _local as thread_local)
except ImportError:
from _thread import (
get_ident as thread_get_ident, _local as thread_local)
def thread_get_name():
return currentThread().getName()
greenlet_get_ident = thread_get_ident
greenlet_local = thread_local
class GreenletRLock(object):
def acquire(self):
pass
def release(self):
pass
def __enter__(self):
pass
def __exit__(self, t, v, tb):
pass
def new_fine_grained_lock():
global use_gevent
if use_gevent:
return GreenletRLock()
else:
return ThreadRLock()
|