diff options
author | Daniel Stenberg <daniel@haxx.se> | 2020-03-15 23:58:10 +0100 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2020-06-04 10:54:05 +0200 |
commit | b40c0d0dbc29e74103836d34ceebd4dac47d0798 (patch) | |
tree | 865a865f3a50a803f431251b0c447a18152d9264 /lib/easy.c | |
parent | c048dd0b7c83f9db08d7ad85522b629458e35dd6 (diff) | |
download | curl-bagder/atomic-init.tar.gz |
global_init: atomic initialize counterbagder/atomic-init
With the use a Windows specific function and _Atomic for the rest.
_Atomic is a C11 feature that when available makes the init reference
counter atomic and thread-safe.
init: call the init functions independent of counter
cleanup: decrements the counter and will only do the cleanups when the
counter reaches zero
Closes #5017
Diffstat (limited to 'lib/easy.c')
-rw-r--r-- | lib/easy.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/easy.c b/lib/easy.c index f58c4521a..9f786a9a8 100644 --- a/lib/easy.c +++ b/lib/easy.c @@ -84,8 +84,13 @@ #include "curl_memory.h" #include "memdebug.h" +/* _Atomic is a C11 feature */ +#ifndef HAVE_ATOMIC +#define _Atomic +#endif + /* true globals -- for curl_global_init() and curl_global_cleanup() */ -static unsigned int initialized; +static _Atomic unsigned int initialized; static long init_flags; /* @@ -140,8 +145,7 @@ curl_calloc_callback Curl_ccalloc; */ static CURLcode global_init(long flags, bool memoryfuncs) { - if(initialized++) - return CURLE_OK; + initialized++; if(memoryfuncs) { /* Setup the default memory functions here (again) */ @@ -258,8 +262,13 @@ void curl_global_cleanup(void) if(!initialized) return; +#ifdef WIN32 + if(InterlockedDecrement((LPLONG)&initialized)) + return; +#else if(--initialized) return; +#endif Curl_ssl_cleanup(); Curl_resolver_global_cleanup(); |