| 1 |
import pytest |
| 2 |
import time |
| 3 |
import sys |
| 4 |
from rpython.rlib.rgil import yield_thread |
| 5 |
from pypy.interpreter.gateway import interp2app |
| 6 |
from pypy.module.thread.os_lock import _set_sentinel |
| 7 |
from pypy.module.thread.os_thread import start_new_thread |
| 8 |
from pypy.module._multiprocessing.interp_semaphore import ( |
| 9 |
create_semaphore, delete_semaphore, W_SemLock, sem_unlink) |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.skipif(sys.platform == 'win32', reason='hangs on win32') |
| 13 |
@pytest.mark.parametrize('spaceconfig', [ |
| 14 |
{'usemodules': ['_multiprocessing', 'thread']}]) |
| 15 |
@pytest.mark.skipif(sys.platform == 'darwin', reason="Hangs on macOSX") |
| 16 |
def test_semlock_release(space): |
| 17 |
|
| 18 |
space.getbuiltinmodule('time') |
| 19 |
sem_name = '/test8' |
| 20 |
_handle = create_semaphore(space, sem_name, 1, 1) |
| 21 |
try: |
| 22 |
sem_unlink(sem_name) |
| 23 |
w_lock = W_SemLock(space, _handle, 0, 1, None) |
| 24 |
created = [] |
| 25 |
successful = [] |
| 26 |
N_THREADS = 16 |
| 27 |
|
| 28 |
def run(space): |
| 29 |
w_sentinel = _set_sentinel(space) |
| 30 |
yield_thread() |
| 31 |
w_sentinel.descr_lock_acquire(space) |
| 32 |
try: |
| 33 |
yield_thread() |
| 34 |
created.append(w_sentinel) |
| 35 |
w_got = w_lock.acquire(space, w_timeout=space.newfloat(5.)) |
| 36 |
if space.is_true(w_got): |
| 37 |
yield_thread() |
| 38 |
w_lock.release(space) |
| 39 |
successful.append(w_sentinel) |
| 40 |
except: |
| 41 |
import traceback |
| 42 |
traceback.print_exc() |
| 43 |
raise |
| 44 |
w_run = space.wrap(interp2app(run)) |
| 45 |
|
| 46 |
w_lock.acquire(space) |
| 47 |
for _ in range(N_THREADS): |
| 48 |
start_new_thread(space, w_run, space.newtuple([])) |
| 49 |
deadline = time.time() + 5. |
| 50 |
while len(created) < N_THREADS: |
| 51 |
assert time.time() < deadline |
| 52 |
yield_thread() |
| 53 |
w_lock.release(space) |
| 54 |
|
| 55 |
for w_sentinel in created: |
| 56 |
|
| 57 |
w_sentinel.descr_lock_acquire(space) |
| 58 |
w_sentinel.descr_lock_release(space) |
| 59 |
assert len(successful) == N_THREADS |
| 60 |
finally: |
| 61 |
delete_semaphore(_handle) |