88#include "threadstate.h"
99#include "runtime.h"
1010
11- static void threadstate_dispose (git_threadstate * threadstate );
12-
1311/**
1412 * Handle the thread-local state
1513 *
1614 * `git_threadstate_global_init` will be called as part
1715 * of `git_libgit2_init` (which itself must be called
1816 * before calling any other function in the library).
1917 *
20- * This function allocates a TLS index (using pthreads
21- * or fiber-local storage in Win32) to store the per-
18+ * This function allocates a TLS index to store the per-
2219 * thread state.
2320 *
2421 * Any internal method that requires thread-local state
@@ -30,77 +27,41 @@ static void threadstate_dispose(git_threadstate *threadstate);
3027 * (`git_threadstate_global_shutdown`) which will free the
3128 * TLS index. This shutdown handler will be called by
3229 * `git_libgit2_shutdown`.
33- *
34- * If libgit2 is built without threading support, the
35- * `git_threadstate_get()` call returns a pointer to a single,
36- * statically allocated global state. The `git_thread_`
37- * functions are not available in that case.
3830 */
3931
40- #if defined( GIT_THREADS ) && defined( GIT_WIN32 )
32+ static git_tlsdata_key tls_key ;
4133
42- static DWORD fls_index ;
43-
44- static void git_threadstate_global_shutdown (void )
34+ static void threadstate_dispose (git_threadstate * threadstate )
4535{
46- FlsFree (fls_index );
36+ if (!threadstate )
37+ return ;
38+
39+ git__free (threadstate -> error_t .message );
40+ threadstate -> error_t .message = NULL ;
4741}
4842
49- static void WINAPI fls_free (void * threadstate )
43+ static void GIT_SYSTEM_CALL threadstate_free (void * threadstate )
5044{
5145 threadstate_dispose (threadstate );
5246 git__free (threadstate );
5347}
5448
55- int git_threadstate_global_init (void )
56- {
57- if ((fls_index = FlsAlloc (fls_free )) == FLS_OUT_OF_INDEXES )
58- return -1 ;
59-
60- return git_runtime_shutdown_register (git_threadstate_global_shutdown );
61- }
62-
63- git_threadstate * git_threadstate_get (void )
64- {
65- git_threadstate * threadstate ;
66-
67- if ((threadstate = FlsGetValue (fls_index )) != NULL )
68- return threadstate ;
69-
70- if ((threadstate = git__calloc (1 , sizeof (git_threadstate ))) == NULL ||
71- git_buf_init (& threadstate -> error_buf , 0 ) < 0 )
72- return NULL ;
73-
74- FlsSetValue (fls_index , threadstate );
75- return threadstate ;
76- }
77-
78- #elif defined(GIT_THREADS ) && defined(_POSIX_THREADS )
79-
80- static pthread_key_t tls_key ;
81-
8249static void git_threadstate_global_shutdown (void )
8350{
8451 git_threadstate * threadstate ;
8552
86- threadstate = pthread_getspecific (tls_key );
87- pthread_setspecific (tls_key , NULL );
53+ threadstate = git_tlsdata_get (tls_key );
54+ git_tlsdata_set (tls_key , NULL );
8855
8956 threadstate_dispose (threadstate );
9057 git__free (threadstate );
9158
92- pthread_key_delete (tls_key );
93- }
94-
95- static void tls_free (void * threadstate )
96- {
97- threadstate_dispose (threadstate );
98- git__free (threadstate );
59+ git_tlsdata_dispose (tls_key );
9960}
10061
10162int git_threadstate_global_init (void )
10263{
103- if (pthread_key_create (& tls_key , & tls_free ) != 0 )
64+ if (git_tlsdata_init (& tls_key , & threadstate_free ) != 0 )
10465 return -1 ;
10566
10667 return git_runtime_shutdown_register (git_threadstate_global_shutdown );
@@ -110,46 +71,13 @@ git_threadstate *git_threadstate_get(void)
11071{
11172 git_threadstate * threadstate ;
11273
113- if ((threadstate = pthread_getspecific (tls_key )) != NULL )
74+ if ((threadstate = git_tlsdata_get (tls_key )) != NULL )
11475 return threadstate ;
11576
11677 if ((threadstate = git__calloc (1 , sizeof (git_threadstate ))) == NULL ||
11778 git_buf_init (& threadstate -> error_buf , 0 ) < 0 )
11879 return NULL ;
11980
120- pthread_setspecific (tls_key , threadstate );
81+ git_tlsdata_set (tls_key , threadstate );
12182 return threadstate ;
12283}
123-
124- #elif defined(GIT_THREADS )
125- # error unknown threading model
126- #else
127-
128- static git_threadstate threadstate ;
129-
130- static void git_threadstate_global_shutdown (void )
131- {
132- threadstate_dispose (& threadstate );
133- memset (& threadstate , 0 , sizeof (git_threadstate ));
134- }
135-
136- int git_threadstate_global_init (void )
137- {
138- return git_runtime_shutdown_register (git_threadstate_global_shutdown );
139- }
140-
141- git_threadstate * git_threadstate_get (void )
142- {
143- return & threadstate ;
144- }
145-
146- #endif
147-
148- static void threadstate_dispose (git_threadstate * threadstate )
149- {
150- if (!threadstate )
151- return ;
152-
153- git__free (threadstate -> error_t .message );
154- threadstate -> error_t .message = NULL ;
155- }
0 commit comments