Skip to content

Commit

Permalink
Reland "[vm] Expose Dart_{CurrentIsolate,ExitIsolate,EnterIsolate}"
Browse files Browse the repository at this point in the history
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 <dacoharkes@google.com>
  • Loading branch information
mkustermann committed Apr 13, 2023
1 parent cc40e17 commit f9eb97f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
16 changes: 8 additions & 8 deletions runtime/bin/ffi_test/ffi_test_functions_vmspecific.cc
Original file line number Diff line number Diff line change
Expand Up @@ -894,16 +894,11 @@ DART_EXPORT void ThreadPoolTest_BarrierSync(
dart_exit_isolate();
{
std::unique_lock<std::mutex> lock(mutex);

++thread_count;
if (thread_count < num_threads) {
while (thread_count < num_threads) {
cvar.wait(lock);
}
} else {
if (thread_count != num_threads) FATAL("bug");
cvar.notify_all();
while (thread_count < num_threads) {
cvar.wait(lock);
}
cvar.notify_all();
}
dart_enter_isolate(isolate);
}
Expand Down Expand Up @@ -1282,6 +1277,11 @@ DART_EXPORT void SetFfiNativeResolverForTest(Dart_Handle url) {
ENSURE(!Dart_IsError(result));
}

DART_EXPORT void WaitUntilNThreadsEnterBarrier(intptr_t num_threads) {
ThreadPoolTest_BarrierSync(Dart_CurrentIsolate_DL, Dart_EnterIsolate_DL,
Dart_ExitIsolate_DL, num_threads);
}

////////////////////////////////////////////////////////////////////////////////
// Helper for the regression test for b/216834909
////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions runtime/include/dart_api_dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ typedef void (*Dart_NativeMessageHandler_DL)(Dart_Port_DL dest_port_id,
F(Dart_UpdateFinalizableExternalSize, void, \
(Dart_FinalizableHandle object, Dart_Handle strong_ref_to_object, \
intptr_t external_allocation_size)) \
/* Isolates */ \
F(Dart_CurrentIsolate, Dart_Isolate, (void)) \
F(Dart_ExitIsolate, void, (void)) \
F(Dart_EnterIsolate, void, (Dart_Isolate)) \
/* Dart_Port */ \
F(Dart_Post, bool, (Dart_Port_DL port_id, Dart_Handle object)) \
F(Dart_NewSendPort, Dart_Handle, (Dart_Port_DL port_id)) \
Expand Down
2 changes: 1 addition & 1 deletion runtime/include/dart_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// On backwards compatible changes the minor version is increased.
// The versioning covers the symbols exposed in dart_api_dl.h
#define DART_API_DL_MAJOR_VERSION 2
#define DART_API_DL_MINOR_VERSION 2
#define DART_API_DL_MINOR_VERSION 3

#endif /* RUNTIME_INCLUDE_DART_VERSION_H_ */ /* NOLINT */
33 changes: 33 additions & 0 deletions tests/ffi/dl_api_exit_enter_isolate_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2023, 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.

// SharedObjects=ffi_test_functions

import 'dart:ffi';
import 'dart:isolate';

import 'package:expect/expect.dart';

import 'dylib_utils.dart';

final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");

final initializeApi = ffiTestFunctions.lookupFunction<
IntPtr Function(Pointer<Void>),
int Function(Pointer<Void>)>("InitDartApiDL");
final enterBarrier =
ffiTestFunctions.lookupFunction<Void Function(IntPtr), void Function(int)>(
"WaitUntilNThreadsEnterBarrier");

main() async {
const threadBarrierCount = 30;

initializeApi(NativeApi.initializeApiDLData);

final all = <Future>[];
for (int i = 0; i < threadBarrierCount; ++i) {
all.add(Isolate.run(() => enterBarrier(threadBarrierCount)));
}
await Future.wait(all);
}
35 changes: 35 additions & 0 deletions tests/ffi_2/dl_api_exit_enter_isolate_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2023, 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.

// @dart = 2.9

// SharedObjects=ffi_test_functions

import 'dart:ffi';
import 'dart:isolate';

import 'package:expect/expect.dart';

import 'dylib_utils.dart';

final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");

final initializeApi = ffiTestFunctions.lookupFunction<
IntPtr Function(Pointer<Void>),
int Function(Pointer<Void>)>("InitDartApiDL");
final enterBarrier =
ffiTestFunctions.lookupFunction<Void Function(IntPtr), void Function(int)>(
"WaitUntilNThreadsEnterBarrier");

main() async {
const threadBarrierCount = 30;

initializeApi(NativeApi.initializeApiDLData);

final all = <Future>[];
for (int i = 0; i < threadBarrierCount; ++i) {
all.add(Isolate.run(() => enterBarrier(threadBarrierCount)));
}
await Future.wait(all);
}

0 comments on commit f9eb97f

Please sign in to comment.