diff options
Diffstat (limited to 'xbmc/utils/CryptThreading.cpp')
| -rw-r--r-- | xbmc/utils/CryptThreading.cpp | 84 |
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 | |||
| 17 | namespace | ||
| 18 | { | ||
| 19 | |||
| 20 | CCriticalSection* getlock(int index) | ||
| 21 | { | ||
| 22 | return g_cryptThreadingInitializer.GetLock(index); | ||
| 23 | } | ||
| 24 | |||
| 25 | void 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 | |||
| 33 | unsigned 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 | |||
| 43 | void 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 | |||
| 51 | CryptThreadingInitializer::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 | |||
| 60 | CryptThreadingInitializer::~CryptThreadingInitializer() | ||
| 61 | { | ||
| 62 | CSingleLock l(m_locksLock); | ||
| 63 | CRYPTO_set_locking_callback(nullptr); | ||
| 64 | m_locks.clear(); | ||
| 65 | } | ||
| 66 | |||
| 67 | CCriticalSection* 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 | |||
| 79 | unsigned long CryptThreadingInitializer::GetCurrentCryptThreadId() | ||
| 80 | { | ||
| 81 | return GetCryptThreadId(); | ||
| 82 | } | ||
| 83 | |||
| 84 | #endif | ||
