Skip to content

Commit

Permalink
Merge pull request #1 from n1hility/add-get-threadid
Browse files Browse the repository at this point in the history
Add routine to get message loop thread id
  • Loading branch information
n1hility committed Sep 14, 2023
2 parents 019eda0 + be9688f commit d75df78
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/winquit/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ func NotifyOnQuit(done chan bool) {
func SimulateSigTermOnQuit(handler chan os.Signal) {
simulateSigTermOnQuit(handler)
}

// Returns the thread id of the message loop thread created by winquit, or "0"
// if one is not running. The latter indicates a mistake, as this function
// should only be called after a call to one of the _OnQuit functions.
//
// In most cases this method should not be necessary, as RequestQuit and
// QuitProcess do not require it. It is primarily provided to enable legacy
// patterns that serialize the thread id for later direct signaling.
func GetCurrentMessageLoopThreadId() uint32 {
return getCurrentMessageLoopThreadId()
}
4 changes: 4 additions & 0 deletions pkg/winquit/server_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ func notifyOnQuit(done chan bool) {

func simulateSigTermOnQuit(handler chan os.Signal) {
}

func getCurrentMessageLoopThreadId() uint32 {
return 0
}
9 changes: 8 additions & 1 deletion pkg/winquit/server_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/containers/winquit/pkg/winquit/win32"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)

type receiversType struct {
Expand All @@ -25,6 +26,7 @@ var (
}

loopInit sync.Once
loopTid uint32
)

func (r *receiversType) add(channel baseChannelType) {
Expand Down Expand Up @@ -75,13 +77,18 @@ func simulateSigTermOnQuit(handler chan os.Signal) {
initLoop()
}

func getCurrentMessageLoopThreadId() uint32 {
return loopTid
}

func messageLoop() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

loopTid = windows.GetCurrentThreadId()
registerDummyWindow()

logrus.Info("Entering loop for quit")
logrus.Debug("Entering loop for quit")
for {
ret, msg, err := win32.GetMessage(0, 0, 0)
if err != nil {
Expand Down

0 comments on commit d75df78

Please sign in to comment.