diff --git a/packager/app/libcrypto_threading.cc b/packager/app/libcrypto_threading.cc index 46c4e62828..174bf95f09 100644 --- a/packager/app/libcrypto_threading.cc +++ b/packager/app/libcrypto_threading.cc @@ -6,17 +6,47 @@ #include "packager/app/libcrypto_threading.h" +#include + +#include + +#include "packager/base/logging.h" +#include "packager/base/memory/scoped_ptr.h" +#include "packager/base/synchronization/lock.h" +#include "packager/base/threading/platform_thread.h" + namespace edash_packager { namespace media { -LibcryptoThreading::LibcryptoThreading() {} +namespace { -LibcryptoThreading::~LibcryptoThreading() { - TerminateLibcryptoThreading(); +scoped_ptr global_locks; + +void LockFunction(int mode, int n, const char* file, int line) { + VLOG(2) << "CryptoLock @ " << file << ":" << line; + if (mode & CRYPTO_LOCK) + global_locks[n].Acquire(); + else + global_locks[n].Release(); } -bool LibcryptoThreading::Initialize() { - return InitLibcryptoThreading(); +void ThreadIdFunction(CRYPTO_THREADID* id) { + CRYPTO_THREADID_set_numeric( + id, static_cast(base::PlatformThread::CurrentId())); +} + +} // namespace + +LibcryptoThreading::LibcryptoThreading() { + global_locks.reset(new base::Lock[CRYPTO_num_locks()]); + CRYPTO_THREADID_set_callback(ThreadIdFunction); + CRYPTO_set_locking_callback(LockFunction); +} + +LibcryptoThreading::~LibcryptoThreading() { + CRYPTO_THREADID_set_callback(NULL); + CRYPTO_set_locking_callback(NULL); + global_locks.reset(); } } // namespace media diff --git a/packager/app/libcrypto_threading.h b/packager/app/libcrypto_threading.h index e672ec3193..73a70702f5 100644 --- a/packager/app/libcrypto_threading.h +++ b/packager/app/libcrypto_threading.h @@ -12,24 +12,12 @@ namespace edash_packager { namespace media { -/// Enable thread safety for OpenSSL libcrypto. -/// @return true if successful, false otherwise. -bool InitLibcryptoThreading(); - -/// Terminate thread safety for OpenSSL libcrypto. -/// @return true if successful, false otherwise. -bool TerminateLibcryptoThreading(); - /// Convenience class which initializes and terminates libcrypto threading. class LibcryptoThreading { public: LibcryptoThreading(); ~LibcryptoThreading(); - /// Enables thread safety for OpenSSL libcrypto. - /// @return true if successful, false otherwise. - bool Initialize(); - private: DISALLOW_COPY_AND_ASSIGN(LibcryptoThreading); }; diff --git a/packager/app/libcrypto_threading_posix.cc b/packager/app/libcrypto_threading_posix.cc deleted file mode 100644 index 6cc188881b..0000000000 --- a/packager/app/libcrypto_threading_posix.cc +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2014 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file or at -// https://developers.google.com/open-source/licenses/bsd - -#include "packager/app/libcrypto_threading.h" - -#include -#include - -#include - -namespace { - -std::vector global_locks; - -void LockFunction(int mode, int n, const char* file, int line) { - if (mode & CRYPTO_LOCK) - pthread_mutex_lock(&global_locks[n]); - else - pthread_mutex_unlock(&global_locks[n]); -} - -unsigned long ThreadIdFunction() { - return static_cast(pthread_self()); -} - -} // anonymous namespace - -namespace edash_packager { -namespace media { - -bool InitLibcryptoThreading() { - int num_global_locks = CRYPTO_num_locks(); - global_locks.resize(num_global_locks); - for (int i = 0; i < num_global_locks; ++i) - pthread_mutex_init(&global_locks[i], NULL); - CRYPTO_set_id_callback(ThreadIdFunction); - CRYPTO_set_locking_callback(LockFunction); - return true; -} - -bool TerminateLibcryptoThreading() { - CRYPTO_set_id_callback(NULL); - CRYPTO_set_locking_callback(NULL); - for (size_t i = 0; i < global_locks.size(); ++i) - pthread_mutex_destroy(&global_locks[i]); - global_locks.clear(); - return true; -} - -} // namespace media -} // namespace edash_packager diff --git a/packager/app/packager_main.cc b/packager/app/packager_main.cc index c91805fdfe..0f76e66f0a 100644 --- a/packager/app/packager_main.cc +++ b/packager/app/packager_main.cc @@ -311,10 +311,6 @@ int PackagerMain(int argc, char** argv) { return kArgumentValidationFailed; edash_packager::media::LibcryptoThreading libcrypto_threading; - if (!libcrypto_threading.Initialize()) { - LOG(ERROR) << "Could not initialize libcrypto threading."; - return kInternalError; - } // TODO(tinskip): Make InsertStreamDescriptor a member of // StreamDescriptorList. StreamDescriptorList stream_descriptors; diff --git a/packager/packager.gyp b/packager/packager.gyp index 4ec1bfe60f..6104bd1037 100644 --- a/packager/packager.gyp +++ b/packager/packager.gyp @@ -43,13 +43,6 @@ 'third_party/gflags/gflags.gyp:gflags', 'third_party/openssl/openssl.gyp:openssl', ], - 'conditions': [ - [ 'os_posix == 1', { - 'sources': [ - 'app/libcrypto_threading_posix.cc', - ] - }], - ], }, { 'target_name': 'mpd_generator',