1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
32 /* The last 3 #include files should be in this order */
33 #include "curl_printf.h"
34 #include "curl_memory.h"
37 void Curl_psl_destroy(struct PslCache *pslcache)
41 psl_free((psl_ctx_t *) pslcache->psl);
43 pslcache->dynamic = FALSE;
47 static time_t now_seconds(void)
49 struct curltime now = Curl_now();
54 const psl_ctx_t *Curl_psl_use(struct Curl_easy *easy)
56 struct PslCache *pslcache = easy->psl;
63 Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
65 if(!pslcache->psl || pslcache->expires <= now) {
66 /* Let a chance to other threads to do the job: avoids deadlock. */
67 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
69 /* Update cache: this needs an exclusive lock. */
70 Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SINGLE);
72 /* Recheck in case another thread did the job. */
74 if(!pslcache->psl || pslcache->expires <= now) {
76 time_t expires = TIME_T_MAX;
78 #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000
79 psl = psl_latest(NULL);
80 dynamic = psl != NULL;
81 /* Take care of possible time computation overflow. */
82 expires = now < TIME_T_MAX - PSL_TTL? now + PSL_TTL: TIME_T_MAX;
84 /* Only get the built-in PSL if we do not already have the "latest". */
85 if(!psl && !pslcache->dynamic)
91 Curl_psl_destroy(pslcache);
93 pslcache->dynamic = dynamic;
94 pslcache->expires = expires;
97 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); /* Release exclusive lock. */
98 Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
102 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
106 void Curl_psl_release(struct Curl_easy *easy)
108 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
111 #endif /* USE_LIBPSL */