Make LibcryptoThreading cross-platform

Change-Id: Id559c9b4d61f6d5d419735becc968fff3aa7f866
This commit is contained in:
KongQun Yang 2015-03-20 18:37:12 -07:00 committed by Gerrit Code Review
parent e3d18b684c
commit d3c3a5573b
5 changed files with 35 additions and 82 deletions

View File

@ -6,17 +6,47 @@
#include "packager/app/libcrypto_threading.h"
#include <openssl/crypto.h>
#include <vector>
#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<base::Lock[]> 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<unsigned long>(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

View File

@ -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);
};

View File

@ -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 <openssl/crypto.h>
#include <pthread.h>
#include <vector>
namespace {
std::vector<pthread_mutex_t> 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<unsigned long>(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

View File

@ -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;

View File

@ -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',