|
5 | 5 | create_string_buffer, create_unicode_buffer, |
6 | 6 | c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, |
7 | 7 | c_long, c_ulonglong, c_float, c_double, c_longdouble) |
8 | | -from test.support import bigmemtest, _2G |
| 8 | +from test.support import bigmemtest, _2G, threading_helper, Py_GIL_DISABLED |
9 | 9 | from ._support import (_CData, PyCArrayType, Py_TPFLAGS_DISALLOW_INSTANTIATION, |
10 | 10 | Py_TPFLAGS_IMMUTABLETYPE) |
11 | 11 |
|
@@ -267,6 +267,26 @@ def test_bpo36504_signed_int_overflow(self): |
267 | 267 | def test_large_array(self, size): |
268 | 268 | c_char * size |
269 | 269 |
|
| 270 | + @threading_helper.requires_working_threading() |
| 271 | + @unittest.skipUnless(Py_GIL_DISABLED, "only meaningful if the GIL is disabled") |
| 272 | + def test_thread_safety(self): |
| 273 | + from threading import Thread |
| 274 | + |
| 275 | + buffer = (ctypes.c_char_p * 10)() |
| 276 | + |
| 277 | + def run(): |
| 278 | + for i in range(100): |
| 279 | + buffer.value = b"hello" |
| 280 | + buffer[0] = b"j" |
| 281 | + |
| 282 | + with threading_helper.catch_threading_exception() as cm: |
| 283 | + threads = (Thread(target=run) for _ in range(25)) |
| 284 | + with threading_helper.start_threads(threads): |
| 285 | + pass |
| 286 | + |
| 287 | + if cm.exc_value: |
| 288 | + raise cm.exc_value |
| 289 | + |
270 | 290 |
|
271 | 291 | if __name__ == '__main__': |
272 | 292 | unittest.main() |
0 commit comments