提交 78c5a39f authored 作者: Saúl Ibarra Corretgé's avatar Saúl Ibarra Corretgé 提交者: Iñaki Baz Castillo

Portability (#35)

* Remove unneeded pthread_sigmask call

* Use libuv provided threading utilities

Increases portability.

* Simplify detecting system endianness
上级 46c7d264
......@@ -4,11 +4,12 @@
#include "common.h"
#include <openssl/ssl.h>
#include <openssl/crypto.h>
#include <uv.h>
/* OpenSSL doc: struct CRYPTO_dynlock_value has to be defined by the application. */
struct CRYPTO_dynlock_value
{
pthread_mutex_t mutex;
uv_mutex_t mutex;
};
class DepOpenSSL
......@@ -25,7 +26,7 @@ private:
static void DynDestroyFunction(CRYPTO_dynlock_value* v, const char* file, int line);
private:
static pthread_mutex_t* mutexes;
static uv_mutex_t* mutexes;
static uint32_t numMutexes;
};
......
......@@ -13,17 +13,5 @@
#define MS_APP_NAME "mediasoup"
#define MS_PROCESS_NAME "mediasoup-worker"
// Detect Little-Endian or Big-Endian CPU.
// NOTE: If the macro MS_LITTLE_ENDIAN or MS_BIG_ENDIAN is already defined
// then don't autodetect it.
#if !defined(MS_LITTLE_ENDIAN) && !defined(MS_BIG_ENDIAN)
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define MS_LITTLE_ENDIAN
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define MS_BIG_ENDIAN
#else
#error "cannot determine whether the CPU is Little-Endian or Big-Endian"
#endif
#endif
#endif
......@@ -118,6 +118,15 @@
'defines': [ 'BUD_FIPS_ENABLED=1' ]
}],
# Endianness
[ 'node_byteorder=="big"', {
# Define Big Endian
'defines': ['MS_BIG_ENDIAN']
}, {
# Define Little Endian
'defines': ['MS_LITTLE_ENDIAN']
}],
# Platform-specifics
[ 'OS == "mac" and mediasoup_asan == "true"', {
......
......@@ -9,7 +9,7 @@
/* Static attributes. */
// This array will store all of the mutex available for OpenSSL.
pthread_mutex_t* DepOpenSSL::mutexes = nullptr;
uv_mutex_t* DepOpenSSL::mutexes = nullptr;
uint32_t DepOpenSSL::numMutexes = 0;
/* Static methods. */
......@@ -26,7 +26,7 @@ void DepOpenSSL::ClassInit()
RAND_poll();
// Make OpenSSL thread-safe (even if we are single thread).
DepOpenSSL::mutexes = new pthread_mutex_t[CRYPTO_num_locks()];
DepOpenSSL::mutexes = new uv_mutex_t[CRYPTO_num_locks()];
if (!DepOpenSSL::mutexes)
MS_THROW_ERROR("allocation of mutexes failed");
......@@ -34,9 +34,9 @@ void DepOpenSSL::ClassInit()
for (uint32_t i = 0; i < DepOpenSSL::numMutexes; i++)
{
int err = pthread_mutex_init(&DepOpenSSL::mutexes[i], nullptr);
int err = uv_mutex_init(&DepOpenSSL::mutexes[i]);
if (err)
MS_THROW_ERROR("pthread_mutex_init() failed with return code %d\n", err);
MS_THROW_ERROR("uv_mutex_init() failed with return code %d\n", err);
}
CRYPTO_THREADID_set_callback(DepOpenSSL::SetThreadId);
......@@ -70,9 +70,7 @@ void DepOpenSSL::ClassDestroy()
// Free mutexes.
for (uint32_t i = 0; i < DepOpenSSL::numMutexes; i++)
{
int err = pthread_mutex_destroy(&DepOpenSSL::mutexes[i]);
if (err)
MS_ERROR("pthread_mutex_destroy() failed with return code %d\n", err);
uv_mutex_destroy(&DepOpenSSL::mutexes[i]);
}
if (DepOpenSSL::mutexes)
delete[] DepOpenSSL::mutexes;
......@@ -89,7 +87,7 @@ void DepOpenSSL::SetThreadId(CRYPTO_THREADID* id)
{
// MS_TRACE();
CRYPTO_THREADID_set_numeric(id, (unsigned long)pthread_self());
CRYPTO_THREADID_set_numeric(id, (unsigned long)uv_thread_self());
}
void DepOpenSSL::LockingFunction(int mode, int n, const char *file, int line)
......@@ -110,9 +108,9 @@ void DepOpenSSL::LockingFunction(int mode, int n, const char *file, int line)
// MS_DEBUG("[mode: %s+%s, mutex id: %d, file: %s, line: %d]", mode & CRYPTO_LOCK ? "LOCK" : "UNLOCK", mode & CRYPTO_READ ? "READ" : "WRITE", n, file, line);
if (mode & CRYPTO_LOCK)
pthread_mutex_lock(&DepOpenSSL::mutexes[n]);
uv_mutex_lock(&DepOpenSSL::mutexes[n]);
else
pthread_mutex_unlock(&DepOpenSSL::mutexes[n]);
uv_mutex_unlock(&DepOpenSSL::mutexes[n]);
}
CRYPTO_dynlock_value* DepOpenSSL::DynCreateFunction(const char* file, int line)
......@@ -127,7 +125,7 @@ CRYPTO_dynlock_value* DepOpenSSL::DynCreateFunction(const char* file, int line)
return nullptr;
}
pthread_mutex_init(&value->mutex, nullptr);
uv_mutex_init(&value->mutex);
return value;
}
......@@ -136,15 +134,15 @@ void DepOpenSSL::DynLockFunction(int mode, CRYPTO_dynlock_value* v, const char*
// MS_TRACE();
if (mode & CRYPTO_LOCK)
pthread_mutex_lock(&v->mutex);
uv_mutex_lock(&v->mutex);
else
pthread_mutex_unlock(&v->mutex);
uv_mutex_unlock(&v->mutex);
}
void DepOpenSSL::DynDestroyFunction(CRYPTO_dynlock_value* v, const char* file, int line)
{
// MS_TRACE();
pthread_mutex_destroy(&v->mutex);
uv_mutex_destroy(&v->mutex);
delete v;
}
......@@ -7,7 +7,6 @@
#include "Logger.h"
#include <string>
#include <utility> // std::pair()
#include <csignal> // sigfillset, pthread_sigmask()
#include <cerrno>
#include <iostream> // std::cout, std::cerr
#include <json/json.h>
......@@ -46,9 +45,6 @@ void Loop::Close()
{
MS_TRACE();
int err;
sigset_t signal_mask;
if (this->closed)
{
MS_ERROR("already closed");
......@@ -57,12 +53,6 @@ void Loop::Close()
}
this->closed = true;
// First block all the signals to not be interrupted while closing.
sigfillset(&signal_mask);
err = pthread_sigmask(SIG_BLOCK, &signal_mask, nullptr);
if (err)
MS_ERROR("pthread_sigmask() failed: %s", std::strerror(errno));
// Close the SignalsHandler.
if (this->signalsHandler)
this->signalsHandler->Close();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论