zed/crates/gpui/src
张小白 728a874b1e
Some checks are pending
CI / Check Postgres and Protobuf migrations, mergability (push) Waiting to run
CI / Check formatting and spelling (push) Waiting to run
CI / (macOS) Run Clippy and tests (push) Waiting to run
CI / (Linux) Run Clippy and tests (push) Waiting to run
CI / (Linux) Build Remote Server (push) Waiting to run
CI / (Windows) Run Clippy and tests (push) Waiting to run
CI / Create a macOS bundle (push) Blocked by required conditions
CI / Linux x86_x64 release bundle (push) Blocked by required conditions
CI / Linux arm64 release bundle (push) Blocked by required conditions
CI / Auto release preview (push) Blocked by required conditions
Deploy Docs / Deploy Docs (push) Waiting to run
Docs / Check formatting (push) Waiting to run
Script / ShellCheck Scripts (push) Waiting to run
windows: Improve foreground task dispatching on Windows (#23283)
Closes #22653

After some investigation, I found this bug is due to that sometimes
`foreground_task` is not dispatched to the main thread unless there is
user input. The current Windows implementation works as follows: when
the `WindowsDispatcher` receives a `foreground_task`, it adds the task
to a queue and uses `SetEvent(dispatch_event)` to notify the main
thread.

The main thread then listens for notifications using
`MsgWaitForMultipleObjects(&[dispatch_event])`.

Essentially, this is a synchronous method, but it is not robust. For
example, if 100 `foreground_task`s are sent, `dispatch_event` should
theoretically be triggered 100 times, and
`MsgWaitForMultipleObjects(&[dispatch_event])` should receive 100
notifications, causing the main thread to execute all 100 tasks.
However, in practice, some `foreground_task`s may not get a chance to
execute due to certain reasons.

As shown in the attached video, when I don't move the mouse, there are
about 20-30 `foreground_task`s waiting in the queue to be executed. When
I move the mouse, `run_foreground_tasks()` is called, which processes
the tasks in the queue.



https://github.com/user-attachments/assets/83cd09ca-4b17-4a1f-9a2a-5d1569b23483



To address this, this PR adopts an approach similar to `winit`. In
`winit`, an invisible window is created for message passing. In this PR,
we use `PostThreadMessage` to directly send messages to the main thread.

With this implementation, when 100 `foreground_task`s are sent, the
`WindowsDispatcher` uses `PostThreadMessageW(thread_id,
RUNNABLE_DISPATCHED)` to notify the main thread. This approach enqueues
100 `RUNNABLE_DISPATCHED` messages in the main thread's message queue,
ensuring that each `foreground_task` is executed as expected. The main
thread continuously processes these messages, guaranteeing that all 100
tasks are executed.

Release Notes:

- N/A
2025-01-18 23:43:56 +08:00
..
app Improve panic report with reentrant SlotMap use (#22667) 2025-01-04 20:37:40 +00:00
elements assistant2: Navigate context strip with keyboard (#23128) 2025-01-14 16:45:11 +00:00
keymap Add initial FreeBSD support (#20480) 2024-11-11 18:39:05 +01:00
platform windows: Improve foreground task dispatching on Windows (#23283) 2025-01-18 23:43:56 +08:00
text_system gpui/perf: Use SharedString on API boundary of line layout (#22566) 2025-01-02 11:06:01 +00:00
window
action.rs Add support for namespace changes in action deprecations (#23086) 2025-01-13 20:56:22 +00:00
app.rs gpui: Clean up AppContext doc comments (#23154) 2025-01-14 23:24:34 +00:00
arena.rs
asset_cache.rs Add Loading and Fallback States to Image Elements (via StyledImage) (#20371) 2024-11-15 19:12:01 -08:00
assets.rs
bounds_tree.rs
color.rs gpui: Add linear gradient support to fill background (#20812) 2024-12-11 21:52:52 +02:00
element.rs Store focus handles in AppContext instead of Window (#22158) 2024-12-17 14:41:00 -07:00
executor.rs Remove Task::get_ready method I added, which is unusable in practice (#22012) 2024-12-14 03:21:41 -07:00
geometry.rs Fix docs for Bounds::from_corner_and_size (#22265) 2024-12-19 21:50:52 +00:00
global.rs Add support for git branches on remote projects (#19755) 2024-10-27 15:50:54 -07:00
gpui.rs Improve keymap json schema (#23044) 2025-01-13 02:34:35 +00:00
input.rs Clip UTF-16 offsets in text for range (#20968) 2024-11-20 22:04:26 -07:00
interactive.rs Dequalify WindowContext and ViewContext references (#22477) 2024-12-28 23:09:55 +00:00
key_dispatch.rs Improve doc comments about keybinding order (#23014) 2025-01-11 22:47:42 +00:00
keymap.rs Improve doc comments about keybinding order (#23014) 2025-01-11 22:47:42 +00:00
platform.rs Add a dedicated action to open files (#22625) 2025-01-08 14:29:15 +00:00
prelude.rs gpui: Don't export named Context from prelude (#21869) 2024-12-11 13:21:40 -05:00
scene.rs gpui: Add linear gradient support to fill background (#20812) 2024-12-11 21:52:52 +02:00
shared_string.rs gpui/perf: Use SharedString on API boundary of line layout (#22566) 2025-01-02 11:06:01 +00:00
shared_uri.rs
style.rs Add Corner to geometry and make names of corner methods consistent (#22119) 2024-12-16 22:57:15 -07:00
styled.rs gpui: Clean up Styled doc comments (#20731) 2024-11-15 11:27:49 -05:00
subscription.rs
svg_renderer.rs Add Loading and Fallback States to Image Elements (via StyledImage) (#20371) 2024-11-15 19:12:01 -08:00
taffy.rs
test.rs deps: Bump smol to 2.0 (#22956) 2025-01-10 13:38:00 +00:00
text_system.rs gpui/perf: Use SharedString on API boundary of line layout (#22566) 2025-01-02 11:06:01 +00:00
util.rs
view.rs Cleanups preparing for WindowContext refactor (#22475) 2024-12-28 21:36:14 +00:00
window.rs Improve doc comments about keybinding order (#23014) 2025-01-11 22:47:42 +00:00