diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index a8b9d167122..76438a96c9f 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -7,7 +7,6 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); apiClient_t *apiClient_create() { - curl_global_init(CURL_GLOBAL_ALL); apiClient_t *apiClient = malloc(sizeof(apiClient_t)); apiClient->basePath = strdup("{{{basePath}}}"); apiClient->sslConfig = NULL; @@ -43,7 +42,6 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath {{/authMethods}} {{/hasAuthMethods}} ) { - curl_global_init(CURL_GLOBAL_ALL); apiClient_t *apiClient = malloc(sizeof(apiClient_t)); if(basePath){ apiClient->basePath = strdup(basePath); @@ -128,7 +126,6 @@ void apiClient_free(apiClient_t *apiClient) { {{/authMethods}} {{/hasAuthMethods}} free(apiClient); - curl_global_cleanup(); } sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify) { @@ -620,3 +617,10 @@ char *strReplace(char *orig, char *rep, char *with) { return result; } +void apiClient_setupGlobalEnv() { + curl_global_init(CURL_GLOBAL_ALL); +} + +void apiClient_unsetupGlobalEnv() { + curl_global_cleanup(); +} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache index 7e443202137..ac17a3253b9 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache @@ -64,4 +64,16 @@ void sslConfig_free(sslConfig_t *sslConfig); char *strReplace(char *orig, char *rep, char *with); +/* + * In single thread program, the function apiClient_setupGlobalEnv is not needed. + * But in multi-thread program, apiClient_setupGlobalEnv must be called before any worker thread is created + */ +void apiClient_setupGlobalEnv(); + +/* + * This function apiClient_unsetupGlobalEnv must be called whether single or multiple program. + * In multi-thread program, it is must be called after all worker threads end. + */ +void apiClient_unsetupGlobalEnv(); + #endif // INCLUDE_API_CLIENT_H diff --git a/samples/client/petstore/c/include/apiClient.h b/samples/client/petstore/c/include/apiClient.h index e1f97c960ab..595e912e092 100644 --- a/samples/client/petstore/c/include/apiClient.h +++ b/samples/client/petstore/c/include/apiClient.h @@ -46,4 +46,16 @@ void sslConfig_free(sslConfig_t *sslConfig); char *strReplace(char *orig, char *rep, char *with); +/* + * In single thread program, the function apiClient_setupGlobalEnv is not needed. + * But in multi-thread program, apiClient_setupGlobalEnv must be called before any worker thread is created + */ +void apiClient_setupGlobalEnv(); + +/* + * This function apiClient_unsetupGlobalEnv must be called whether single or multiple program. + * In multi-thread program, it is must be called after all worker threads end. + */ +void apiClient_unsetupGlobalEnv(); + #endif // INCLUDE_API_CLIENT_H diff --git a/samples/client/petstore/c/src/apiClient.c b/samples/client/petstore/c/src/apiClient.c index d67690cc83f..f757bcf4d7d 100644 --- a/samples/client/petstore/c/src/apiClient.c +++ b/samples/client/petstore/c/src/apiClient.c @@ -7,7 +7,6 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); apiClient_t *apiClient_create() { - curl_global_init(CURL_GLOBAL_ALL); apiClient_t *apiClient = malloc(sizeof(apiClient_t)); apiClient->basePath = strdup("http://petstore.swagger.io/v2"); apiClient->sslConfig = NULL; @@ -25,7 +24,6 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath , sslConfig_t *sslConfig , list_t *apiKeys_api_key ) { - curl_global_init(CURL_GLOBAL_ALL); apiClient_t *apiClient = malloc(sizeof(apiClient_t)); if(basePath){ apiClient->basePath = strdup(basePath); @@ -82,7 +80,6 @@ void apiClient_free(apiClient_t *apiClient) { free(apiClient->accessToken); } free(apiClient); - curl_global_cleanup(); } sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify) { @@ -526,3 +523,10 @@ char *strReplace(char *orig, char *rep, char *with) { return result; } +void apiClient_setupGlobalEnv() { + curl_global_init(CURL_GLOBAL_ALL); +} + +void apiClient_unsetupGlobalEnv() { + curl_global_cleanup(); +} diff --git a/samples/client/petstore/c/unit-tests/manual-PetAPI.c b/samples/client/petstore/c/unit-tests/manual-PetAPI.c index 657a4fe3879..f82c5353891 100644 --- a/samples/client/petstore/c/unit-tests/manual-PetAPI.c +++ b/samples/client/petstore/c/unit-tests/manual-PetAPI.c @@ -139,4 +139,6 @@ int main() { fclose(file); } apiClient_free(apiClient3); + + apiClient_unsetupGlobalEnv(); } diff --git a/samples/client/petstore/c/unit-tests/manual-StoreAPI.c b/samples/client/petstore/c/unit-tests/manual-StoreAPI.c index 485cf384bbd..ce29f77b961 100644 --- a/samples/client/petstore/c/unit-tests/manual-StoreAPI.c +++ b/samples/client/petstore/c/unit-tests/manual-StoreAPI.c @@ -100,4 +100,6 @@ int main() { } list_free(elementToReturn); apiClient_free(apiClient5); + + apiClient_unsetupGlobalEnv(); } diff --git a/samples/client/petstore/c/unit-tests/manual-UserAPI.c b/samples/client/petstore/c/unit-tests/manual-UserAPI.c index 56c7eac5ee1..8f91839d0f4 100644 --- a/samples/client/petstore/c/unit-tests/manual-UserAPI.c +++ b/samples/client/petstore/c/unit-tests/manual-UserAPI.c @@ -120,4 +120,6 @@ int main() { UserAPI_deleteUser(apiClient5, "example123"); apiClient_free(apiClient5); + + apiClient_unsetupGlobalEnv(); }