| 1 |
from __future__ import with_statement |
| 2 |
import py |
| 3 |
import sys, os |
| 4 |
from pypy.module.thread.test.support import GenericTestThread |
| 5 |
from rpython.translator.c.test.test_genc import compile |
| 6 |
from platform import machine |
| 7 |
|
| 8 |
|
| 9 |
class AppTestLock(GenericTestThread): |
| 10 |
|
| 11 |
def test_lock_basic(self): |
| 12 |
import _thread |
| 13 |
lock = _thread.allocate_lock() |
| 14 |
assert type(lock) is _thread.LockType |
| 15 |
assert lock.locked() is False |
| 16 |
raises(RuntimeError, lock.release) |
| 17 |
assert lock.locked() is False |
| 18 |
r = lock.acquire() |
| 19 |
assert r is True |
| 20 |
r = lock.acquire(False) |
| 21 |
assert r is False |
| 22 |
assert lock.locked() is True |
| 23 |
lock.release() |
| 24 |
assert lock.locked() is False |
| 25 |
raises(RuntimeError, lock.release) |
| 26 |
assert lock.locked() is False |
| 27 |
feedback = [] |
| 28 |
lock.acquire() |
| 29 |
def f(): |
| 30 |
self.busywait(0.25) |
| 31 |
feedback.append(42) |
| 32 |
lock.release() |
| 33 |
assert lock.locked() is True |
| 34 |
_thread.start_new_thread(f, ()) |
| 35 |
lock.acquire() |
| 36 |
assert lock.locked() is True |
| 37 |
assert feedback == [42] |
| 38 |
|
| 39 |
def test_lock_in_with(self): |
| 40 |
import _thread |
| 41 |
lock = _thread.allocate_lock() |
| 42 |
feedback = [] |
| 43 |
lock.acquire() |
| 44 |
def f(): |
| 45 |
self.busywait(0.25) |
| 46 |
feedback.append(42) |
| 47 |
lock.release() |
| 48 |
assert lock.locked() is True |
| 49 |
_thread.start_new_thread(f, ()) |
| 50 |
with lock: |
| 51 |
assert lock.locked() is True |
| 52 |
assert feedback == [42] |
| 53 |
assert lock.locked() is False |
| 54 |
|
| 55 |
def test_weakrefable(self): |
| 56 |
import _thread, weakref |
| 57 |
weakref.ref(_thread.allocate_lock()) |
| 58 |
|
| 59 |
def test_timeout(self): |
| 60 |
import _thread |
| 61 |
assert isinstance(_thread.TIMEOUT_MAX, float) |
| 62 |
assert _thread.TIMEOUT_MAX > 1000 |
| 63 |
lock = _thread.allocate_lock() |
| 64 |
assert lock.acquire() is True |
| 65 |
assert lock.acquire(False) is False |
| 66 |
assert lock.acquire(True, timeout=.1) is False |
| 67 |
|
| 68 |
def test_timeout_overflow(self): |
| 69 |
import _thread |
| 70 |
lock = _thread.allocate_lock() |
| 71 |
maxint = 2**63 - 1 |
| 72 |
for i in [-100000, -10000, -1000, -100, -10, -1, 0, |
| 73 |
1, 10, 100, 1000, 10000, 100000]: |
| 74 |
timeout = (maxint + i) * 1e-6 |
| 75 |
try: |
| 76 |
lock.acquire(True, timeout=timeout) |
| 77 |
except OverflowError: |
| 78 |
got_ovf = True |
| 79 |
else: |
| 80 |
got_ovf = False |
| 81 |
lock.release() |
| 82 |
assert (i, got_ovf) == (i, int(timeout * 1e6) > maxint) |
| 83 |
|
| 84 |
@py.test.mark.xfail(machine()=='s390x', reason='may fail under heavy load') |
| 85 |
def test_ping_pong(self): |
| 86 |
# The purpose of this test is that doing a large number of ping-pongs |
| 87 |
# between two threads, using locks, should complete in a reasonable |
| 88 |
# time on a translated pypy with -A. If the GIL logic causes too |
| 89 |
# much sleeping, then it will fail. |
| 90 |
import _thread as thread, time |
| 91 |
COUNT = 100000 if self.runappdirect else 50 |
| 92 |
lock1 = thread.allocate_lock() |
| 93 |
lock2 = thread.allocate_lock() |
| 94 |
def fn(): |
| 95 |
for i in range(COUNT): |
| 96 |
lock1.acquire() |
| 97 |
lock2.release() |
| 98 |
lock2.acquire() |
| 99 |
print("STARTING") |
| 100 |
start = time.time() |
| 101 |
thread.start_new_thread(fn, ()) |
| 102 |
for i in range(COUNT): |
| 103 |
lock2.acquire() |
| 104 |
lock1.release() |
| 105 |
stop = time.time() |
| 106 |
assert stop - start < 30.0 # ~0.6 sec on pypy-c-jit |
| 107 |
|
| 108 |
def test_at_fork_reinit(self): |
| 109 |
import _thread as thread |
| 110 |
def use_lock(lock): |
| 111 |
# make sure that the lock still works normally |
| 112 |
# after _at_fork_reinit() |
| 113 |
lock.acquire() |
| 114 |
lock.release() |
| 115 |
|
| 116 |
# unlocked |
| 117 |
for constr in [thread.allocate_lock, thread.RLock]: |
| 118 |
lock = constr() |
| 119 |
lock._at_fork_reinit() |
| 120 |
use_lock(lock) |
| 121 |
|
| 122 |
# locked: _at_fork_reinit() resets the lock to the unlocked state |
| 123 |
lock2 = constr() |
| 124 |
lock2.acquire() |
| 125 |
lock2._at_fork_reinit() |
| 126 |
use_lock(lock2) |
| 127 |
|
| 128 |
def test_compile_lock(): |
| 129 |
from rpython.rlib import rgc |
| 130 |
from rpython.rlib.rthread import allocate_lock |
| 131 |
def g(): |
| 132 |
l = allocate_lock() |
| 133 |
ok1 = l.acquire(True) |
| 134 |
ok2 = l.acquire(False) |
| 135 |
l.release() |
| 136 |
ok3 = l.acquire(False) |
| 137 |
res = ok1 and not ok2 and ok3 |
| 138 |
return res |
| 139 |
g._dont_inline_ = True |
| 140 |
def f(): |
| 141 |
res = g() |
| 142 |
# the lock must have been freed by now - we use refcounting |
| 143 |
return res |
| 144 |
fn = compile(f, [], gcpolicy='ref') |
| 145 |
res = fn() |
| 146 |
assert res |
| 147 |
|
| 148 |
|
| 149 |
class AppTestLockAgain(GenericTestThread): |
| 150 |
# test it at app-level again to detect strange interactions |
| 151 |
test_lock_again = AppTestLock.test_lock_basic.im_func |
| 152 |
|
| 153 |
|
| 154 |
class AppTestRLock(GenericTestThread): |
| 155 |
""" |
| 156 |
Tests for recursive locks. |
| 157 |
""" |
| 158 |
def test_reacquire(self): |
| 159 |
import _thread |
| 160 |
lock = _thread.RLock() |
| 161 |
lock.acquire() |
| 162 |
lock.acquire() |
| 163 |
lock.release() |
| 164 |
lock.acquire() |
| 165 |
lock.release() |
| 166 |
lock.release() |
| 167 |
|
| 168 |
def test_release_unacquired(self): |
| 169 |
# Cannot release an unacquired lock |
| 170 |
import _thread |
| 171 |
lock = _thread.RLock() |
| 172 |
raises(RuntimeError, lock.release) |
| 173 |
lock.acquire() |
| 174 |
lock.acquire() |
| 175 |
lock.release() |
| 176 |
lock.acquire() |
| 177 |
lock.release() |
| 178 |
lock.release() |
| 179 |
raises(RuntimeError, lock.release) |
| 180 |
|
| 181 |
def test_release_save(self): |
| 182 |
import _thread |
| 183 |
lock = _thread.RLock() |
| 184 |
raises(RuntimeError, lock._release_save) |
| 185 |
lock.acquire() |
| 186 |
state = lock._release_save() |
| 187 |
lock._acquire_restore(state) |
| 188 |
lock.release() |
| 189 |
|
| 190 |
def test__is_owned(self): |
| 191 |
import _thread |
| 192 |
lock = _thread.RLock() |
| 193 |
assert lock._is_owned() is False |
| 194 |
lock.acquire() |
| 195 |
assert lock._is_owned() is True |
| 196 |
lock.acquire() |
| 197 |
assert lock._is_owned() is True |
| 198 |
lock.release() |
| 199 |
assert lock._is_owned() is True |
| 200 |
lock.release() |
| 201 |
assert lock._is_owned() is False |
| 202 |
|
| 203 |
def test_context_manager(self): |
| 204 |
import _thread |
| 205 |
lock = _thread.RLock() |
| 206 |
with lock: |
| 207 |
assert lock._is_owned() is True |
| 208 |
|
| 209 |
def test_timeout(self): |
| 210 |
import _thread |
| 211 |
lock = _thread.RLock() |
| 212 |
assert lock.acquire() is True |
| 213 |
assert lock.acquire(False) is True |
| 214 |
assert lock.acquire(True, timeout=.1) is True |
| 215 |
|
| 216 |
|
| 217 |
class AppTestLockSignals(GenericTestThread): |
| 218 |
pytestmark = py.test.mark.skipif("os.name != 'posix'") |
| 219 |
|
| 220 |
def w_acquire_retries_on_intr(self, lock): |
| 221 |
import _thread, os, signal, time |
| 222 |
self.sig_recvd = False |
| 223 |
def my_handler(signal, frame): |
| 224 |
self.sig_recvd = True |
| 225 |
old_handler = signal.signal(signal.SIGUSR1, my_handler) |
| 226 |
try: |
| 227 |
ready = _thread.allocate_lock() |
| 228 |
ready.acquire() |
| 229 |
def other_thread(): |
| 230 |
# Acquire the lock in a non-main thread, so this test works for |
| 231 |
# RLocks. |
| 232 |
lock.acquire() |
| 233 |
# Notify the main thread that we're ready |
| 234 |
ready.release() |
| 235 |
# Wait for 5 seconds here |
| 236 |
for n in range(50): |
| 237 |
time.sleep(0.1) |
| 238 |
# Send the signal |
| 239 |
os.kill(os.getpid(), signal.SIGUSR1) |
| 240 |
# Let the main thread take the interrupt, handle it, and retry |
| 241 |
# the lock acquisition. Then we'll let it run. |
| 242 |
for n in range(50): |
| 243 |
time.sleep(0.1) |
| 244 |
lock.release() |
| 245 |
_thread.start_new_thread(other_thread, ()) |
| 246 |
ready.acquire() |
| 247 |
result = lock.acquire() # Block while we receive a signal. |
| 248 |
assert self.sig_recvd |
| 249 |
assert result |
| 250 |
finally: |
| 251 |
signal.signal(signal.SIGUSR1, old_handler) |
| 252 |
for i in range(50): |
| 253 |
time.sleep(0.1) |
| 254 |
|
| 255 |
def test_lock_acquire_retries_on_intr(self): |
| 256 |
import _thread |
| 257 |
self.acquire_retries_on_intr(_thread.allocate_lock()) |
| 258 |
|
| 259 |
def test_rlock_acquire_retries_on_intr(self): |
| 260 |
import _thread |
| 261 |
self.acquire_retries_on_intr(_thread.RLock()) |
| 262 |
|
| 263 |
def w_alarm_interrupt(self, sig, frame): |
| 264 |
raise KeyboardInterrupt |
| 265 |
|
| 266 |
def test_lock_acquire_interruption(self): |
| 267 |
import _thread, signal, time |
| 268 |
# Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck |
| 269 |
# in a deadlock. |
| 270 |
# XXX this test can fail when the legacy (non-semaphore) implementation |
| 271 |
# of locks is used in thread_pthread.h, see issue #11223. |
| 272 |
oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt) |
| 273 |
try: |
| 274 |
lock = _thread.allocate_lock() |
| 275 |
lock.acquire() |
| 276 |
signal.alarm(1) |
| 277 |
t1 = time.time() |
| 278 |
# XXX: raises doesn't work here? |
| 279 |
#raises(KeyboardInterrupt, lock.acquire, timeout=5) |
| 280 |
try: |
| 281 |
lock.acquire(timeout=5) |
| 282 |
except KeyboardInterrupt: |
| 283 |
pass |
| 284 |
else: |
| 285 |
assert False, 'Expected KeyboardInterrupt' |
| 286 |
dt = time.time() - t1 |
| 287 |
# Checking that KeyboardInterrupt was raised is not sufficient. |
| 288 |
# We want to assert that lock.acquire() was interrupted because |
| 289 |
# of the signal, not that the signal handler was called immediately |
| 290 |
# after timeout return of lock.acquire() (which can fool assertRaises). |
| 291 |
assert dt < 3.0 |
| 292 |
finally: |
| 293 |
signal.signal(signal.SIGALRM, oldalrm) |
| 294 |
|
| 295 |
def test_rlock_acquire_interruption(self): |
| 296 |
import _thread, signal, time |
| 297 |
# Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck |
| 298 |
# in a deadlock. |
| 299 |
# XXX this test can fail when the legacy (non-semaphore) implementation |
| 300 |
# of locks is used in thread_pthread.h, see issue #11223. |
| 301 |
oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt) |
| 302 |
try: |
| 303 |
rlock = _thread.RLock() |
| 304 |
# For reentrant locks, the initial acquisition must be in another |
| 305 |
# thread. |
| 306 |
def other_thread(): |
| 307 |
rlock.acquire() |
| 308 |
_thread.start_new_thread(other_thread, ()) |
| 309 |
# Wait until we can't acquire it without blocking... |
| 310 |
while rlock.acquire(blocking=False): |
| 311 |
rlock.release() |
| 312 |
time.sleep(0.01) |
| 313 |
signal.alarm(1) |
| 314 |
t1 = time.time() |
| 315 |
#raises(KeyboardInterrupt, rlock.acquire, timeout=5) |
| 316 |
try: |
| 317 |
rlock.acquire(timeout=5) |
| 318 |
except KeyboardInterrupt: |
| 319 |
pass |
| 320 |
else: |
| 321 |
assert False, 'Expected KeyboardInterrupt' |
| 322 |
dt = time.time() - t1 |
| 323 |
# See rationale above in test_lock_acquire_interruption |
| 324 |
assert dt < 3.0 |
| 325 |
finally: |
| 326 |
signal.signal(signal.SIGALRM, oldalrm) |
| 327 |
|
| 328 |
|
| 329 |
class AppTestLockRepr(GenericTestThread): |
| 330 |
|
| 331 |
def test_lock_repr(self): |
| 332 |
import _thread |
| 333 |
lock = _thread.allocate_lock() |
| 334 |
assert repr(lock).startswith("<unlocked _thread.lock object at ") |
| 335 |
lock.acquire() |
| 336 |
assert repr(lock).startswith("<locked _thread.lock object at ") |
| 337 |
|
| 338 |
def test_rlock_repr(self): |
| 339 |
import _thread |
| 340 |
rlock = _thread.RLock() |
| 341 |
assert repr(rlock).startswith( |
| 342 |
"<unlocked _thread.RLock object owner=0 count=0 at ") |
| 343 |
rlock.acquire() |
| 344 |
rlock.acquire() |
| 345 |
assert repr(rlock).startswith("<locked _thread.RLock object owner=") |
| 346 |
assert 'owner=0' not in repr(rlock) |
| 347 |
assert " count=2 at " in repr(rlock) |