|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Tests that interleaved creation and calling of many NativeCallable.listener |
| 6 | +// callbacks does not deadlock. |
| 7 | +// Regression test for https://github.com/dart-lang/sdk/issues/61272 |
| 8 | +// |
| 9 | +// VMOptions= |
| 10 | +// VMOptions=--use-slow-path |
| 11 | +// VMOptions=--use-slow-path --stacktrace-every=100 |
| 12 | +// VMOptions=--dwarf_stack_traces --no-retain_function_objects --no-retain_code_objects |
| 13 | +// VMOptions=--test_il_serialization |
| 14 | +// VMOptions=--profiler --profile_vm=true |
| 15 | +// VMOptions=--profiler --profile_vm=false |
| 16 | +// SharedObjects=ffi_test_functions |
| 17 | + |
| 18 | +import 'dart:async'; |
| 19 | +import 'dart:ffi'; |
| 20 | + |
| 21 | +import 'dylib_utils.dart'; |
| 22 | + |
| 23 | +typedef FnStartNativeType = Pointer Function(Pointer); |
| 24 | +typedef FnStartType = Pointer Function(Pointer); |
| 25 | +typedef FnStopNativeType = Void Function(Pointer); |
| 26 | +typedef FnStopType = void Function(Pointer); |
| 27 | + |
| 28 | +late final FnStartType callFunctionOnNewThreadRepeatedly; |
| 29 | +late final FnStopType callFunctionOnNewThreadStop; |
| 30 | + |
| 31 | +void main() async { |
| 32 | + final ffiTestFunctions = dlopenPlatformSpecific('ffi_test_functions'); |
| 33 | + callFunctionOnNewThreadRepeatedly = ffiTestFunctions |
| 34 | + .lookupFunction<FnStartNativeType, FnStartType>( |
| 35 | + 'CallFunctionOnNewThreadRepeatedly', |
| 36 | + ); |
| 37 | + callFunctionOnNewThreadStop = ffiTestFunctions |
| 38 | + .lookupFunction<FnStopNativeType, FnStopType>( |
| 39 | + 'CallFunctionOnNewThreadStop', |
| 40 | + ); |
| 41 | + |
| 42 | + final repeaters = <(Pointer, NativeCallable)>[]; |
| 43 | + void spawn(void Function(int) callback) { |
| 44 | + final listener = NativeCallable<Void Function(Int32)>.listener(callback); |
| 45 | + final repeater = callFunctionOnNewThreadRepeatedly(listener.nativeFunction); |
| 46 | + repeaters.add((repeater, listener)); |
| 47 | + } |
| 48 | + |
| 49 | + // Spawn one repeater, wait for it to start running, then spawn 30 more. |
| 50 | + final firstIsRunning = Completer<void>(); |
| 51 | + spawn((int n) { |
| 52 | + if (n == 10) firstIsRunning.complete(); |
| 53 | + }); |
| 54 | + |
| 55 | + await firstIsRunning.future; |
| 56 | + |
| 57 | + for (var i = 0; i < 30; ++i) { |
| 58 | + spawn((int n) {}); |
| 59 | + } |
| 60 | + |
| 61 | + await Future.delayed(Duration(seconds: 3)); |
| 62 | + |
| 63 | + for (final (repeater, listener) in repeaters) { |
| 64 | + callFunctionOnNewThreadStop(repeater); |
| 65 | + listener.close(); |
| 66 | + } |
| 67 | +} |
0 commit comments