diff --git a/runtime/bin/run_vm_tests.cc b/runtime/bin/run_vm_tests.cc index d560dab733f..a2e2af8a5f0 100644 --- a/runtime/bin/run_vm_tests.cc +++ b/runtime/bin/run_vm_tests.cc @@ -380,6 +380,7 @@ static int Main(int argc, const char** argv) { /*shutdown_isolate=*/nullptr, /*cleanup_isolate=*/nullptr, /*cleanup_group=*/CleanupIsolateGroup, + /*thread_start=*/nullptr, /*thread_exit=*/nullptr, dart::bin::DartUtils::OpenFile, dart::bin::DartUtils::ReadFile, dart::bin::DartUtils::WriteFile, dart::bin::DartUtils::CloseFile, /*entropy_source=*/nullptr, diff --git a/runtime/include/dart_api.h b/runtime/include/dart_api.h index 61c56f67928..360de52663d 100644 --- a/runtime/include/dart_api.h +++ b/runtime/include/dart_api.h @@ -756,6 +756,15 @@ typedef void (*Dart_IsolateCleanupCallback)(void* isolate_group_data, */ typedef void (*Dart_IsolateGroupCleanupCallback)(void* isolate_group_data); +/** + * A thread start callback function. + * This callback, provided by the embedder, is called after a thread in the + * vm thread pool starts. + * This function could be used to adjust thread priority or attach native + * resources to the thread. + */ +typedef void (*Dart_ThreadStartCallback)(void); + /** * A thread death callback function. * This callback, provided by the embedder, is called before a thread in the @@ -840,7 +849,7 @@ typedef Dart_Handle (*Dart_GetVMServiceAssetsArchive)(void); * The current version of the Dart_InitializeFlags. Should be incremented every * time Dart_InitializeFlags changes in a binary incompatible way. */ -#define DART_INITIALIZE_PARAMS_CURRENT_VERSION (0x00000005) +#define DART_INITIALIZE_PARAMS_CURRENT_VERSION (0x00000006) /** Forward declaration */ struct Dart_CodeObserver; @@ -966,6 +975,7 @@ typedef struct { */ Dart_IsolateGroupCleanupCallback cleanup_group; + Dart_ThreadStartCallback thread_start; Dart_ThreadExitCallback thread_exit; Dart_FileOpenCallback file_open; Dart_FileReadCallback file_read; diff --git a/runtime/vm/dart.cc b/runtime/vm/dart.cc index 1f74527683b..2eee4a94a68 100644 --- a/runtime/vm/dart.cc +++ b/runtime/vm/dart.cc @@ -63,6 +63,7 @@ ThreadPool* Dart::thread_pool_ = NULL; DebugInfo* Dart::pprof_symbol_generator_ = NULL; ReadOnlyHandles* Dart::predefined_handles_ = NULL; Snapshot::Kind Dart::vm_snapshot_kind_ = Snapshot::kInvalid; +Dart_ThreadStartCallback Dart::thread_start_callback_ = NULL; Dart_ThreadExitCallback Dart::thread_exit_callback_ = NULL; Dart_FileOpenCallback Dart::file_open_callback_ = NULL; Dart_FileReadCallback Dart::file_read_callback_ = NULL; @@ -254,6 +255,7 @@ char* Dart::DartInit(const uint8_t* vm_isolate_snapshot, Dart_IsolateShutdownCallback shutdown, Dart_IsolateCleanupCallback cleanup, Dart_IsolateGroupCleanupCallback cleanup_group, + Dart_ThreadStartCallback thread_start, Dart_ThreadExitCallback thread_exit, Dart_FileOpenCallback file_open, Dart_FileReadCallback file_read, @@ -296,6 +298,7 @@ char* Dart::DartInit(const uint8_t* vm_isolate_snapshot, UntaggedFrame::Init(); + set_thread_start_callback(thread_start); set_thread_exit_callback(thread_exit); SetFileCallbacks(file_open, file_read, file_write, file_close); set_entropy_source_callback(entropy_source); @@ -532,6 +535,7 @@ char* Dart::Init(const uint8_t* vm_isolate_snapshot, Dart_IsolateShutdownCallback shutdown, Dart_IsolateCleanupCallback cleanup, Dart_IsolateGroupCleanupCallback cleanup_group, + Dart_ThreadStartCallback thread_start, Dart_ThreadExitCallback thread_exit, Dart_FileOpenCallback file_open, Dart_FileReadCallback file_read, @@ -552,9 +556,9 @@ char* Dart::Init(const uint8_t* vm_isolate_snapshot, char* retval = DartInit(vm_isolate_snapshot, instructions_snapshot, create_group, initialize_isolate, shutdown, cleanup, cleanup_group, - thread_exit, file_open, file_read, file_write, file_close, - entropy_source, get_service_assets, start_kernel_isolate, - observer, post_task, post_task_data); + thread_start, thread_exit, file_open, file_read, file_write, + file_close, entropy_source, get_service_assets, + start_kernel_isolate, observer, post_task, post_task_data); if (retval != NULL) { init_state_.ResetInitializing(); return retval; diff --git a/runtime/vm/dart.h b/runtime/vm/dart.h index a8c4a56b28b..5a170c28174 100644 --- a/runtime/vm/dart.h +++ b/runtime/vm/dart.h @@ -36,6 +36,7 @@ class Dart : public AllStatic { Dart_IsolateShutdownCallback shutdown, Dart_IsolateCleanupCallback cleanup, Dart_IsolateGroupCleanupCallback cleanup_group, + Dart_ThreadStartCallback thread_start, Dart_ThreadExitCallback thread_exit, Dart_FileOpenCallback file_open, Dart_FileReadCallback file_read, @@ -119,6 +120,12 @@ class Dart : public AllStatic { Snapshot::Kind kind); static Snapshot::Kind vm_snapshot_kind() { return vm_snapshot_kind_; } + static Dart_ThreadStartCallback thread_start_callback() { + return thread_start_callback_; + } + static void set_thread_start_callback(Dart_ThreadStartCallback cback) { + thread_start_callback_ = cback; + } static Dart_ThreadExitCallback thread_exit_callback() { return thread_exit_callback_; } @@ -176,6 +183,7 @@ class Dart : public AllStatic { Dart_IsolateShutdownCallback shutdown, Dart_IsolateCleanupCallback cleanup, Dart_IsolateGroupCleanupCallback cleanup_group, + Dart_ThreadStartCallback thread_start, Dart_ThreadExitCallback thread_exit, Dart_FileOpenCallback file_open, Dart_FileReadCallback file_read, @@ -199,6 +207,7 @@ class Dart : public AllStatic { static DebugInfo* pprof_symbol_generator_; static ReadOnlyHandles* predefined_handles_; static Snapshot::Kind vm_snapshot_kind_; + static Dart_ThreadStartCallback thread_start_callback_; static Dart_ThreadExitCallback thread_exit_callback_; static Dart_FileOpenCallback file_open_callback_; static Dart_FileReadCallback file_read_callback_; diff --git a/runtime/vm/dart_api_impl.cc b/runtime/vm/dart_api_impl.cc index 7106d5bbbf2..42021bd0fbe 100644 --- a/runtime/vm/dart_api_impl.cc +++ b/runtime/vm/dart_api_impl.cc @@ -1206,14 +1206,15 @@ DART_EXPORT char* Dart_Initialize(Dart_InitializeParams* params) { "Invalid Dart_InitializeParams version."); } - return Dart::Init( - params->vm_snapshot_data, params->vm_snapshot_instructions, - params->create_group, params->initialize_isolate, - params->shutdown_isolate, params->cleanup_isolate, params->cleanup_group, - params->thread_exit, params->file_open, params->file_read, - params->file_write, params->file_close, params->entropy_source, - params->get_service_assets, params->start_kernel_isolate, - params->code_observer, params->post_task, params->post_task_data); + return Dart::Init(params->vm_snapshot_data, params->vm_snapshot_instructions, + params->create_group, params->initialize_isolate, + params->shutdown_isolate, params->cleanup_isolate, + params->cleanup_group, params->thread_start, + params->thread_exit, params->file_open, params->file_read, + params->file_write, params->file_close, + params->entropy_source, params->get_service_assets, + params->start_kernel_isolate, params->code_observer, + params->post_task, params->post_task_data); } DART_EXPORT char* Dart_Cleanup() { diff --git a/runtime/vm/thread_pool.cc b/runtime/vm/thread_pool.cc index f45055574d1..ac6adfc8808 100644 --- a/runtime/vm/thread_pool.cc +++ b/runtime/vm/thread_pool.cc @@ -317,6 +317,13 @@ void ThreadPool::Worker::StartThread() { } void ThreadPool::Worker::Main(uword args) { + // Call the thread start hook here to notify the embedder that the + // thread pool thread has started. + Dart_ThreadStartCallback start_cb = Dart::thread_start_callback(); + if (start_cb != nullptr) { + start_cb(); + } + OSThread* os_thread = OSThread::Current(); ASSERT(os_thread != nullptr); @@ -343,8 +350,9 @@ void ThreadPool::Worker::Main(uword args) { // Call the thread exit hook here to notify the embedder that the // thread pool thread is exiting. - if (Dart::thread_exit_callback() != NULL) { - (*Dart::thread_exit_callback())(); + Dart_ThreadExitCallback exit_cb = Dart::thread_exit_callback(); + if (exit_cb != nullptr) { + exit_cb(); } }