1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, 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 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #include <curl/curl.h>
34 /* The last 3 #include files should be in this order */
35 #include "curl_printf.h"
36 #include "curl_memory.h"
39 void Curl_psl_destroy(struct PslCache *pslcache)
43 psl_free((psl_ctx_t *) pslcache->psl);
45 pslcache->dynamic = FALSE;
49 static time_t now_seconds(void)
51 struct curltime now = Curl_now();
56 const psl_ctx_t *Curl_psl_use(struct Curl_easy *easy)
58 struct PslCache *pslcache = easy->psl;
65 Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
67 if(!pslcache->psl || pslcache->expires <= now) {
68 /* Let a chance to other threads to do the job: avoids deadlock. */
69 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
71 /* Update cache: this needs an exclusive lock. */
72 Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SINGLE);
74 /* Recheck in case another thread did the job. */
76 if(!pslcache->psl || pslcache->expires <= now) {
78 time_t expires = TIME_T_MAX;
80 #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000
81 psl = psl_latest(NULL);
82 dynamic = psl != NULL;
83 /* Take care of possible time computation overflow. */
84 expires = now < TIME_T_MAX - PSL_TTL? now + PSL_TTL: TIME_T_MAX;
86 /* Only get the built-in PSL if we do not already have the "latest". */
87 if(!psl && !pslcache->dynamic)
93 Curl_psl_destroy(pslcache);
95 pslcache->dynamic = dynamic;
96 pslcache->expires = expires;
99 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); /* Release exclusive lock. */
100 Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
104 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
108 void Curl_psl_release(struct Curl_easy *easy)
110 Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
113 #endif /* USE_LIBPSL */