Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
libgit2/src/global.c at master · RedisOptimal/libgit2 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
RedisOptimal
libgit2
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
libgit2
/
src
/
global.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
319 lines (251 loc) · 6.95 KB
master
Breadcrumbs
libgit2
/
src
/
global.c
Copy path
Top
File metadata and controls
Code
Blame
319 lines (251 loc) · 6.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include
"common.h"
#include
"global.h"
#include
"hash.h"
#include
"sysdir.h"
#include
"git2/threads.h"
#include
"thread-utils.h"
git_mutex
git__mwindow_mutex
;
#define
MAX_SHUTDOWN_CB
8
#ifdef
GIT_SSL
# include
<openssl/ssl.h>
SSL_CTX
*
git__ssl_ctx
;
# ifdef
GIT_THREADS
static
git_mutex
*
openssl_locks
;
# endif
#endif
static
git_global_shutdown_fn
git__shutdown_callbacks
[
MAX_SHUTDOWN_CB
];
static
git_atomic
git__n_shutdown_callbacks
;
static
git_atomic
git__n_inits
;
void
git__on_shutdown
(
git_global_shutdown_fn
callback
)
{
int
count
=
git_atomic_inc
(
&
git__n_shutdown_callbacks
);
assert
(
count
<=
MAX_SHUTDOWN_CB
&&
count
>
0
);
git__shutdown_callbacks
[
count
-
1
]
=
callback
;
}
static
void
git__shutdown
(
void
)
{
int
pos
;
for
(
pos
=
git_atomic_get
(
&
git__n_shutdown_callbacks
);
pos
>
0
;
pos
=
git_atomic_dec
(
&
git__n_shutdown_callbacks
)) {
git_global_shutdown_fn
cb
=
git__swap
(
git__shutdown_callbacks
[
pos
-
1
],
NULL
);
if
(
cb
!=
NULL
)
cb
();
}
}
#if
defined(
GIT_THREADS
)
&&
defined(
GIT_SSL
)
void
openssl_locking_function
(
int
mode
,
int
n
,
const
char
*
file
,
int
line
)
{
int
lock
;
GIT_UNUSED
(
file
);
GIT_UNUSED
(
line
);
lock
=
mode
&
CRYPTO_LOCK
;
if
(
lock
) {
git_mutex_lock
(
&
openssl_locks
[
n
]);
}
else
{
git_mutex_unlock
(
&
openssl_locks
[
n
]);
}
}
#endif
static
void
init_ssl
(
void
)
{
#ifdef
GIT_SSL
SSL_load_error_strings
();
OpenSSL_add_ssl_algorithms
();
git__ssl_ctx
=
SSL_CTX_new
(
SSLv23_method
());
SSL_CTX_set_mode
(
git__ssl_ctx
,
SSL_MODE_AUTO_RETRY
);
SSL_CTX_set_verify
(
git__ssl_ctx
,
SSL_VERIFY_NONE
,
NULL
);
if
(!
SSL_CTX_set_default_verify_paths
(
git__ssl_ctx
)) {
SSL_CTX_free
(
git__ssl_ctx
);
git__ssl_ctx
=
NULL
;
}
# ifdef
GIT_THREADS
{
int
num_locks
,
i
;
num_locks
=
CRYPTO_num_locks
();
openssl_locks
=
git__calloc
(
num_locks
,
sizeof
(
git_mutex
));
if
(
openssl_locks
==
NULL
) {
SSL_CTX_free
(
git__ssl_ctx
);
git__ssl_ctx
=
NULL
;
}
for
(
i
=
0
;
i
<
num_locks
;
i
++
) {
if
(
git_mutex_init
(
&
openssl_locks
[
i
])
!=
0
) {
SSL_CTX_free
(
git__ssl_ctx
);
git__ssl_ctx
=
NULL
;
}
}
CRYPTO_set_locking_callback
(
openssl_locking_function
);
}
# endif
#endif
}
/**
* Handle the global state with TLS
*
* If libgit2 is built with GIT_THREADS enabled,
* the `git_threads_init()` function must be called
* before calling any other function of the library.
*
* This function allocates a TLS index (using pthreads
* or the native Win32 API) to store the global state
* on a per-thread basis.
*
* Any internal method that requires global state will
* then call `git__global_state()` which returns a pointer
* to the global state structure; this pointer is lazily
* allocated on each thread.
*
* Before shutting down the library, the
* `git_threads_shutdown` method must be called to free
* the previously reserved TLS index.
*
* If libgit2 is built without threading support, the
* `git__global_statestate()` call returns a pointer to a single,
* statically allocated global state. The `git_thread_`
* functions are not available in that case.
*/
/*
* `git_threads_init()` allows subsystems to perform global setup,
* which may take place in the global scope. An explicit memory
* fence exists at the exit of `git_threads_init()`. Without this,
* CPU cores are free to reorder cache invalidation of `_tls_init`
* before cache invalidation of the subsystems' newly written global
* state.
*/
#if
defined(
GIT_THREADS
)
&&
defined(
GIT_WIN32
)
static
DWORD
_tls_index
;
static
volatile
LONG
_mutex
=
0
;
static
int
synchronized_threads_init
(
void
)
{
int
error
;
_tls_index
=
TlsAlloc
();
if
(
git_mutex_init
(
&
git__mwindow_mutex
))
return
-1
;
/* Initialize any other subsystems that have global state */
if
((
error
=
git_hash_global_init
()) >=
0
)
error
=
git_sysdir_global_init
();
win32_pthread_initialize
();
return
error
;
}
int
git_threads_init
(
void
)
{
int
error
=
0
;
/* Enter the lock */
while
(
InterlockedCompareExchange
(
&
_mutex
,
1
,
0
)) {
Sleep
(
0
); }
/* Only do work on a 0 -> 1 transition of the refcount */
if
(
1
==
git_atomic_inc
(
&
git__n_inits
))
error
=
synchronized_threads_init
();
/* Exit the lock */
InterlockedExchange
(
&
_mutex
,
0
);
return
error
;
}
static
void
synchronized_threads_shutdown
(
void
)
{
/* Shut down any subsystems that have global state */
git__shutdown
();
TlsFree
(
_tls_index
);
git_mutex_free
(
&
git__mwindow_mutex
);
}
void
git_threads_shutdown
(
void
)
{
/* Enter the lock */
while
(
InterlockedCompareExchange
(
&
_mutex
,
1
,
0
)) {
Sleep
(
0
); }
/* Only do work on a 1 -> 0 transition of the refcount */
if
(
0
==
git_atomic_dec
(
&
git__n_inits
))
synchronized_threads_shutdown
();
/* Exit the lock */
InterlockedExchange
(
&
_mutex
,
0
);
}
git_global_st
*
git__global_state
(
void
)
{
void
*
ptr
;
assert
(
git_atomic_get
(
&
git__n_inits
)
>
0
);
if
((
ptr
=
TlsGetValue
(
_tls_index
))
!=
NULL
)
return
ptr
;
ptr
=
git__malloc
(
sizeof
(
git_global_st
));
if
(!
ptr
)
return
NULL
;
memset
(
ptr
,
0x0
,
sizeof
(
git_global_st
));
TlsSetValue
(
_tls_index
,
ptr
);
return
ptr
;
}
#elif
defined(
GIT_THREADS
)
&&
defined(
_POSIX_THREADS
)
static
pthread_key_t
_tls_key
;
static
pthread_once_t
_once_init
=
PTHREAD_ONCE_INIT
;
int
init_error
=
0
;
static
void
cb__free_status
(
void
*
st
)
{
git__free
(
st
);
}
static
void
init_once
(
void
)
{
if
((
init_error
=
git_mutex_init
(
&
git__mwindow_mutex
))
!=
0
)
return
;
pthread_key_create
(
&
_tls_key
,
&
cb__free_status
);
/* Initialize any other subsystems that have global state */
if
((
init_error
=
git_hash_global_init
()) >=
0
)
init_error
=
git_sysdir_global_init
();
/* OpenSSL needs to be initialized from the main thread */
init_ssl
();
GIT_MEMORY_BARRIER
;
}
int
git_threads_init
(
void
)
{
pthread_once
(
&
_once_init
,
init_once
);
git_atomic_inc
(
&
git__n_inits
);
return
init_error
;
}
void
git_threads_shutdown
(
void
)
{
void
*
ptr
=
NULL
;
pthread_once_t
new_once
=
PTHREAD_ONCE_INIT
;
if
(
git_atomic_dec
(
&
git__n_inits
)
>
0
)
return
;
/* Shut down any subsystems that have global state */
git__shutdown
();
ptr
=
pthread_getspecific
(
_tls_key
);
pthread_setspecific
(
_tls_key
,
NULL
);
git__free
(
ptr
);
pthread_key_delete
(
_tls_key
);
git_mutex_free
(
&
git__mwindow_mutex
);
_once_init
=
new_once
;
}
git_global_st
*
git__global_state
(
void
)
{
void
*
ptr
;
assert
(
git_atomic_get
(
&
git__n_inits
)
>
0
);
if
((
ptr
=
pthread_getspecific
(
_tls_key
))
!=
NULL
)
return
ptr
;
ptr
=
git__malloc
(
sizeof
(
git_global_st
));
if
(!
ptr
)
return
NULL
;
memset
(
ptr
,
0x0
,
sizeof
(
git_global_st
));
pthread_setspecific
(
_tls_key
,
ptr
);
return
ptr
;
}
#else
static
git_global_st
__state
;
int
git_threads_init
(
void
)
{
static
int
ssl_inited
=
0
;
if
(!
ssl_inited
) {
init_ssl
();
ssl_inited
=
1
;
}
git_atomic_inc
(
&
git__n_inits
);
return
0
;
}
void
git_threads_shutdown
(
void
)
{
/* Shut down any subsystems that have global state */
if
(
0
==
git_atomic_dec
(
&
git__n_inits
))
git__shutdown
();
}
git_global_st
*
git__global_state
(
void
)
{
return
&
__state
;
}
#endif
/* GIT_THREADS */
You can’t perform that action at this time.