Skip to content

Instantly share code, notes, and snippets.

@nothings
Last active February 1, 2020 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nothings/cf2b268ba06567e538dfbcdac5cb6272 to your computer and use it in GitHub Desktop.
Save nothings/cf2b268ba06567e538dfbcdac5cb6272 to your computer and use it in GitHub Desktop.
// Old code which could only have one value for all threads:
static int stbi__vertically_flip_on_load = 0;
STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
{
stbi__vertically_flip_on_load = flag_true_if_should_flip;
}
// New code which allows per-thread values without breaking old behavior:
static int stbi__vertically_flip_on_load_global = 0;
STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
{
stbi__vertically_flip_on_load_global = flag_true_if_should_flip;
}
#ifndef STBI_NO_THREAD_LOCALS
#if __cplusplus >= 201103L
#define STBI_THREAD_LOCAL thread_local
#elif __STDC_VERSION_ >= 201112L
#define STBI_THREAD_LOCAL _Thread_local
#elif defined(__GNUC__)
#define STBI_THREAD_LOCAL __thread
#elif _MSC_VER
#define STBI_THREAD_LOCAL __declspec(thread)
#endif
#endif
#ifndef STBI_THREAD_LOCAL
#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global
#else
static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;
STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)
{
stbi__vertically_flip_on_load_local = flag_true_if_should_flip;
stbi__vertically_flip_on_load_set = 1;
}
#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \
? stbi__vertically_flip_on_load_local \
: stbi__vertically_flip_on_load_global)
#endif // STBI_THREAD_LOCAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment