Skip to content

Commit

Permalink
Merge pull request #83 from mappu/miqt-lockosthread
Browse files Browse the repository at this point in the history
Call runtime.LockOSThread() automatically for the Qt main thread
  • Loading branch information
mappu authored Nov 19, 2024
2 parents 30c6cb4 + 281b1a8 commit 47d4581
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Qt class inherited types are projected as a Go embedded struct. For example, to

- When a Qt subclass adds a method overload (e.g. `QMenu::addAction(QString)` vs `QWidget::addAction(QAction*)`), the base class version is shadowed and can only be called via `myQMenu.QWidget.AddAction(QAction*)`.

The Go runtime migrates goroutines between OS threads, but Qt expects fixed OS threads to be used for each QObject. When you first call `qt.NewQApplication` in MIQT, that will be considered the [Qt main thread](https://doc.qt.io/qt-6/thread-basics.html#gui-thread-and-worker-thread) and will automatically signal the Go runtime to bind to a fixed OS thread using `runtime.LockOSThread()`.

Some C++ idioms that were difficult to project were omitted from the binding. But, this can be improved in the future.

### Q6. Can I use Qt Designer and the Qt Resource system?
Expand Down
6 changes: 6 additions & 0 deletions cmd/genbindings/emitgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func (gfs *goFileState) emitParametersGo2CABIForwarding(m CppMethod) (preamble s

tmp = append(tmp, "argc, &argv[0]")

// Additional quirk for QApplication constructor: bind to OS thread
gfs.imports["runtime"] = struct{}{}
preamble += "\n"
preamble += "runtime.LockOSThread() // Prevent Go from migrating the main Qt thread\n"
preamble += "\n"

} else if skipNext {
// Skip this parameter, already handled
skipNext = false
Expand Down
6 changes: 6 additions & 0 deletions qt/gen_qapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func NewQApplication(args []string) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QApplication_new(argc, &argv[0])
return newQApplication(ret)
}
Expand All @@ -74,6 +77,9 @@ func NewQApplication2(args []string, param3 int) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QApplication_new2(argc, &argv[0], (C.int)(param3))
return newQApplication(ret)
}
Expand Down
6 changes: 6 additions & 0 deletions qt/gen_qcoreapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func NewQCoreApplication(args []string) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QCoreApplication_new(argc, &argv[0])
return newQCoreApplication(ret)
}
Expand All @@ -72,6 +75,9 @@ func NewQCoreApplication2(args []string, param3 int) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QCoreApplication_new2(argc, &argv[0], (C.int)(param3))
return newQCoreApplication(ret)
}
Expand Down
6 changes: 6 additions & 0 deletions qt/gen_qguiapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func NewQGuiApplication(args []string) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QGuiApplication_new(argc, &argv[0])
return newQGuiApplication(ret)
}
Expand All @@ -66,6 +69,9 @@ func NewQGuiApplication2(args []string, param3 int) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QGuiApplication_new2(argc, &argv[0], (C.int)(param3))
return newQGuiApplication(ret)
}
Expand Down
6 changes: 6 additions & 0 deletions qt6/gen_qapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func NewQApplication(args []string) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QApplication_new(argc, &argv[0])
return newQApplication(ret)
}
Expand All @@ -66,6 +69,9 @@ func NewQApplication2(args []string, param3 int) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QApplication_new2(argc, &argv[0], (C.int)(param3))
return newQApplication(ret)
}
Expand Down
6 changes: 6 additions & 0 deletions qt6/gen_qcoreapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func NewQCoreApplication(args []string) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QCoreApplication_new(argc, &argv[0])
return newQCoreApplication(ret)
}
Expand All @@ -72,6 +75,9 @@ func NewQCoreApplication2(args []string, param3 int) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QCoreApplication_new2(argc, &argv[0], (C.int)(param3))
return newQCoreApplication(ret)
}
Expand Down
6 changes: 6 additions & 0 deletions qt6/gen_qguiapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func NewQGuiApplication(args []string) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QGuiApplication_new(argc, &argv[0])
return newQGuiApplication(ret)
}
Expand All @@ -66,6 +69,9 @@ func NewQGuiApplication2(args []string, param3 int) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}

runtime.LockOSThread() // Prevent Go from migrating the main Qt thread

ret := C.QGuiApplication_new2(argc, &argv[0], (C.int)(param3))
return newQGuiApplication(ret)
}
Expand Down

0 comments on commit 47d4581

Please sign in to comment.