Skip to content

Introduce Context.WithLock#1018

Open
dizzyd wants to merge 1 commit intoAllenDang:masterfrom
dizzyd:master
Open

Introduce Context.WithLock#1018
dizzyd wants to merge 1 commit intoAllenDang:masterfrom
dizzyd:master

Conversation

@dizzyd
Copy link

@dizzyd dizzyd commented Aug 23, 2025

Summary

  • Introduces Context.WithLock() method to provide thread-safe access to context operations
  • Wraps texture loading and freeing queue operations with proper mutex protection
  • Prevents race conditions when multiple threads enqueue texture operations simultaneously

The texture loading and freeing queues were not thread-safe when accessed from multiple goroutines. This could lead to queue corruption and unpredictable behavior when textures were being loaded or freed concurrently.

Thus, I added a WithLock() method to the GIUContext that safely executes functions while holding the context mutex. This ensures that:

  1. Texture load requests enqueued via EnqueueNewTextureFromRgba() are thread-safe
  2. Texture free requests triggered by finalizers are thread-safe
  3. Queue processing in the render loop is protected from concurrent modifications

Files modified:

  • Context.go: Added WithLock() method for safe mutex-protected operations
  • MasterWindow.go: Wrapped texture queue processing in WithLock() during render
  • Texture.go: Protected queue operations in both EnqueueNewTextureFromRgba() and the texture finalizer

… corrupted while enqueuing from multiple threads
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces thread-safe context operations by adding a WithLock() method to prevent race conditions in texture loading and freeing operations. The change addresses concurrent access issues that could corrupt texture queues when multiple goroutines perform texture operations simultaneously.

  • Added Context.WithLock() method for mutex-protected operations
  • Protected texture queue enqueue operations with proper locking
  • Wrapped texture queue processing in the render loop with mutex protection

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
Context.go Adds WithLock() method for thread-safe context operations
Texture.go Wraps texture loading and freeing queue operations with mutex protection
MasterWindow.go Protects texture queue processing during render with WithLock()

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +217 to +221
func (c *GIUContext) WithLock(fn func()) {
c.m.Lock()
defer c.m.Unlock()
fn()
}
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

The WithLock method lacks documentation. Add a comment explaining its purpose for thread-safe execution of functions while holding the context mutex.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

@gucio321 gucio321 left a comment

Choose a reason for hiding this comment

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

@dizzyd Thank you for this PR (and sorry for late review).
Generally, it ok (I like the idea).
The only problem is the linter that you need to fix (just add whatever copilot wrote in its comment as a doc comment on your WithLock function)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants