Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Potential issue with error mutex? #2542

Closed
hypatia-of-sva opened this issue Apr 20, 2024 · 2 comments
Closed

Question: Potential issue with error mutex? #2542

hypatia-of-sva opened this issue Apr 20, 2024 · 2 comments
Labels

Comments

@hypatia-of-sva
Copy link

Hello GLFW team,

I was looking through the code of GLFW 3.4 and was a bit confused by the following lines in _glfwInputError in init.c:

            error = _glfw_calloc(1, sizeof(_GLFWerror));
            _glfwPlatformSetTls(&_glfw.errorSlot, error);
            _glfwPlatformLockMutex(&_glfw.errorLock);
            error->next = _glfw.errorListHead;
            _glfw.errorListHead = error;
            _glfwPlatformUnlockMutex(&_glfw.errorLock);

It seems like the value of the at the errorslot is set to the local allocation before the mutex is set. If I understand it correctly, the slot should be locked by the mutex. Was this just a confusion on my part? I would have expected something like:

            error = _glfw_calloc(1, sizeof(_GLFWerror));
            _glfwPlatformLockMutex(&_glfw.errorLock);
            _glfwPlatformSetTls(&_glfw.errorSlot, error);
            error->next = _glfw.errorListHead;
            _glfw.errorListHead = error;
            _glfwPlatformUnlockMutex(&_glfw.errorLock);

instead. If this is intentional, maybe a comment here would also help other people trying to understand and/or refactor the GLFW code for their own needs.

With regards,
Hypatia of Sva.

@combolek
Copy link

combolek commented May 6, 2024

_glfwPlatformSetTls sets a value that is in the thread local storage (that's what Tls stands for). So each thread has its own and no locking is needed.

The code within the mutex section is collecting the _GLFWerror structs from all threads together in a global linked list (so they can be freed later without relying on each thread), so it needs to to be properly synchronized.

@dougbinks
Copy link
Contributor

As @combolek mentions _glfwPlatformSetTls operates on thread local storage and does not need to be synchronized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants