summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/CryptThreading.cpp
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
committermanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
commitbe933ef2241d79558f91796cc5b3a161f72ebf9c (patch)
treefe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/utils/CryptThreading.cpp
parent5f8335c1e49ce108ef3481863833c98efa00411b (diff)
downloadkodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip
sync with upstream
Diffstat (limited to 'xbmc/utils/CryptThreading.cpp')
-rw-r--r--xbmc/utils/CryptThreading.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/xbmc/utils/CryptThreading.cpp b/xbmc/utils/CryptThreading.cpp
new file mode 100644
index 0000000..3484635
--- /dev/null
+++ b/xbmc/utils/CryptThreading.cpp
@@ -0,0 +1,84 @@
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#include "CryptThreading.h"
10#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
11
12#include "threads/Thread.h"
13#include "utils/log.h"
14
15#include <atomic>
16
17namespace
18{
19
20CCriticalSection* getlock(int index)
21{
22 return g_cryptThreadingInitializer.GetLock(index);
23}
24
25void lock_callback(int mode, int type, const char* file, int line)
26{
27 if (mode & CRYPTO_LOCK)
28 getlock(type)->lock();
29 else
30 getlock(type)->unlock();
31}
32
33unsigned long GetCryptThreadId()
34{
35 static std::atomic<unsigned long> tidSequence{0};
36 static thread_local unsigned long tidTl{0};
37
38 if (tidTl == 0)
39 tidTl = ++tidSequence;
40 return tidTl;
41}
42
43void thread_id(CRYPTO_THREADID* tid)
44{
45 // C-style cast required due to vastly differing native ID return types
46 CRYPTO_THREADID_set_numeric(tid, GetCryptThreadId());
47}
48
49}
50
51CryptThreadingInitializer::CryptThreadingInitializer()
52{
53 // OpenSSL < 1.1 needs integration code to support multi-threading
54 // This is absolutely required for libcurl if it uses the OpenSSL backend
55 m_locks.resize(CRYPTO_num_locks());
56 CRYPTO_THREADID_set_callback(thread_id);
57 CRYPTO_set_locking_callback(lock_callback);
58}
59
60CryptThreadingInitializer::~CryptThreadingInitializer()
61{
62 CSingleLock l(m_locksLock);
63 CRYPTO_set_locking_callback(nullptr);
64 m_locks.clear();
65}
66
67CCriticalSection* CryptThreadingInitializer::GetLock(int index)
68{
69 CSingleLock l(m_locksLock);
70 auto& curlock = m_locks[index];
71 if (!curlock)
72 {
73 curlock.reset(new CCriticalSection());
74 }
75
76 return curlock.get();
77}
78
79unsigned long CryptThreadingInitializer::GetCurrentCryptThreadId()
80{
81 return GetCryptThreadId();
82}
83
84#endif