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