[maint] Reworked C++03 solution for thread-local error#3315
Open
ethouris wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3311
The change:
static thread_localinside theAccessThreadLocalErrorfunction. This function then guarantees always possible access to the object for setting and obtaining.CThreadLocalwrapper to ensure unique and per-thread object initialization:CThreadLocal-wrapped object declared as local static (initialized at the first call ofAccessThreadLocalError) bypthread_key_createAccessThreadLocalErrorusesCThreadLocal::getto get pointer to the object. The object is lazy-initialized. There exists a possibility that the allocation will fail and the pointer remains NULL. The successfully allocated object is saved bypthread_setspecificunder the globally-accessible key so that further calls obtain it bypthread_getspecific.SetThreadLocalErrorgets first access to the object byget; if this fails, setting is silently ignored.GetThreadLocalErrorgets first access to the object byget; if this fails, the fallback error object (this time as simply global static object) is returned, preconfigured as memory allocation errorClearThreadLocalErrorsets the error as success, again, only ifgetreturns the valid object and it's ignored otherwiseCThreadLocaldeletes also the object that is assigned in the current thread; it is believed that this will only happen in the same thread that will exit as the last one, so it's a safety precaution to not access it too late.NOTE: A small change in the API was also necessary: the
CUDT::getLastErrornow returns the error as constant reference. This shouldn't matter provided that this is an unofficial C++ API only and for the official C API this doesn't change anything. This is a safety precaution, which although the allocation error possible in the POSIX version is highly unlikely, the safety precaution in case when the fallback memory allocation resident error is returned as a fallback, it is not allowed to be modified.