Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
libcrypto_threading.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/app/libcrypto_threading.h"
8 
9 #include <openssl/thread.h>
10 
11 #include <memory>
12 
13 #include "packager/base/logging.h"
14 #include "packager/base/synchronization/lock.h"
15 #include "packager/base/threading/platform_thread.h"
16 
17 namespace shaka {
18 namespace media {
19 
20 namespace {
21 
22 std::unique_ptr<base::Lock[]> global_locks;
23 
24 void LockFunction(int mode, int n, const char* file, int line) {
25  VLOG(2) << "CryptoLock @ " << file << ":" << line;
26  if (mode & CRYPTO_LOCK)
27  global_locks[n].Acquire();
28  else
29  global_locks[n].Release();
30 }
31 
32 void ThreadIdFunction(CRYPTO_THREADID* id) {
33  CRYPTO_THREADID_set_numeric(
34  id, static_cast<unsigned long>(base::PlatformThread::CurrentId()));
35 }
36 
37 } // namespace
38 
39 LibcryptoThreading::LibcryptoThreading() {
40  global_locks.reset(new base::Lock[CRYPTO_num_locks()]);
41  CRYPTO_THREADID_set_callback(ThreadIdFunction);
42  CRYPTO_set_locking_callback(LockFunction);
43 }
44 
45 LibcryptoThreading::~LibcryptoThreading() {
46  CRYPTO_THREADID_set_callback(NULL);
47  CRYPTO_set_locking_callback(NULL);
48  global_locks.reset();
49 }
50 
51 } // namespace media
52 } // namespace shaka