Debian Codegraph @

extra_tests/cffi_tests/cffi0/callback_in_thread.py

raw symbols SCIP
1 # Generated by pypy/tool/import_cffi.py
2 import sys, time
3 sys.path.insert(0, sys.argv[1])
4 from cffi import FFI
5
6 def _run_callback_in_thread():
7 ffi = FFI()
8 ffi.cdef("""
9 typedef int (*mycallback_func_t)(int, int);
10 int threaded_ballback_test(mycallback_func_t mycb);
11 """)
12 lib = ffi.verify("""
13 #include <pthread.h>
14 typedef int (*mycallback_func_t)(int, int);
15 void *my_wait_function(void *ptr) {
16 mycallback_func_t cbfunc = (mycallback_func_t)ptr;
17 cbfunc(10, 10);
18 cbfunc(12, 15);
19 return NULL;
20 }
21 int threaded_ballback_test(mycallback_func_t mycb) {
22 pthread_t thread;
23 pthread_create(&thread, NULL, my_wait_function, (void*)mycb);
24 return 0;
25 }
26 """, extra_compile_args=['-pthread'])
27 seen = []
28 @ffi.callback('int(*)(int,int)')
29 def mycallback(x, y):
30 time.sleep(0.022)
31 seen.append((x, y))
32 return 0
33 lib.threaded_ballback_test(mycallback)
34 count = 300
35 while len(seen) != 2:
36 time.sleep(0.01)
37 count -= 1
38 assert count > 0, "timeout"
39 assert seen == [(10, 10), (12, 15)]
40
41 print('STARTING')
42 _run_callback_in_thread()
43 print('DONE')