| 1 |
import gc, time |
| 2 |
from rpython.rlib.rthread import * |
| 3 |
from rpython.rlib.rarithmetic import r_longlong |
| 4 |
from rpython.rlib import objectmodel |
| 5 |
from rpython.translator.c.test.test_boehm import AbstractGCTestClass |
| 6 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 7 |
import py |
| 8 |
import platform |
| 9 |
|
| 10 |
def test_lock(): |
| 11 |
l = allocate_lock() |
| 12 |
ok1 = l.acquire(True) |
| 13 |
ok2 = l.acquire(False) |
| 14 |
l.release() |
| 15 |
ok3 = l.acquire(False) |
| 16 |
res = ok1 and not ok2 and ok3 |
| 17 |
assert res == 1 |
| 18 |
|
| 19 |
def test_lock_is_aquired(): |
| 20 |
l = allocate_lock() |
| 21 |
ok1 = l.acquire(True) |
| 22 |
assert l.is_acquired() == True |
| 23 |
assert l.is_acquired() == True |
| 24 |
l.release() |
| 25 |
assert l.is_acquired() == False |
| 26 |
|
| 27 |
def test_thread_error(): |
| 28 |
l = allocate_lock() |
| 29 |
try: |
| 30 |
l.release() |
| 31 |
except error: |
| 32 |
pass |
| 33 |
else: |
| 34 |
py.test.fail("Did not raise") |
| 35 |
|
| 36 |
def test_tlref_untranslated(): |
| 37 |
import thread |
| 38 |
class FooBar(object): |
| 39 |
pass |
| 40 |
t = ThreadLocalReference(FooBar) |
| 41 |
results = [] |
| 42 |
def subthread(): |
| 43 |
x = FooBar() |
| 44 |
results.append(t.get() is None) |
| 45 |
t.set(x) |
| 46 |
results.append(t.get() is x) |
| 47 |
time.sleep(0.2) |
| 48 |
results.append(t.get() is x) |
| 49 |
for i in range(5): |
| 50 |
thread.start_new_thread(subthread, ()) |
| 51 |
time.sleep(0.5) |
| 52 |
assert results == [True] * 15 |
| 53 |
|
| 54 |
def test_get_ident(): |
| 55 |
import thread |
| 56 |
assert get_ident() == thread.get_ident() |
| 57 |
|
| 58 |
|
| 59 |
def test_threadlocalref_on_llinterp(): |
| 60 |
from rpython.rtyper.test.test_llinterp import interpret |
| 61 |
tlfield = ThreadLocalField(lltype.Signed, "rthread_test_") |
| 62 |
|
| 63 |
def f(): |
| 64 |
x = tlfield.setraw(42) |
| 65 |
return tlfield.getraw() |
| 66 |
|
| 67 |
res = interpret(f, []) |
| 68 |
assert res == 42 |
| 69 |
|
| 70 |
|
| 71 |
class AbstractThreadTests(AbstractGCTestClass): |
| 72 |
use_threads = True |
| 73 |
|
| 74 |
def test_start_new_thread(self): |
| 75 |
import time |
| 76 |
|
| 77 |
class State: |
| 78 |
pass |
| 79 |
state = State() |
| 80 |
|
| 81 |
def bootstrap1(): |
| 82 |
state.my_thread_ident1 = get_ident() |
| 83 |
def bootstrap2(): |
| 84 |
state.my_thread_ident2 = get_ident() |
| 85 |
|
| 86 |
def f(): |
| 87 |
state.my_thread_ident1 = get_ident() |
| 88 |
state.my_thread_ident2 = get_ident() |
| 89 |
start_new_thread(bootstrap1, ()) |
| 90 |
start_new_thread(bootstrap2, ()) |
| 91 |
willing_to_wait_more = 1000 |
| 92 |
while (state.my_thread_ident1 == get_ident() or |
| 93 |
state.my_thread_ident2 == get_ident()): |
| 94 |
willing_to_wait_more -= 1 |
| 95 |
if not willing_to_wait_more: |
| 96 |
raise Exception("thread didn't start?") |
| 97 |
time.sleep(0.01) |
| 98 |
return 42 |
| 99 |
|
| 100 |
fn = self.getcompiled(f, []) |
| 101 |
res = fn() |
| 102 |
assert res == 42 |
| 103 |
|
| 104 |
@py.test.mark.xfail(platform.machine() == 's390x' or sys.platform == 'darwin', |
| 105 |
reason='may fail this test under heavy load') |
| 106 |
def test_gc_locking(self): |
| 107 |
import time |
| 108 |
from rpython.rlib.debug import ll_assert |
| 109 |
|
| 110 |
class State: |
| 111 |
pass |
| 112 |
state = State() |
| 113 |
|
| 114 |
class Z: |
| 115 |
def __init__(self, i, j): |
| 116 |
self.i = i |
| 117 |
self.j = j |
| 118 |
def run(self): |
| 119 |
j = self.j |
| 120 |
if self.i > 1: |
| 121 |
g(self.i-1, self.j * 2) |
| 122 |
ll_assert(j == self.j, "1: bad j") |
| 123 |
g(self.i-2, self.j * 2 + 1) |
| 124 |
else: |
| 125 |
if len(state.answers) % 7 == 5: |
| 126 |
gc.collect() |
| 127 |
state.answers.append(self.j) |
| 128 |
ll_assert(j == self.j, "2: bad j") |
| 129 |
run._dont_inline_ = True |
| 130 |
|
| 131 |
def bootstrap(): |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
z = state.z |
| 139 |
state.z = None |
| 140 |
state.bootstrapping.release() |
| 141 |
z.run() |
| 142 |
gc_thread_die() |
| 143 |
|
| 144 |
|
| 145 |
def g(i, j): |
| 146 |
state.bootstrapping.acquire(True) |
| 147 |
state.z = Z(i, j) |
| 148 |
start_new_thread(bootstrap, ()) |
| 149 |
|
| 150 |
def f(): |
| 151 |
state.bootstrapping = allocate_lock() |
| 152 |
state.answers = [] |
| 153 |
state.finished = 0 |
| 154 |
|
| 155 |
g(10, 1) |
| 156 |
done = False |
| 157 |
willing_to_wait_more = 2000 |
| 158 |
while not done: |
| 159 |
if not willing_to_wait_more: |
| 160 |
break |
| 161 |
willing_to_wait_more -= 1 |
| 162 |
done = len(state.answers) == expected |
| 163 |
|
| 164 |
print "waitting %d more iterations" % willing_to_wait_more |
| 165 |
time.sleep(0.01) |
| 166 |
|
| 167 |
time.sleep(0.1) |
| 168 |
|
| 169 |
return len(state.answers) |
| 170 |
|
| 171 |
expected = 89 |
| 172 |
fn = self.getcompiled(f, []) |
| 173 |
answers = fn() |
| 174 |
assert answers == expected |
| 175 |
|
| 176 |
def test_acquire_timed_1sec(self): |
| 177 |
import time |
| 178 |
def f(): |
| 179 |
l = allocate_lock() |
| 180 |
l.acquire(True) |
| 181 |
t1 = time.time() |
| 182 |
ok = l.acquire_timed(1000001) |
| 183 |
t2 = time.time() |
| 184 |
delay = t2 - t1 |
| 185 |
if ok == 0: |
| 186 |
return -delay |
| 187 |
elif ok == 2: |
| 188 |
return delay |
| 189 |
else: |
| 190 |
return 0.0 |
| 191 |
fn = self.getcompiled(f, []) |
| 192 |
res = fn() |
| 193 |
assert res < -1.0 |
| 194 |
|
| 195 |
def test_acquire_timed_huge_timeout(self): |
| 196 |
t = r_longlong(2 ** 61) |
| 197 |
def f(): |
| 198 |
l = allocate_lock() |
| 199 |
return l.acquire_timed(t) |
| 200 |
fn = self.getcompiled(f, []) |
| 201 |
res = fn() |
| 202 |
assert res == 1 |
| 203 |
|
| 204 |
def test_acquire_timed_alarm(self): |
| 205 |
import sys |
| 206 |
if not sys.platform.startswith('linux'): |
| 207 |
py.test.skip("skipped on non-linux") |
| 208 |
import time |
| 209 |
from rpython.rlib import rsignal |
| 210 |
def f(): |
| 211 |
l = allocate_lock() |
| 212 |
l.acquire(True) |
| 213 |
|
| 214 |
rsignal.pypysig_setflag(rsignal.SIGALRM) |
| 215 |
rsignal.c_alarm(1) |
| 216 |
|
| 217 |
t1 = time.time() |
| 218 |
ok = l.acquire_timed(2500000) |
| 219 |
t2 = time.time() |
| 220 |
delay = t2 - t1 |
| 221 |
if ok == 0: |
| 222 |
return -delay |
| 223 |
elif ok == 2: |
| 224 |
return delay |
| 225 |
else: |
| 226 |
return 0.0 |
| 227 |
fn = self.getcompiled(f, []) |
| 228 |
res = fn() |
| 229 |
assert res >= 0.95 |
| 230 |
|
| 231 |
def test_tlref(self): |
| 232 |
class FooBar(object): |
| 233 |
pass |
| 234 |
t = ThreadLocalReference(FooBar) |
| 235 |
def f(): |
| 236 |
x1 = FooBar() |
| 237 |
t.set(x1) |
| 238 |
import gc; gc.collect() |
| 239 |
assert t.get() is x1 |
| 240 |
return 42 |
| 241 |
fn = self.getcompiled(f, []) |
| 242 |
res = fn() |
| 243 |
assert res == 42 |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
class TestUsingBoehm(AbstractThreadTests): |
| 252 |
gcpolicy = 'boehm' |
| 253 |
|
| 254 |
class TestUsingFramework(AbstractThreadTests): |
| 255 |
gcpolicy = 'minimark' |
| 256 |
|
| 257 |
def test_tlref_keepalive(self, no__thread=True): |
| 258 |
import weakref |
| 259 |
from rpython.config.translationoption import SUPPORT__THREAD |
| 260 |
|
| 261 |
if not (SUPPORT__THREAD or no__thread): |
| 262 |
py.test.skip("no __thread support here") |
| 263 |
|
| 264 |
class FooBar(object): |
| 265 |
def __init__(self, a, b): |
| 266 |
self.lst = [a, b] |
| 267 |
t = ThreadLocalReference(FooBar) |
| 268 |
t2 = ThreadLocalReference(FooBar) |
| 269 |
|
| 270 |
def tset(): |
| 271 |
x1 = FooBar(40, 2) |
| 272 |
t.set(x1) |
| 273 |
return weakref.ref(x1) |
| 274 |
tset._dont_inline_ = True |
| 275 |
|
| 276 |
def t2set(): |
| 277 |
x1 = FooBar(50, 3) |
| 278 |
t2.set(x1) |
| 279 |
return weakref.ref(x1) |
| 280 |
t2set._dont_inline_ = True |
| 281 |
|
| 282 |
class WrFromThread: |
| 283 |
pass |
| 284 |
wr_from_thread = WrFromThread() |
| 285 |
|
| 286 |
def f(): |
| 287 |
config = objectmodel.fetch_translated_config() |
| 288 |
assert t.automatic_keepalive(config) is True |
| 289 |
wr = tset() |
| 290 |
wr2 = t2set() |
| 291 |
import gc; gc.collect() |
| 292 |
x1 = t.get() |
| 293 |
assert x1 is not None |
| 294 |
assert wr() is not None |
| 295 |
assert wr() is x1 |
| 296 |
assert x1.lst == [40, 2] |
| 297 |
x2 = t2.get() |
| 298 |
assert x2 is not None |
| 299 |
assert wr2() is not None |
| 300 |
assert wr2() is x2 |
| 301 |
assert x2.lst == [50, 3] |
| 302 |
return wr, wr2 |
| 303 |
|
| 304 |
def thread_entry_point(): |
| 305 |
wr, wr2 = f() |
| 306 |
wr_from_thread.wr = wr |
| 307 |
wr_from_thread.wr2 = wr2 |
| 308 |
wr_from_thread.seen = True |
| 309 |
|
| 310 |
def main(): |
| 311 |
wr_from_thread.seen = False |
| 312 |
start_new_thread(thread_entry_point, ()) |
| 313 |
wr1, wr2 = f() |
| 314 |
count = 0 |
| 315 |
while True: |
| 316 |
time.sleep(0.5) |
| 317 |
if wr_from_thread.seen or count >= 50: |
| 318 |
break |
| 319 |
count += 1 |
| 320 |
assert wr_from_thread.seen is True |
| 321 |
wr_other_1 = wr_from_thread.wr |
| 322 |
wr_other_2 = wr_from_thread.wr2 |
| 323 |
import gc; gc.collect() |
| 324 |
assert wr1() is not None |
| 325 |
assert wr2() is not None |
| 326 |
assert wr_other_1() is None |
| 327 |
assert wr_other_2() is None |
| 328 |
assert wr1().lst == [40, 2] |
| 329 |
assert wr2().lst == [50, 3] |
| 330 |
return 42 |
| 331 |
|
| 332 |
extra_options = {'no__thread': no__thread, 'shared': True} |
| 333 |
fn = self.getcompiled(main, [], extra_options=extra_options) |
| 334 |
res = fn() |
| 335 |
assert res == 42 |
| 336 |
|
| 337 |
def test_tlref_keepalive__thread(self): |
| 338 |
self.test_tlref_keepalive(no__thread=False) |