-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[samples/ffi] Sample for asynchronous callbacks
Issue: #37022 (comment) Change-Id: If30d168e6666131b6d96d5885a0dbe32291b1ef9 Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134704 Reviewed-by: Martin Kustermann <[email protected]>
- Loading branch information
1 parent
b20c35c
commit 76ef075
Showing
5 changed files
with
327 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
// | ||
// This file exercises the sample files so that they are tested. | ||
// | ||
// SharedObjects=ffi_test_dynamic_library ffi_test_functions | ||
|
||
import 'sample_async_callback.dart' as sample0; | ||
|
||
main() { | ||
sample0.main(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
// | ||
// Sample showing how to do async callbacks by telling the Dart isolate to | ||
// yields its execution thread to C so it can perform the callbacks on the | ||
// main Dart thread. | ||
// | ||
// TODO(dartbug.com/37022): Update this when we get real async callbacks. | ||
// TODO(dartbug.com/40564): On Windows DLL is wrongly linked against dart.exe | ||
// instead of dart_precompiled_runtime.exe | ||
|
||
import 'dart:ffi'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:expect/expect.dart'; | ||
|
||
import '../dylib_utils.dart'; | ||
|
||
int globalResult = 0; | ||
int numCallbacks1 = 0; | ||
int numCallbacks2 = 0; | ||
|
||
main() async { | ||
print("Dart = Dart mutator thread executing Dart."); | ||
print("C Da = Dart mutator thread executing C."); | ||
print("C T1 = Some C thread executing C."); | ||
print("C T2 = Some C thread executing C."); | ||
print("C = C T1 or C T2."); | ||
print("Dart: Setup."); | ||
final interactiveCppRequests = ReceivePort()..listen(requestExecuteCallback); | ||
|
||
final int nativePort = interactiveCppRequests.sendPort.nativePort; | ||
registerCallback1(nativePort, callback1FP); | ||
registerCallback2(nativePort, callback2FP); | ||
print("Dart: Tell C to start worker threads."); | ||
startWorkSimulator(); | ||
|
||
// We need to yield control in order to be able to receive messages. | ||
while (numCallbacks2 < 3) { | ||
print("Dart: Yielding (able to receive messages on port)."); | ||
await asyncSleep(500); | ||
} | ||
print("Dart: Received expected number of callbacks."); | ||
|
||
Expect.equals(2, numCallbacks1); | ||
Expect.equals(3, numCallbacks2); | ||
Expect.equals(14, globalResult); | ||
|
||
print("Dart: Tell C to stop worker threads."); | ||
stopWorkSimulator(); | ||
interactiveCppRequests.close(); | ||
print("Dart: Done."); | ||
} | ||
|
||
int callback1(int a) { | ||
print("Dart: callback1($a)."); | ||
numCallbacks1++; | ||
return a + 3; | ||
} | ||
|
||
void callback2(int a) { | ||
print("Dart: callback2($a)."); | ||
globalResult += a; | ||
numCallbacks2++; | ||
} | ||
|
||
void requestExecuteCallback(dynamic message) { | ||
final int work_address = message; | ||
final work = Pointer<Work>.fromAddress(work_address); | ||
print("Dart: Calling into C to execute callback ($work)."); | ||
executeCallback(work); | ||
print("Dart: Done with callback."); | ||
} | ||
|
||
final callback1FP = Pointer.fromFunction<IntPtr Function(IntPtr)>(callback1, 0); | ||
|
||
final callback2FP = Pointer.fromFunction<Void Function(IntPtr)>(callback2); | ||
|
||
final dl = dlopenPlatformSpecific("ffi_test_functions"); | ||
|
||
final registerCallback1 = dl.lookupFunction< | ||
Void Function(Int64 sendPort, | ||
Pointer<NativeFunction<IntPtr Function(IntPtr)>> functionPointer), | ||
void Function(int sendPort, | ||
Pointer<NativeFunction<IntPtr Function(IntPtr)>> functionPointer)>( | ||
'RegisterMyCallbackBlocking'); | ||
|
||
final registerCallback2 = dl.lookupFunction< | ||
Void Function(Int64 sendPort, | ||
Pointer<NativeFunction<Void Function(IntPtr)>> functionPointer), | ||
void Function(int sendPort, | ||
Pointer<NativeFunction<Void Function(IntPtr)>> functionPointer)>( | ||
'RegisterMyCallbackNonBlocking'); | ||
|
||
final startWorkSimulator = | ||
dl.lookupFunction<Void Function(), void Function()>('StartWorkSimulator'); | ||
|
||
final stopWorkSimulator = | ||
dl.lookupFunction<Void Function(), void Function()>('StopWorkSimulator'); | ||
|
||
final executeCallback = dl.lookupFunction<Void Function(Pointer<Work>), | ||
void Function(Pointer<Work>)>('ExecuteCallback'); | ||
|
||
class Work extends Struct {} | ||
|
||
Future asyncSleep(int ms) { | ||
return new Future.delayed(Duration(milliseconds: ms)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters