diff --git a/webrtc/base/logging.h b/webrtc/base/logging.h index 91d61b3e9..e07045ff1 100644 --- a/webrtc/base/logging.h +++ b/webrtc/base/logging.h @@ -246,21 +246,7 @@ void LogMultiline(LoggingSeverity level, const char* label, bool input, const void* data, size_t len, bool hex_mode, LogMultilineState* state); -////////////////////////////////////////////////////////////////////// -// Macros which automatically disable logging when LOGGING == 0 -////////////////////////////////////////////////////////////////////// - -// If LOGGING is not explicitly defined, default to enabled in debug mode -#if !defined(LOGGING) -#if defined(_DEBUG) && !defined(NDEBUG) -#define LOGGING 1 -#else -#define LOGGING 0 -#endif -#endif // !defined(LOGGING) - #ifndef LOG -#if LOGGING // The following non-obvious technique for implementation of a // conditional log stream was stolen from google3/base/logging.h. @@ -317,30 +303,6 @@ inline bool LogCheckLevel(LoggingSeverity sev) { #define LOG_T(sev) LOG(sev) << this << ": " -#else // !LOGGING - -// Hopefully, the compiler will optimize away some of this code. -// Note: syntax of "1 ? (void)0 : LogMessage" was causing errors in g++, -// converted to "while (false)" -#define LOG(sev) \ - while (false)rtc:: LogMessage(NULL, 0, rtc::sev).stream() -#define LOG_V(sev) \ - while (false) rtc::LogMessage(NULL, 0, sev).stream() -#define LOG_F(sev) LOG(sev) << __FUNCTION__ << ": " -#define LOG_CHECK_LEVEL(sev) \ - false -#define LOG_CHECK_LEVEL_V(sev) \ - false - -#define LOG_E(sev, ctx, err, ...) \ - while (false) rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \ - rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \ - .stream() - -#define LOG_T(sev) LOG(sev) << this << ": " -#define LOG_T_F(sev) LOG(sev) << this << ": " << __FUNCTION__ << -#endif // !LOGGING - #define LOG_ERRNO_EX(sev, err) \ LOG_E(sev, ERRNO, err) #define LOG_ERRNO(sev) \ diff --git a/webrtc/base/openssladapter.cc b/webrtc/base/openssladapter.cc index 601b4afcb..68a1fcb18 100644 --- a/webrtc/base/openssladapter.cc +++ b/webrtc/base/openssladapter.cc @@ -695,7 +695,10 @@ bool OpenSSLAdapter::VerifyServerName(SSL* ssl, const char* host, } STACK_OF(CONF_VALUE)* value = meth->i2v(meth, ext_str, NULL); - for (int j = 0; j < sk_CONF_VALUE_num(value); ++j) { + + // Cast to size_t to be compilable for both OpenSSL and BoringSSL. + for (size_t j = 0; j < static_cast(sk_CONF_VALUE_num(value)); + ++j) { CONF_VALUE* nval = sk_CONF_VALUE_value(value, j); // The value for nval can contain wildcards if (!strcmp(nval->name, "DNS") && string_match(host, nval->value)) { diff --git a/webrtc/base/thread.cc b/webrtc/base/thread.cc index 49a299d65..6da9a7fbb 100644 --- a/webrtc/base/thread.cc +++ b/webrtc/base/thread.cc @@ -125,6 +125,16 @@ struct ThreadInit { Runnable* runnable; }; +Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() + : thread_(Thread::Current()), + previous_state_(thread_->SetAllowBlockingCalls(false)) { +} + +Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { + ASSERT(thread_->IsCurrent()); + thread_->SetAllowBlockingCalls(previous_state_); +} + Thread::Thread(SocketServer* ss) : MessageQueue(ss), priority_(PRIORITY_NORMAL), @@ -133,7 +143,8 @@ Thread::Thread(SocketServer* ss) thread_(NULL), thread_id_(0), #endif - owned_(true) { + owned_(true), + blocking_calls_allowed_(true) { SetName("Thread", this); // default name } @@ -143,6 +154,8 @@ Thread::~Thread() { } bool Thread::SleepMs(int milliseconds) { + AssertBlockingIsAllowedOnCurrentThread(); + #if defined(WEBRTC_WIN) ::Sleep(milliseconds); return true; @@ -276,6 +289,8 @@ bool Thread::Start(Runnable* runnable) { } void Thread::Join() { + AssertBlockingIsAllowedOnCurrentThread(); + if (running()) { ASSERT(!IsCurrent()); #if defined(WEBRTC_WIN) @@ -291,6 +306,21 @@ void Thread::Join() { } } +bool Thread::SetAllowBlockingCalls(bool allow) { + ASSERT(IsCurrent()); + bool previous = blocking_calls_allowed_; + blocking_calls_allowed_ = allow; + return previous; +} + +// static +void Thread::AssertBlockingIsAllowedOnCurrentThread() { +#ifdef _DEBUG + Thread* current = Thread::Current(); + ASSERT(!current || current->blocking_calls_allowed_); +#endif +} + #if defined(WEBRTC_WIN) // As seen on MSDN. // http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx @@ -357,6 +387,8 @@ void Thread::Stop() { } void Thread::Send(MessageHandler *phandler, uint32 id, MessageData *pdata) { + AssertBlockingIsAllowedOnCurrentThread(); + if (fStop_) return; diff --git a/webrtc/base/thread.h b/webrtc/base/thread.h index 38727464b..6132aa0a7 100644 --- a/webrtc/base/thread.h +++ b/webrtc/base/thread.h @@ -108,6 +108,19 @@ class Thread : public MessageQueue { static Thread* Current(); + // Used to catch performance regressions. Use this to disallow blocking calls + // (Invoke) for a given scope. If a synchronous call is made while this is in + // effect, an assert will be triggered. + // Note that this is a single threaded class. + class ScopedDisallowBlockingCalls { + public: + ScopedDisallowBlockingCalls(); + ~ScopedDisallowBlockingCalls(); + private: + Thread* const thread_; + const bool previous_state_; + }; + bool IsCurrent() const { return Current() == this; } @@ -148,8 +161,11 @@ class Thread : public MessageQueue { // Uses Send() internally, which blocks the current thread until execution // is complete. // Ex: bool result = thread.Invoke(&MyFunctionReturningBool); + // NOTE: This function can only be called when synchronous calls are allowed. + // See ScopedDisallowBlockingCalls for details. template ReturnT Invoke(const FunctorT& functor) { + AssertBlockingIsAllowedOnCurrentThread(); FunctorMessageHandler handler(functor); Send(&handler); return handler.result(); @@ -212,6 +228,14 @@ class Thread : public MessageQueue { // Blocks the calling thread until this thread has terminated. void Join(); + // Sets the per-thread allow-blocking-calls flag and returns the previous + // value. + bool SetAllowBlockingCalls(bool allow); + + static void AssertBlockingIsAllowedOnCurrentThread(); + + friend class ScopedDisallowBlockingCalls; + private: static void *PreRun(void *pv); @@ -238,6 +262,7 @@ class Thread : public MessageQueue { #endif bool owned_; + bool blocking_calls_allowed_; // By default set to |true|. friend class ThreadManager;