| 1 |
from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC |
| 2 |
|
| 3 |
|
| 4 |
class TestThread(BaseTestPyPyC): |
| 5 |
def test_simple(self): |
| 6 |
def main(n): |
| 7 |
try: |
| 8 |
import _thread as thread |
| 9 |
except ImportError: |
| 10 |
import thread |
| 11 |
def f(): |
| 12 |
i = 0 |
| 13 |
while i < n: |
| 14 |
i += 1 |
| 15 |
done.release() |
| 16 |
|
| 17 |
done = thread.allocate_lock() |
| 18 |
done.acquire() |
| 19 |
thread.start_new_thread(f, ()) |
| 20 |
done.acquire() |
| 21 |
return 0 |
| 22 |
log = self.run(main, [500]) |
| 23 |
assert round(log.result, 6) == round(main(500), 6) |
| 24 |
loop, = log.loops_by_filename(self.filepath) |
| 25 |
assert loop.match(""" |
| 26 |
i2 = int_lt(i0, i1) |
| 27 |
guard_true(i2, descr=...) |
| 28 |
i3 = int_add(i0, 1) |
| 29 |
--THREAD-TICK-- |
| 30 |
jump(..., descr=...) |
| 31 |
""") |
| 32 |
|
| 33 |
def test_tls(self): |
| 34 |
def main(n): |
| 35 |
try: |
| 36 |
from _thread import _local |
| 37 |
except ImportError: |
| 38 |
from thread import _local |
| 39 |
local = _local() |
| 40 |
local.x = "abc" |
| 41 |
local.x = 1 |
| 42 |
i = 0 |
| 43 |
while i < n: |
| 44 |
i += local.x |
| 45 |
return 0 |
| 46 |
log = self.run(main, [500]) |
| 47 |
assert round(log.result, 6) == round(main(500), 6) |
| 48 |
loop, = log.loops_by_filename(self.filepath) |
| 49 |
assert loop.match(""" |
| 50 |
i53 = int_lt(i48, i27) |
| 51 |
guard_true(i53, descr=...) |
| 52 |
i54 = int_add_ovf(i48, i47) |
| 53 |
guard_no_overflow(descr=...) |
| 54 |
--TICK-- |
| 55 |
i58 = arraylen_gc(p43, descr=...) |
| 56 |
jump(..., descr=...) |
| 57 |
""") |
| 58 |
|
| 59 |
def test_lock_acquire_release(self): |
| 60 |
def main(n): |
| 61 |
import threading |
| 62 |
lock = threading.Lock() |
| 63 |
while n > 0: |
| 64 |
with lock: |
| 65 |
n -= 1 |
| 66 |
log = self.run(main, [500]) |
| 67 |
assert log.result == main(500) |
| 68 |
loop, = log.loops_by_filename(self.filepath) |
| 69 |
assert loop.match(""" |
| 70 |
i56 = int_gt(i44, 0) |
| 71 |
guard_true(i56, descr=...) |
| 72 |
p57 = force_token() |
| 73 |
setfield_gc(p0, p57, descr=<FieldP pypy.interpreter.pyframe.PyFrame.vable_token 8>) |
| 74 |
i58 = call_may_force_i(ConstClass(acquire_timed), p31, -1, descr=<Calli . ri EF=7>) |
| 75 |
guard_not_forced(descr=...) |
| 76 |
guard_no_exception(descr=...) |
| 77 |
i99 = int_eq(i58, 1) |
| 78 |
guard_true(i99, descr=...) |
| 79 |
i58 = int_sub(i44, 1) |
| 80 |
guard_not_invalidated? |
| 81 |
i59 = call_i(ConstClass(RPyThreadReleaseLock), i37, descr=<Calli . i EF=2>) |
| 82 |
i60 = int_is_true(i59) |
| 83 |
guard_false(i60, descr=...) |
| 84 |
--TICK-- |
| 85 |
jump(..., descr=...) |
| 86 |
""") |