DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
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 <vector>
12 
13 #include "packager/base/logging.h"
14 #include "packager/base/memory/scoped_ptr.h"
15 #include "packager/base/synchronization/lock.h"
16 #include "packager/base/threading/platform_thread.h"
17 
18 namespace edash_packager {
19 namespace media {
20 
21 namespace {
22 
23 scoped_ptr<base::Lock[]> global_locks;
24 
25 void LockFunction(int mode, int n, const char* file, int line) {
26  VLOG(2) << "CryptoLock @ " << file << ":" << line;
27  if (mode & CRYPTO_LOCK)
28  global_locks[n].Acquire();
29  else
30  global_locks[n].Release();
31 }
32 
33 void ThreadIdFunction(CRYPTO_THREADID* id) {
34  CRYPTO_THREADID_set_numeric(
35  id, static_cast<unsigned long>(base::PlatformThread::CurrentId()));
36 }
37 
38 } // namespace
39 
40 LibcryptoThreading::LibcryptoThreading() {
41  global_locks.reset(new base::Lock[CRYPTO_num_locks()]);
42  CRYPTO_THREADID_set_callback(ThreadIdFunction);
43  CRYPTO_set_locking_callback(LockFunction);
44 }
45 
46 LibcryptoThreading::~LibcryptoThreading() {
47  CRYPTO_THREADID_set_callback(NULL);
48  CRYPTO_set_locking_callback(NULL);
49  global_locks.reset();
50 }
51 
52 } // namespace media
53 } // namespace edash_packager