Skip to content

Run all GTK dialogs on one thread#152

Merged
PolyMeilex merged 1 commit into
PolyMeilex:masterfrom
valadaptive:gtk-cleanup
Sep 28, 2023
Merged

Run all GTK dialogs on one thread#152
PolyMeilex merged 1 commit into
PolyMeilex:masterfrom
valadaptive:gtk-cleanup

Conversation

@valadaptive
Copy link
Copy Markdown
Contributor

When investigating #150 more deeply, I noticed that the GTK documentation seems to indicate that all GTK functions must only be called on the thread that called gtk_init(). The existing code does ensure that only one thread accesses GTK at a time, but the documentation seems to suggest that once a thread calls gtk_init, all calls to GTK must go through that thread:

"GTK+, however, is not thread safe. You should only use GTK+ and GDK from the thread gtk_init() and gtk_main() were called on. This is usually referred to as the "main thread"."

To facilitate that, this PR changes the GTK dialog code to spin up a thread and initialize GTK on it, keeping it alive for the lifetime of the program. It's a bit unfortunate that we have to keep it around forever, but the GTK documentation seems to indicate that once you call gtk_init on a thread, you're stuck with that thread.

Per the GDK documentation:
"GTK+, however, is not thread safe. You should only use GTK+ and GDK
from the thread gtk_init() and gtk_main() were called on. This is
usually referred to as the "main thread"."

We were previously calling GTK functions from different threads--there
was a mutex so that only one thread was calling GTK at any given time,
but that's not sufficient--only the thread *that called gtk_init* is
allowed to call GTK functions. To facilitate that, spin up a thread and
initialize GTK on it, keeping it alive for the lifetime of the program.
It's a bit unfortunate that we have to keep it around forever, but the
GTK documentation seems to indicate that once you call gtk_init on a
thread, you're stuck with that thread.
Copy link
Copy Markdown
Owner

@PolyMeilex PolyMeilex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a decent solution, not much else we can do about this global state bs on C side.

This will obviously blow up as soon as someone has other code that also uses GTK, but let's ignore that for now, as I want to get rid of GTK backend one day anyway (#66).

@PolyMeilex PolyMeilex added the no changelog Ignore changelog CI check label Sep 28, 2023
@PolyMeilex PolyMeilex merged commit 4ea34bc into PolyMeilex:master Sep 28, 2023
notriddle added a commit to notriddle/plugins-workspace that referenced this pull request Dec 5, 2023
PolyMeilex/rfd#152
conflicts with the way tauri uses it.

> Yeah, this is a decent solution, not much else we can do about this global state bs on C side.
>
> This will obviously blow up as soon as someone has other code that also uses GTK, but let's ignore that for now, as I want to get rid of GTK backend one day anyway (tauri-apps#66).

Yes, Tauri is other code that also uses GTK, and it does, indeed, blow up. The best way around this is to stop using rfd entirely (they're planning to drop the GTK backend, which will probably break since the XDG Portal API doesn't cover message dialogs).
notriddle added a commit to notriddle/plugins-workspace that referenced this pull request Dec 5, 2023
PolyMeilex/rfd#152
conflicts with the way tauri uses it.

> Yeah, this is a decent solution, not much else we can do about this global state bs on C side.
>
> This will obviously blow up as soon as someone has other code that also uses GTK, but let's ignore that for now, as I want to get rid of GTK backend one day anyway (tauri-apps#66).

Yes, Tauri is other code that also uses GTK, and it does, indeed, blow up. The best way around this is to stop using rfd entirely (they're planning to drop the GTK backend, which will probably break since the XDG Portal API doesn't cover message dialogs).
notriddle added a commit to notriddle/plugins-workspace that referenced this pull request Dec 5, 2023
PolyMeilex/rfd#152
conflicts with the way tauri uses it.

> Yeah, this is a decent solution, not much else we can do about this global state bs on C side.
>
> This will obviously blow up as soon as someone has other code that also uses GTK, but let's ignore that for now, as I want to get rid of GTK backend one day anyway (tauri-apps#66).

Yes, Tauri is other code that also uses GTK, and it does, indeed, blow up. The best way around this is to stop using rfd entirely (they're planning to drop the GTK backend, which will probably break since the XDG Portal API doesn't cover message dialogs).
FabianLars added a commit to tauri-apps/plugins-workspace that referenced this pull request Dec 5, 2023
* Do not use rfd 0.12.1

PolyMeilex/rfd#152
conflicts with the way tauri uses it.

> Yeah, this is a decent solution, not much else we can do about this global state bs on C side.
>
> This will obviously blow up as soon as someone has other code that also uses GTK, but let's ignore that for now, as I want to get rid of GTK backend one day anyway (#66).

Yes, Tauri is other code that also uses GTK, and it does, indeed, blow up. The best way around this is to stop using rfd entirely (they're planning to drop the GTK backend, which will probably break since the XDG Portal API doesn't cover message dialogs).

* Update dialog-pin-rfd.md

---------

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
tauri-bot pushed a commit to tauri-apps/tauri-plugin-dialog that referenced this pull request Dec 5, 2023
* Do not use rfd 0.12.1

PolyMeilex/rfd#152
conflicts with the way tauri uses it.

> Yeah, this is a decent solution, not much else we can do about this global state bs on C side.
>
> This will obviously blow up as soon as someone has other code that also uses GTK, but let's ignore that for now, as I want to get rid of GTK backend one day anyway (#66).

Yes, Tauri is other code that also uses GTK, and it does, indeed, blow up. The best way around this is to stop using rfd entirely (they're planning to drop the GTK backend, which will probably break since the XDG Portal API doesn't cover message dialogs).

* Update dialog-pin-rfd.md

---------

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>

Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/7105035118

Co-authored-by: FabianLars <FabianLars@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no changelog Ignore changelog CI check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants