| 1 |
import time |
| 2 |
from pypy.module.thread import gil |
| 3 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 4 |
from rpython.rtyper.lltypesystem import lltype |
| 5 |
from rpython.rlib import rgil |
| 6 |
from rpython.rlib.test import test_rthread |
| 7 |
from rpython.rlib import rthread as thread |
| 8 |
from rpython.rlib.objectmodel import we_are_translated |
| 9 |
|
| 10 |
class FakeEC(object): |
| 11 |
pass |
| 12 |
|
| 13 |
class FakeActionFlag(object): |
| 14 |
def register_periodic_action(self, action, use_bytecode_counter): |
| 15 |
pass |
| 16 |
def get(self): |
| 17 |
return 0 |
| 18 |
def set(self, x): |
| 19 |
pass |
| 20 |
|
| 21 |
class FakeSpace(object): |
| 22 |
def __init__(self): |
| 23 |
self.actionflag = FakeActionFlag() |
| 24 |
def _freeze_(self): |
| 25 |
return True |
| 26 |
def getexecutioncontext(self): |
| 27 |
return FakeEC() |
| 28 |
def fromcache(self, key): |
| 29 |
raise NotImplementedError |
| 30 |
|
| 31 |
|
| 32 |
class GILTests(test_rthread.AbstractGCTestClass): |
| 33 |
use_threads = True |
| 34 |
bigtest = False |
| 35 |
|
| 36 |
def test_one_thread(self, skew=+1): |
| 37 |
from rpython.rlib.debug import debug_print |
| 38 |
if self.bigtest: |
| 39 |
N = 100000 |
| 40 |
skew *= 25000 |
| 41 |
else: |
| 42 |
N = 100 |
| 43 |
skew *= 25 |
| 44 |
space = FakeSpace() |
| 45 |
class State: |
| 46 |
pass |
| 47 |
state = State() |
| 48 |
def runme(main=False): |
| 49 |
j = 0 |
| 50 |
for i in range(N + [-skew, skew][main]): |
| 51 |
state.datalen1 += 1 |
| 52 |
state.datalen2 += 1 |
| 53 |
state.data.append((thread.get_ident(), i)) |
| 54 |
state.datalen3 += 1 |
| 55 |
state.datalen4 += 1 |
| 56 |
assert state.datalen1 == len(state.data) |
| 57 |
assert state.datalen2 == len(state.data) |
| 58 |
assert state.datalen3 == len(state.data) |
| 59 |
assert state.datalen4 == len(state.data) |
| 60 |
debug_print(main, i, state.datalen4) |
| 61 |
rgil.yield_thread() |
| 62 |
assert i == j |
| 63 |
j += 1 |
| 64 |
def bootstrap(): |
| 65 |
try: |
| 66 |
runme() |
| 67 |
except Exception as e: |
| 68 |
assert 0 |
| 69 |
thread.gc_thread_die() |
| 70 |
my_gil_threadlocals = gil.GILThreadLocals(space) |
| 71 |
def f(): |
| 72 |
state.data = [] |
| 73 |
state.datalen1 = 0 |
| 74 |
state.datalen2 = 0 |
| 75 |
state.datalen3 = 0 |
| 76 |
state.datalen4 = 0 |
| 77 |
state.threadlocals = my_gil_threadlocals |
| 78 |
state.threadlocals.setup_threads(space) |
| 79 |
subident = thread.start_new_thread(bootstrap, ()) |
| 80 |
mainident = thread.get_ident() |
| 81 |
runme(True) |
| 82 |
still_waiting = 3000 |
| 83 |
while len(state.data) < 2*N: |
| 84 |
debug_print(len(state.data)) |
| 85 |
if not still_waiting: |
| 86 |
llop.debug_print(lltype.Void, "timeout. progress: " |
| 87 |
"%d of %d (= %f%%)" % \ |
| 88 |
(len(state.data), 2*N, 100*len(state.data)/(2.0*N))) |
| 89 |
raise ValueError("time out") |
| 90 |
still_waiting -= 1 |
| 91 |
if not we_are_translated(): rgil.release() |
| 92 |
time.sleep(0.1) |
| 93 |
if not we_are_translated(): rgil.acquire() |
| 94 |
debug_print("leaving!") |
| 95 |
i1 = i2 = 0 |
| 96 |
for tid, i in state.data: |
| 97 |
if tid == mainident: |
| 98 |
assert i == i1; i1 += 1 |
| 99 |
elif tid == subident: |
| 100 |
assert i == i2; i2 += 1 |
| 101 |
else: |
| 102 |
assert 0 |
| 103 |
assert i1 == N + skew |
| 104 |
assert i2 == N - skew |
| 105 |
return len(state.data) |
| 106 |
|
| 107 |
fn = self.getcompiled(f, []) |
| 108 |
res = fn() |
| 109 |
assert res == 2*N |
| 110 |
|
| 111 |
def test_one_thread_rev(self): |
| 112 |
self.test_one_thread(skew=-1) |
| 113 |
|
| 114 |
|
| 115 |
class TestRunDirectly(GILTests): |
| 116 |
def getcompiled(self, f, argtypes): |
| 117 |
return f |
| 118 |
|
| 119 |
class TestUsingFramework(GILTests): |
| 120 |
gcpolicy = 'generation' |
| 121 |
bigtest = True |