Skip to content

Commit f9eb97f

Browse files
committed
Reland "[vm] Expose Dart_{CurrentIsolate,ExitIsolate,EnterIsolate}"
For applications that want to have arbitrary number of isolates call into native code that may be blocking, we expose the API functions that allows those native threads to exit an isolate before running long/blocking code. Without the ability to exit/re-enter isolate, one may experience deadlocks as we have a fixed limit on the number of concurrently executing isolates atm. In the longer term we may find a way to do this automatically with low overhead, see [0]. But since those API functions are quite stable and we already expose e.g. `Dart_{Enter,Exit}Scope`, I don't see a reason not to expose `Dart_{Enter,Exit}Isolate`. Difference to original CL: Do use STL synchronization primitives (as the ones in runtime/bin are not always available in shared libraries) [0] Issue #51261 Issue #51254 TEST=ffi{,_2}/dl_api_exit_enter_isolate_test Change-Id: Id817e8d4edb3db35f029248d62388cbd0682001d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/294980 Reviewed-by: Daco Harkes <[email protected]>
1 parent cc40e17 commit f9eb97f

5 files changed

+81
-9
lines changed

runtime/bin/ffi_test/ffi_test_functions_vmspecific.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -894,16 +894,11 @@ DART_EXPORT void ThreadPoolTest_BarrierSync(
894894
dart_exit_isolate();
895895
{
896896
std::unique_lock<std::mutex> lock(mutex);
897-
898897
++thread_count;
899-
if (thread_count < num_threads) {
900-
while (thread_count < num_threads) {
901-
cvar.wait(lock);
902-
}
903-
} else {
904-
if (thread_count != num_threads) FATAL("bug");
905-
cvar.notify_all();
898+
while (thread_count < num_threads) {
899+
cvar.wait(lock);
906900
}
901+
cvar.notify_all();
907902
}
908903
dart_enter_isolate(isolate);
909904
}
@@ -1282,6 +1277,11 @@ DART_EXPORT void SetFfiNativeResolverForTest(Dart_Handle url) {
12821277
ENSURE(!Dart_IsError(result));
12831278
}
12841279

1280+
DART_EXPORT void WaitUntilNThreadsEnterBarrier(intptr_t num_threads) {
1281+
ThreadPoolTest_BarrierSync(Dart_CurrentIsolate_DL, Dart_EnterIsolate_DL,
1282+
Dart_ExitIsolate_DL, num_threads);
1283+
}
1284+
12851285
////////////////////////////////////////////////////////////////////////////////
12861286
// Helper for the regression test for b/216834909
12871287
////////////////////////////////////////////////////////////////////////////////

runtime/include/dart_api_dl.h

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ typedef void (*Dart_NativeMessageHandler_DL)(Dart_Port_DL dest_port_id,
9090
F(Dart_UpdateFinalizableExternalSize, void, \
9191
(Dart_FinalizableHandle object, Dart_Handle strong_ref_to_object, \
9292
intptr_t external_allocation_size)) \
93+
/* Isolates */ \
94+
F(Dart_CurrentIsolate, Dart_Isolate, (void)) \
95+
F(Dart_ExitIsolate, void, (void)) \
96+
F(Dart_EnterIsolate, void, (Dart_Isolate)) \
9397
/* Dart_Port */ \
9498
F(Dart_Post, bool, (Dart_Port_DL port_id, Dart_Handle object)) \
9599
F(Dart_NewSendPort, Dart_Handle, (Dart_Port_DL port_id)) \

runtime/include/dart_version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
// On backwards compatible changes the minor version is increased.
1212
// The versioning covers the symbols exposed in dart_api_dl.h
1313
#define DART_API_DL_MAJOR_VERSION 2
14-
#define DART_API_DL_MINOR_VERSION 2
14+
#define DART_API_DL_MINOR_VERSION 3
1515

1616
#endif /* RUNTIME_INCLUDE_DART_VERSION_H_ */ /* NOLINT */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2023, 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+
// SharedObjects=ffi_test_functions
6+
7+
import 'dart:ffi';
8+
import 'dart:isolate';
9+
10+
import 'package:expect/expect.dart';
11+
12+
import 'dylib_utils.dart';
13+
14+
final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
15+
16+
final initializeApi = ffiTestFunctions.lookupFunction<
17+
IntPtr Function(Pointer<Void>),
18+
int Function(Pointer<Void>)>("InitDartApiDL");
19+
final enterBarrier =
20+
ffiTestFunctions.lookupFunction<Void Function(IntPtr), void Function(int)>(
21+
"WaitUntilNThreadsEnterBarrier");
22+
23+
main() async {
24+
const threadBarrierCount = 30;
25+
26+
initializeApi(NativeApi.initializeApiDLData);
27+
28+
final all = <Future>[];
29+
for (int i = 0; i < threadBarrierCount; ++i) {
30+
all.add(Isolate.run(() => enterBarrier(threadBarrierCount)));
31+
}
32+
await Future.wait(all);
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2023, 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+
// @dart = 2.9
6+
7+
// SharedObjects=ffi_test_functions
8+
9+
import 'dart:ffi';
10+
import 'dart:isolate';
11+
12+
import 'package:expect/expect.dart';
13+
14+
import 'dylib_utils.dart';
15+
16+
final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
17+
18+
final initializeApi = ffiTestFunctions.lookupFunction<
19+
IntPtr Function(Pointer<Void>),
20+
int Function(Pointer<Void>)>("InitDartApiDL");
21+
final enterBarrier =
22+
ffiTestFunctions.lookupFunction<Void Function(IntPtr), void Function(int)>(
23+
"WaitUntilNThreadsEnterBarrier");
24+
25+
main() async {
26+
const threadBarrierCount = 30;
27+
28+
initializeApi(NativeApi.initializeApiDLData);
29+
30+
final all = <Future>[];
31+
for (int i = 0; i < threadBarrierCount; ++i) {
32+
all.add(Isolate.run(() => enterBarrier(threadBarrierCount)));
33+
}
34+
await Future.wait(all);
35+
}

0 commit comments

Comments
 (0)