stb_image: use thread-locals for vertically_flip flag and g_failure_reason
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent 7cce4c3 · Browse files · 1 file·+37 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+37 added−4 removed
- other:+37 added−4 removed
Provenance
- Commit
eb48fbd- Tree
3518f74- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent
7cce4c3
1 file changed+37 added−4 removed
modified:stb_image.h+37 added-4 removed
464 unmodified lines465 STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);466 467 // flip the image vertically, so the first pixel in the output array is the bottom left468 STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);469+// as above, but only applies to images loaded on the thread that calls the function470+// this function is only available if your compiler supports thread-local variables471+STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);472 473 // ZLIB client - used by PNG, available for other purposes474 475 STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);89 unmodified lines565 #else566 #define stbi_inline __forceinline567 #endif568 569+#ifndef STBI_NO_THREAD_LOCALS570+ #if __cplusplus >= 201103L571+ #define STBI_THREAD_LOCAL thread_local572+ #elif __STDC_VERSION_ >= 201112L573+ #define STBI_THREAD_LOCAL _Thread_local574+ #elif defined(__GNUC__)575+ #define STBI_THREAD_LOCAL __thread576+ #elif _MSC_VER577+ #define STBI_THREAD_LOCAL __declspec(thread)578+#endif579+#endif580 581 #ifdef _MSC_VER582 typedef unsigned short stbi__uint16;583 typedef signed short stbi__int16;302 unmodified lines886 static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);887 static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);888 #endif889 876-// this is not threadsafe877-static const char *stbi__g_failure_reason;890+static 891+#ifdef STBI_THREAD_LOCAL892+STBI_THREAD_LOCAL893+#endif894+const char *stbi__g_failure_reason;895 896 STBIDEF const char *stbi_failure_reason(void)897 {898 return stbi__g_failure_reason;112 unmodified lines1011 #ifndef STBI_NO_HDR1012 static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);1013 #endif1014 998-static int stbi__vertically_flip_on_load = 0;1015+static int stbi__vertically_flip_on_load_global = 0;1016 1017 STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)1018 {1002- stbi__vertically_flip_on_load = flag_true_if_should_flip;1019+ stbi__vertically_flip_on_load_global = flag_true_if_should_flip;1020 }1021 1022+#ifndef STBI_THREAD_LOCAL1023+#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global1024+#else1025+static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;1026+1027+STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)1028+{1029+ stbi__vertically_flip_on_load_local = flag_true_if_should_flip;1030+ stbi__vertically_flip_on_load_set = 1;1031+}1032+1033+#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \1034+ ? stbi__vertically_flip_on_load_local \1035+ : stbi__vertically_flip_on_load_global)1036+#endif // STBI_THREAD_LOCAL1037+1038 static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)1039 {1040 memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields1041 ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed6557 unmodified lines