Cache Handle.koid(). (flutter/engine#26506)

This commit is contained in:
Darren Chan
2021-06-02 13:09:02 -04:00
committed by GitHub
parent e530f6406c
commit 8dc11f638c
3 changed files with 27 additions and 4 deletions

View File

@@ -60,6 +60,7 @@ zx_handle_t Handle::ReleaseHandle() {
zx_handle_t handle = handle_;
handle_ = ZX_HANDLE_INVALID;
cached_koid_ = std::nullopt;
while (waiters_.size()) {
// HandleWaiter::Cancel calls Handle::ReleaseWaiter which removes the
// HandleWaiter from waiters_.

View File

@@ -7,6 +7,7 @@
#include <zircon/syscalls.h>
#include <optional>
#include <vector>
#include "flutter/fml/memory/ref_counted.h"
@@ -52,10 +53,14 @@ class Handle : public fml::RefCountedThreadSafe<Handle>,
zx_handle_t handle() const { return handle_; }
zx_koid_t koid() const {
zx_info_handle_basic_t info;
zx_status_t status = zx_object_get_info(
handle_, ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr, nullptr);
return status == ZX_OK ? info.koid : ZX_KOID_INVALID;
if (!cached_koid_.has_value()) {
zx_info_handle_basic_t info;
zx_status_t status = zx_object_get_info(
handle_, ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr, nullptr);
cached_koid_ = std::optional<zx_koid_t>(
status == ZX_OK ? info.koid : ZX_KOID_INVALID);
}
return cached_koid_.value();
}
zx_status_t Close();
@@ -90,6 +95,11 @@ class Handle : public fml::RefCountedThreadSafe<Handle>,
tonic::DartPersistentValue closure_string_;
tonic::DartPersistentValue on_wait_completer_type_;
tonic::DartPersistentValue schedule_microtask_string_;
// Cache koid. To guarantee proper cache invalidation, only one `Handle`
// should own the `zx_handle_t` at a time and use of syscalls that invalidate
// the handle should use `ReleaseHandle()`.
mutable std::optional<zx_koid_t> cached_koid_;
};
} // namespace dart

View File

@@ -153,4 +153,16 @@ void main() {
expect(readResult.bytesAsUTF8String(), equals('\x00\x00'));
});
});
test('cache koid and invalidate', () {
final HandleResult vmo = System.vmoCreate(0);
expect(vmo.status, equals(ZX.OK));
int originalKoid = vmo.handle.koid;
expect(originalKoid, isNot(equals(ZX.KOID_INVALID)));
// Cached koid should be same value.
expect(originalKoid, equals(vmo.handle.koid));
vmo.handle.close();
// koid should be invalidated.
expect(vmo.handle.koid, equals(ZX.KOID_INVALID));
});
}