WebRTC源碼分析-線程基礎(chǔ)之線程基本功能

前言

如之前的總述文章所述,rtc::Thread類封裝了WebRTC中線程的一般功能,比如設(shè)置線程名稱,啟動(dòng)線程執(zhí)行用戶代碼,線程的join,sleep,run,stop等方法;同時(shí)也提供了線程內(nèi)部的消息循環(huán),以及線程之間以同步、異步方式投遞消息,同步方式在目標(biāo)線程執(zhí)行方法并返回結(jié)果等線程之間交互的方式;另外,每個(gè)線程均持有SocketServer類成員對(duì)象,該類實(shí)現(xiàn)了IO多路復(fù)用功能。

本文將針對(duì)rtc::Thread類所提供的基礎(chǔ)線程功能來(lái)進(jìn)行介紹,Thread類在rtc_base目錄下的thread.h中聲明,如下(刪除了其他非線程基礎(chǔ)功能的API,其他的API將于另外的文章中分析):

class RTC_LOCKABLE Thread : public MessageQueue {
public:
  // 線程的構(gòu)造,析構(gòu)
  Thread();
  explicit Thread(SocketServer* ss);
  explicit Thread(std::unique_ptr<SocketServer> ss);
  Thread(SocketServer* ss, bool do_init);
  Thread(std::unique_ptr<SocketServer> ss, bool do_init);
  ~Thread() override;
  static std::unique_ptr<Thread> CreateWithSocketServer();
  static std::unique_ptr<Thread> Create();

  // 線程的名字
  const std::string& name() const { return name_; }
  bool SetName(const std::string& name, const void* obj);

  // 當(dāng)前線程
  static Thread* Current();
  bool IsCurrent() const;

  // 阻塞權(quán)限
  bool SetAllowBlockingCalls(bool allow);
  static void AssertBlockingIsAllowedOnCurrentThread();

  // 休眠
  static bool SleepMs(int millis);

  // 線程的啟動(dòng)與停止
  bool Start(Runnable* runnable = nullptr);
  virtual void Stop();
  virtual void Run();

  // 線程的Wrap
  bool IsOwned();
  bool WrapCurrent();
  void UnwrapCurrent();

 protected:
  void SafeWrapCurrent();

  // 等待線程結(jié)束
  void Join();

 private:
#if defined(WEBRTC_WIN)
  static DWORD WINAPI PreRun(LPVOID context);
#else
  static void* PreRun(void* pv);
#endif

  bool WrapCurrentWithThreadManager(ThreadManager* thread_manager,
                                    bool need_synchronize_access);
  bool IsRunning();

  std::string name_;

#if defined(WEBRTC_POSIX)
  pthread_t thread_ = 0;
#endif
#if defined(WEBRTC_WIN)
  HANDLE thread_ = nullptr;
  DWORD thread_id_ = 0;
#endif
  bool owned_ = true;

  friend class ThreadManager;

  RTC_DISALLOW_COPY_AND_ASSIGN(Thread);
};

Thread對(duì)象的創(chuàng)建

創(chuàng)建Thread對(duì)象的構(gòu)造方法有5個(gè),如下源碼所示:

// DEPRECATED.
Thread::Thread() : Thread(SocketServer::CreateDefault()) {}

Thread::Thread(SocketServer* ss) : Thread(ss, /*do_init=*/true) {}

Thread::Thread(std::unique_ptr<SocketServer> ss)
    : Thread(std::move(ss), /*do_init=*/true) {}

Thread::Thread(SocketServer* ss, bool do_init)
    : MessageQueue(ss, /*do_init=*/false) {
  SetName("Thread", this);  // default name
  if (do_init) {
    DoInit();
  }
}

Thread::Thread(std::unique_ptr<SocketServer> ss, bool do_init)
    : MessageQueue(std::move(ss), false) {
  SetName("Thread", this);  // default name
  if (do_init) {
    DoInit();
  }
}

需要注意的是:

  1. 默認(rèn)構(gòu)造函數(shù)Thread()被標(biāo)注為DEPRECATED,原因是其對(duì)外隱藏了一個(gè)事實(shí),即Thread對(duì)象是否與一個(gè)SocketServer對(duì)象綁定。實(shí)際上該默認(rèn)構(gòu)造會(huì)創(chuàng)建一個(gè)SocketServer對(duì)象綁定到Thread對(duì)象,而大多數(shù)的應(yīng)用場(chǎng)景下Thread對(duì)象不需要SocketServer。因此,源碼的注釋中告知使用Create*的兩個(gè)靜態(tài)方法來(lái)創(chuàng)建Thread對(duì)象。

  2. 兩個(gè)explicit聲明的單個(gè)入?yún)⒌臉?gòu)造函數(shù),分別會(huì)調(diào)用含有兩個(gè)入?yún)⒌臉?gòu)造函數(shù),唯一的區(qū)別在于入?yún)⑹荢ocketServer*指針還是智能指針std::unique_ptr<SocketServer>類型。如果是后者那么需要使用std::move來(lái)轉(zhuǎn)移SocketServer的擁有權(quán)(std::move是一個(gè)c++11的語(yǔ)法糖,實(shí)現(xiàn)了移動(dòng)語(yǔ)義,詳細(xì)的分析可以見(jiàn)博客C++11 std::move和std::forward)。但無(wú)論是哪個(gè)構(gòu)造函數(shù),都需要做以下三件事:
    1) 構(gòu)造父對(duì)象MQ。由于Thread繼承于MessageQueue對(duì)象,因此首先構(gòu)造MQ父對(duì)象,調(diào)用MQ的構(gòu)造函數(shù),傳入SocketServer對(duì)象指針以及布爾值false。此處傳入的SocketServer不允許為空,否則觸發(fā)斷言;此處傳入布爾值false,告知MQ的構(gòu)造函數(shù) “DoInit()方法你就不要調(diào)用了,我會(huì)在外面調(diào)用的”。
    2) 調(diào)用DoInit(),該方法應(yīng)該在Thread構(gòu)造中調(diào)用,而非在MQ的構(gòu)造中調(diào)用,為什么要這么做?該方法源碼如下:我們會(huì)發(fā)現(xiàn)該方法將MQ的初始化標(biāo)志置為true,并且將自身納入MQ管理類的管理列表中。如果DoInit在MQ構(gòu)造中調(diào)用,意味著MQ構(gòu)造后,Thread對(duì)象的指針已經(jīng)暴露于外(被MQ管理類對(duì)象持有),此時(shí)Thread對(duì)象并未完全構(gòu)建完成,其虛表vtable還未完全建立。這勢(shì)必會(huì)導(dǎo)致Thread的對(duì)象還未構(gòu)造完成時(shí),就可能會(huì)被外部使用(在別的線程中通過(guò)MessageQueueManager訪問(wèn)該對(duì)象)的風(fēng)險(xiǎn)。為了規(guī)避這樣的競(jìng)太條件,因此,需要給MQ的構(gòu)造傳入false,并在Thread構(gòu)造中調(diào)用DoInit()。

void MessageQueue::DoInit() {
  if (fInitialized_) {
    return;
  }

  fInitialized_ = true;
  MessageQueueManager::Add(this);
}

3)調(diào)用SetName()方法為Thread對(duì)象命名。源碼如下,需要明白的一點(diǎn)是,該方法的執(zhí)行必須在線程啟動(dòng)之前,否則會(huì)觸發(fā)斷言。并且,由于是在線程啟動(dòng)之前執(zhí)行,該方法僅僅是給用戶層的Thread對(duì)象成員name_賦值而已,系統(tǒng)內(nèi)核中線程相關(guān)的結(jié)構(gòu)體還未建立,因此,也就無(wú)法將該名稱設(shè)置到內(nèi)核。只有當(dāng)線程啟動(dòng)后,才能進(jìn)一步的將name_設(shè)置到線程的內(nèi)核結(jié)構(gòu)體中去。一般默認(rèn)名稱形如"Thread 0x04EFF758"。

bool Thread::SetName(const std::string& name, const void* obj) {
  RTC_DCHECK(!IsRunning());

  name_ = name;
  if (obj) {
    // The %p specifier typically produce at most 16 hex digits, possibly with a
    // 0x prefix. But format is implementation defined, so add some margin.
    char buf[30];
    snprintf(buf, sizeof(buf), " 0x%p", obj);
    name_ += buf;
  }
  return true;
}

兩個(gè)靜態(tài)Create*方法來(lái)創(chuàng)建Thread對(duì)象,一個(gè)傳入的是NullSocketServer對(duì)象,該對(duì)象不持有真正的Socket,不處理網(wǎng)絡(luò)IO;另外一個(gè)傳入PhysicalSocketServer對(duì)象,持有平臺(tái)相關(guān)的Socket對(duì)象,能處理網(wǎng)絡(luò)IO。源碼如下:

std::unique_ptr<Thread> Thread::CreateWithSocketServer() {
  return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault()));
}

std::unique_ptr<Thread> Thread::Create() {
  return std::unique_ptr<Thread>(
      new Thread(std::unique_ptr<SocketServer>(new NullSocketServer())));
}

線程的啟動(dòng)

線程啟動(dòng)相關(guān)的API為Start(),IsRunnning(),PreRun(),結(jié)構(gòu)體ThreadInit,類Runable,平臺(tái)相關(guān)的線程啟動(dòng)函數(shù)CreateThread()以及pthread_create()。

  • Start() 方法是WebRTC中線程啟動(dòng)的標(biāo)準(zhǔn)方法,算法流程如下:斷言,判斷線程是否已經(jīng)啟動(dòng)-->復(fù)位底層消息循環(huán)停止標(biāo)志位-->確保ThreadManager對(duì)象的創(chuàng)建-->賦值結(jié)構(gòu)體ThreadInit->平臺(tái)相關(guān)的線程啟動(dòng)。
bool Thread::Start(Runnable* runnable) {
  // 測(cè)試環(huán)境下的斷言,當(dāng)前線程必須處于非運(yùn)行狀態(tài),否則觸發(fā)Fatal Error
  RTC_DCHECK(!IsRunning());

  // 如果線程處于運(yùn)行狀態(tài)則Start返回false
  if (IsRunning())
    return false;

  // 復(fù)位消息循環(huán)stop標(biāo)志位
  Restart();  // reset IsQuitting() if the thread is being restarted

  // Make sure that ThreadManager is created on the main thread before
  // we start a new thread.
  // 確保ThreadManager在主線程中構(gòu)建
  ThreadManager::Instance();
  
  // 線程對(duì)象Thread不是Wrap而來(lái)
  owned_ = true;

  // 賦值結(jié)構(gòu)體ThreadInit
  ThreadInit* init = new ThreadInit;
  init->thread = this;
  init->runnable = runnable;

  // 平臺(tái)相關(guān)代碼,Windows系統(tǒng)下啟動(dòng)線程,使用CreateThread API
#if defined(WEBRTC_WIN)
  thread_ = CreateThread(nullptr, 0, PreRun, init, 0, &thread_id_);
  if (!thread_) {
    return false;
  }
  // 類Unix系統(tǒng)下啟動(dòng)線程,使用pthread庫(kù)
#elif defined(WEBRTC_POSIX)
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  int error_code = pthread_create(&thread_, &attr, PreRun, init);
  if (0 != error_code) {
    RTC_LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;
    thread_ = 0;
    return false;
  }
  RTC_DCHECK(thread_);
#endif

  return true;
}
  • RTC_DCHECK 宏在debug模式下,被定義為RTC_CHECK,起到斷言的作用(具體會(huì)專門寫一篇文章來(lái)分析WebRTC中的斷言);在非debug模式下,RTC_DCHECK被定義為RTC_EAT_STREAM_PARAMETERS,不論condition(ignored)是true or false,該宏定義的最后兩行斷言代碼都不會(huì)被執(zhí)行,而宏定義的第二行(true ? true : ((void)(ignored), true))是為了不讓編譯器報(bào)ignored未被使用的警告。
// The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
// code in debug builds. It does reference the condition parameter in all cases,
// though, so callers won't risk getting warnings about unused variables.
#if RTC_DCHECK_IS_ON
#define RTC_DCHECK(condition) RTC_CHECK(condition)
#else
#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
#endif

#define RTC_EAT_STREAM_PARAMETERS(ignored)                        \
  (true ? true : ((void)(ignored), true))                         \
      ? static_cast<void>(0)                                      \
      : rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
            rtc::webrtc_checks_impl::LogStreamer<>()
  • IsRunning() 代碼如下。在Thread類聲明中,Windows環(huán)境下有兩個(gè)值來(lái)表征線程,句柄類型的HWND thread_被初始化為nullptr,整數(shù)類型的DWORD thread_id_ 被初始化為0; 類Unix系統(tǒng)中只有pthread_t thread_一個(gè)變量來(lái)表征線程,初始化為0。判斷線程是否為Running狀態(tài),只要判斷thread_是否為有效值即可,因?yàn)樵赟tart()方法中啟動(dòng)線程時(shí),會(huì)調(diào)用平臺(tái)相關(guān)的方法給該參數(shù)賦有效值。
bool Thread::IsRunning() {
#if defined(WEBRTC_WIN)
  return thread_ != nullptr;
#elif defined(WEBRTC_POSIX)
  return thread_ != 0;
#endif
}
  • Restart() 是父類MQ的方法, 作用是原子操作將stop_置為0,表征該MQ未停止。
void MessageQueue::Restart() {
  AtomicOps::ReleaseStore(&stop_, 0);
}
  • 初始化ThreadManager 此處即調(diào)用ThreadManager::instance()方法,確保ThreadManager在主線程中初始化。另外owned_設(shè)置為true,表征Thread對(duì)象是常規(guī)的Start()方法啟用的,而非Wrap而來(lái)。
  • ThreadInit 結(jié)構(gòu)體用于平臺(tái)相關(guān)的啟動(dòng)線程函數(shù)的入?yún)ⅲx如下:
  struct ThreadInit {
    Thread* thread;
    Runnable* runnable;
  };

其中Runnable用來(lái)承載用戶需要執(zhí)行的代碼,用于繼承Runnable并實(shí)現(xiàn)Run方法即可。

class Runnable {
 public:
  virtual ~Runnable() {}
  virtual void Run(Thread* thread) = 0;
 protected:
  Runnable() {}
 private:
  RTC_DISALLOW_COPY_AND_ASSIGN(Runnable);
};
  • 啟動(dòng)線程:
    在Windows上調(diào)用CreateThread API來(lái)啟動(dòng)線程,當(dāng)該方法執(zhí)行成功時(shí),將返回線程的句柄給thread_,線程的id給thread_id_,并在該線程上執(zhí)行PreRun()方法,init為上述ThreadInit對(duì)象,其作為入?yún)鬟f給PreRun()方法。
#if defined(WEBRTC_WIN)
  thread_ = CreateThread(nullptr, 0, PreRun, init, 0, &thread_id_);
  if (!thread_) {
    return false;
  }

在類Unix系統(tǒng)上,使用pthread庫(kù)的pthread_create() API來(lái)啟用線程,方法執(zhí)行成功對(duì)pthread thread_的成員賦值,類似的將在新線程上執(zhí)行PreRun()方法,并將ThreadInit結(jié)構(gòu)的對(duì)象init作為其入?yún)魅搿?/p>

#elif defined(WEBRTC_POSIX)
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  int error_code = pthread_create(&thread_, &attr, PreRun, init);
  if (0 != error_code) {
    RTC_LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;
    thread_ = 0;
    return false;
  }
  RTC_DCHECK(thread_);
#endif
  • PreRun() 方法是在新啟用的線程上執(zhí)行的代碼,不同平臺(tái)下該函數(shù)聲明上略有差別,實(shí)質(zhì)上入?yún)⑹窍嗤腡hreadInit對(duì)象。本函數(shù)是本文最重要的地方,涉及到了ThreadManager管理Thread類的秘密,涉及到了用戶代碼如何在新線程上被執(zhí)行,線程內(nèi)部的消息循環(huán)是如何被運(yùn)行起來(lái)的。
    函數(shù)內(nèi)部的執(zhí)行邏輯寫在代碼注釋上,特殊需要拆解的地方后續(xù)說(shuō)明,源碼如下:
#if defined(WEBRTC_WIN)
DWORD WINAPI Thread::PreRun(LPVOID pv) {
#else
void* Thread::PreRun(void* pv) {
#endif
  // 如前文所述,ThreadInit作為入?yún)鹘oPreRun方法。
  ThreadInit* init = static_cast<ThreadInit*>(pv);
  // 將新創(chuàng)建的Thread對(duì)象納入管理,與當(dāng)前線程進(jìn)行綁定。
  ThreadManager::Instance()->SetCurrentThread(init->thread);
  // 為線程設(shè)置名稱,該方法會(huì)調(diào)用平臺(tái)相關(guān)的API給線程內(nèi)核結(jié)構(gòu)體賦值上該線程的名稱。
  rtc::SetCurrentThreadName(init->thread->name_.c_str());
 // 如果是MAC系統(tǒng),通過(guò)pool對(duì)象的創(chuàng)建和析構(gòu)來(lái)使用oc的自動(dòng)釋放池技術(shù),進(jìn)行內(nèi)存回收。
#if defined(WEBRTC_MAC)
  ScopedAutoReleasePool pool;
#endif
  // 如果用戶需要執(zhí)行自己的代碼,那么就會(huì)繼承Runnable并實(shí)現(xiàn)Run方法,此時(shí),正是執(zhí)行
  // 用戶代碼的時(shí)刻;否則,將執(zhí)行Thread的默認(rèn)Run方法。
  if (init->runnable) {
    init->runnable->Run(init->thread);
  } else {
    init->thread->Run();
  }
  // 到此,線程主要的活兒已干完,以下做清理工作
  // 將線程對(duì)象與當(dāng)前線程解綁
  ThreadManager::Instance()->SetCurrentThread(nullptr);
  // 釋放ThreadInit對(duì)象
  delete init;
  // 返回,記得pool局部對(duì)象的釋放會(huì)觸發(fā)MAC系統(tǒng)下的自動(dòng)釋放池進(jìn)行內(nèi)存回收。
#ifdef WEBRTC_WIN
  return 0;
#else
  return nullptr;
#endif
}

上述代碼需要特殊關(guān)注點(diǎn)在于:
1)新線程上PreRun()方法執(zhí)行起來(lái)后,ThreadManager立馬將當(dāng)前線程與該Thread對(duì)象關(guān)聯(lián)起來(lái),納入管理之中,當(dāng)PreRun()方法要執(zhí)行完畢了,又將當(dāng)前線程與Thread對(duì)象解綁,畢竟該方法退出后,線程就會(huì)停止。
2)為當(dāng)前線程設(shè)置名稱:前文已經(jīng)知道在Thread對(duì)象構(gòu)造時(shí),會(huì)給Thread的命名字段name_賦值形如"Thread 0x04EFF758"的名稱,但并未調(diào)用系統(tǒng)相關(guān)的API給線程內(nèi)核對(duì)象相關(guān)的字段賦值,因?yàn)槟莻€(gè)時(shí)候線程還未啟動(dòng),線程在系統(tǒng)內(nèi)核中還沒(méi)有相應(yīng)的對(duì)象存在呢。此時(shí),需要做這個(gè)工作。如下就是rtc::SetCurrentThreadName(init->thread->name_.c_str()) 方法的源碼:

void SetCurrentThreadName(const char* name) {
#if defined(WEBRTC_WIN)
  struct {
    DWORD dwType;
    LPCSTR szName;
    DWORD dwThreadID;
    DWORD dwFlags;
  } threadname_info = {0x1000, name, static_cast<DWORD>(-1), 0};

  __try {
    ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD),
                     reinterpret_cast<ULONG_PTR*>(&threadname_info));
  } __except (EXCEPTION_EXECUTE_HANDLER) {  // NOLINT
  }
#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
  prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name));  // NOLINT
#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
  pthread_setname_np(name);
#endif
}

3)作為Mac系統(tǒng)上的特例,使用了objc的自動(dòng)釋放池技術(shù)來(lái)管理內(nèi)存,實(shí)際上就是通過(guò)局部變量ScopedAutoReleasePool pool的構(gòu)造以及PreRun函數(shù)結(jié)束時(shí)該對(duì)象的析構(gòu)來(lái)調(diào)用objc的objc_autoreleasePoolPush()和objc_autoreleasePoolPop()進(jìn)行內(nèi)存釋放。至于其原理嘛,可以看此篇博客:自動(dòng)釋放池的前世今生 ---- 深入解析 Autoreleasepool

#if defined(WEBRTC_MAC)
#include "rtc_base/system/cocoa_threading.h"

extern "C" {
void* objc_autoreleasePoolPush(void);
void objc_autoreleasePoolPop(void* pool);
}

class ScopedAutoReleasePool {
 public:
  ScopedAutoReleasePool() : pool_(objc_autoreleasePoolPush()) {}
  ~ScopedAutoReleasePool() { objc_autoreleasePoolPop(pool_); }
 private:
  void* const pool_;
};
#endif

4)如果用戶并不想執(zhí)行自己的代碼,即不給Start方法傳入Runnabel對(duì)象,那么Thread對(duì)象提供了默認(rèn)的Run()方法在新線程上執(zhí)行,該方法源碼如下。本文不展開(kāi)去敘述ProcessMessages(kForever)是如何運(yùn)作的,因?yàn)檫@屬于消息循環(huán)的內(nèi)容,會(huì)在下一篇文章中分析,此處只要知道,如果用戶不運(yùn)行自己的代碼干自己的活,那么默認(rèn)的方式就是啟動(dòng)了一個(gè)消息循環(huán)不停地在此執(zhí)行。

void Thread::Run() {
  ProcessMessages(kForever);
}

bool Thread::ProcessMessages(int cmsLoop) {
  // Using ProcessMessages with a custom clock for testing and a time greater
  // than 0 doesn't work, since it's not guaranteed to advance the custom
  // clock's time, and may get stuck in an infinite loop.
  RTC_DCHECK(GetClockForTesting() == nullptr || cmsLoop == 0 ||
             cmsLoop == kForever);
  int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop);
  int cmsNext = cmsLoop;

  while (true) {
#if defined(WEBRTC_MAC)
    ScopedAutoReleasePool pool;
#endif
    Message msg;
    if (!Get(&msg, cmsNext))
      return !IsQuitting();
    Dispatch(&msg);

    if (cmsLoop != kForever) {
      cmsNext = static_cast<int>(TimeUntil(msEnd));
      if (cmsNext < 0)
        return true;
    }
  }
}

線程的終止

停止一個(gè)線程,可以通過(guò)調(diào)用線程的Thread.Stop()方法來(lái)實(shí)施,但千萬(wàn)不能在當(dāng)前線程上調(diào)用該方法來(lái)終止自己。MQ的Quit()方法會(huì)在介紹消息循環(huán)時(shí)來(lái)詳細(xì)解釋,此處作用就是停止線程消息循環(huán)。Join()方法在后面介紹,此處作用是阻塞地等待目標(biāo)線程終止,因此,Stop函數(shù)一般會(huì)阻塞當(dāng)前線程。

void Thread::Stop() {
  MessageQueue::Quit();
  Join();
}

線程的sleep,join以及阻塞權(quán)限

Thread類中bool blocking_calls_allowed_字段控制著在該線程是否可以運(yùn)行阻塞,等待操作,比如靜態(tài)方法SleepMs,線程的Join。
-SleepMs() 方法提供了線程休眠功能,方法中先對(duì)當(dāng)前線程是否允許阻塞進(jìn)行斷言,然后在Windows上調(diào)用Windows API Sleep()方法對(duì)當(dāng)前線程休眠,類Unix系統(tǒng)上使用nanosleep()系統(tǒng)調(diào)用進(jìn)行休眠。

bool Thread::SleepMs(int milliseconds) {
  // 斷言當(dāng)前線程是否允許阻塞
  AssertBlockingIsAllowedOnCurrentThread();
 //調(diào)用不同平臺(tái)下的線程休眠函數(shù)進(jìn)行休眠
#if defined(WEBRTC_WIN)
  ::Sleep(milliseconds);
  return true;
#else
  // POSIX has both a usleep() and a nanosleep(), but the former is deprecated,
  // so we use nanosleep() even though it has greater precision than necessary.
  struct timespec ts;
  ts.tv_sec = milliseconds / 1000;
  ts.tv_nsec = (milliseconds % 1000) * 1000000;
  int ret = nanosleep(&ts, nullptr);
  if (ret != 0) {
    RTC_LOG_ERR(LS_WARNING) << "nanosleep() returning early";
    return false;
  }
  return true;
#endif
}

此處我們看一下這個(gè)是否允許阻塞的函數(shù)實(shí)現(xiàn),該方法由NDEBUG宏來(lái)控制,意味著在debug模式下才會(huì)起作用,而非debug模式下,該函數(shù)什么也不做。在debug模式,如果當(dāng)前線程關(guān)聯(lián)了Thread對(duì)象,并且其Thread.blocking_calls_allowed_字段設(shè)置為false,表示不允許該線程阻塞的情況下,就會(huì)觸發(fā)斷言。即在此Thread所關(guān)聯(lián)的線程中調(diào)用SleepMs()方法會(huì)觸發(fā)斷言,從而終止程序的運(yùn)行。

// static
void Thread::AssertBlockingIsAllowedOnCurrentThread() {
#if !defined(NDEBUG)
  Thread* current = Thread::Current();
  RTC_DCHECK(!current || current->blocking_calls_allowed_);
#endif
}

另外,Thread也提供了一個(gè)可以設(shè)置blocking_calls_allowed_字段的方法SetAllowBlockingCalls()

bool Thread::SetAllowBlockingCalls(bool allow) {
  RTC_DCHECK(IsCurrent());
  bool previous = blocking_calls_allowed_;
  blocking_calls_allowed_ = allow;
  return previous;
}
  • Join()
    該方法用于某個(gè)線程中等待另外一個(gè)Thread所表征的線程停止。算法流程如源碼注釋:
void Thread::Join() {
  // 判斷等待的線程對(duì)象Thread所表征的線程是否已經(jīng)停止,若已停止了,那么就不需要等待了,直接返回吧
  if (!IsRunning())
    return;
  // 斷言是否是在當(dāng)前線程調(diào)用自己的Join造成自己等待自己
  RTC_DCHECK(!IsCurrent());
  // 判斷當(dāng)前線程是否具有阻塞權(quán)限,如無(wú),則打印警告,但是并沒(méi)有進(jìn)行斷言
  if (Current() && !Current()->blocking_calls_allowed_) {
    RTC_LOG(LS_WARNING) << "Waiting for the thread to join, "
                        << "but blocking calls have been disallowed";
  }

  // 平臺(tái)相關(guān)的實(shí)現(xiàn)

  // Windows平臺(tái)下調(diào)用WaitForSingleObject() API進(jìn)行等待
#if defined(WEBRTC_WIN)
  RTC_DCHECK(thread_ != nullptr);
  // 等待目標(biāo)線程終止 
  WaitForSingleObject(thread_, INFINITE);
  // 關(guān)閉線程句柄
  CloseHandle(thread_);
  // 成員復(fù)位
  thread_ = nullptr;
  thread_id_ = 0;

  // 類Unix系統(tǒng)下調(diào)用pthread庫(kù)的pthread_join()方法進(jìn)行等待
#elif defined(WEBRTC_POSIX)
  // 等待目標(biāo)線程終止 
  pthread_join(thread_, nullptr);
  // 成員復(fù)位
  thread_ = 0;
#endif
}

獲取當(dāng)前線程對(duì)象 && 判斷是否是當(dāng)前線程

獲取當(dāng)前線程Thread對(duì)象的方式直接復(fù)用了ThreadManager的CurrentThread()方法,若當(dāng)前線程沒(méi)有關(guān)聯(lián)相關(guān)的Thread對(duì)象,那么返回空指針,若當(dāng)前線程是創(chuàng)建ThreadManager對(duì)象的線程,也即主線程,那么如果主線程沒(méi)有關(guān)聯(lián)Thread對(duì)象,且沒(méi)有定義NO_MAIN_THREAD_WRAPPING,則會(huì)給主線程Wrap一個(gè)Thread對(duì)象。

Thread* Thread::Current() {
  ThreadManager* manager = ThreadManager::Instance();
  Thread* thread = manager->CurrentThread();

#ifndef NO_MAIN_THREAD_WRAPPING
  // Only autowrap the thread which instantiated the ThreadManager.
  if (!thread && manager->IsMainThread()) {
    thread = new Thread(SocketServer::CreateDefault());
    thread->WrapCurrentWithThreadManager(manager, true);
  }
#endif
  return thread;
}

判斷Thread對(duì)象是否是當(dāng)前線程關(guān)聯(lián)的Thread,也很簡(jiǎn)單,源碼如下:

bool Thread::IsCurrent() const {
  return ThreadManager::Instance()->CurrentThread() == this;
}

線程的Wrap

Thread中Wrap相關(guān)的API有4個(gè),如下源碼所示。在WebRTC源碼分析-線程基礎(chǔ)之線程管理一文中已經(jīng)解析過(guò)Wrap相關(guān)的函數(shù),此處不再展開(kāi)表述。Wrap函數(shù)主要用于如下情形:線程啟動(dòng)不是由標(biāo)準(zhǔn)的WebRTC啟動(dòng)方式實(shí)施,即不是通過(guò)調(diào)用Thread.Start()方法啟動(dòng)。那么,此刻線程沒(méi)有與一個(gè)Thread對(duì)象相關(guān)聯(lián),那么Wrap就是干這個(gè)事,將一個(gè)線程與一個(gè)Thread對(duì)象關(guān)聯(lián)起來(lái)。具體而言,有如下3件事需要做:

  • 獲取當(dāng)前線程的句柄,并賦值給Thread.thread_成員;
  • 將Thread.owned_成員設(shè)置為false,表示該線程對(duì)象是Wrap而來(lái);
  • 將Thread對(duì)象與當(dāng)前線程關(guān)聯(lián)起來(lái),納入ThreadManager的管理之中。
bool WrapCurrent();
void UnwrapCurrent();
void SafeWrapCurrent();
bool WrapCurrentWithThreadManager(ThreadManager* thread_manager,
                                    bool need_synchronize_access);

總結(jié)

本文闡述了rtc::Thread類所提供的基礎(chǔ)線程功能,分別從Thread對(duì)象的創(chuàng)建,新線程的啟動(dòng)與終止,線程阻塞權(quán)限以及線程阻塞相關(guān)的函數(shù)SleepMs、Join,獲取當(dāng)前線程、如何判斷代碼是否在當(dāng)前線程中執(zhí)行,線程的Wrap等幾個(gè)方面進(jìn)行了分析。以下是需要再重點(diǎn)回顧的幾個(gè)點(diǎn):

  • Thread類中線程相關(guān)的功能是在封裝了平臺(tái)相關(guān)API的基礎(chǔ)上實(shí)現(xiàn)的,在Windows系統(tǒng)上是通過(guò)Windows API提供,在類Unix系統(tǒng)上是通過(guò)pthread庫(kù)提供。比如Win上創(chuàng)建線程CreateThread(),類Unix上相應(yīng)為pthread_create();Win上休眠Sleep(),類Unix上是nanosleep()系統(tǒng)調(diào)用,非pthread庫(kù)功能;Win上線程join使用WaitForSingleObject(),類Unix上使用pthread庫(kù)的pthread_join();
  • Thread對(duì)象的構(gòu)造時(shí)有個(gè)需要注意的點(diǎn),那就是DoInit()方法需要Thread構(gòu)造函數(shù)中被調(diào)用,否則會(huì)引發(fā)競(jìng)太條件。
  • Thread.Start()方法以及Thread.PreRun()方法是本文重點(diǎn),是WebRTC中啟動(dòng)線程的標(biāo)準(zhǔn)流程。
  • Thread類提供了線程是否允許阻塞的權(quán)限設(shè)置,由成員Thread.blocking_calls_allowed_來(lái)控制,Thread中引發(fā)線程阻塞的函數(shù),如SleepMs,Join,Send等都會(huì)先進(jìn)行權(quán)限檢查,要么打印警告日志,要么觸發(fā)斷言。
  • Thread類的Wrap函數(shù)族提供了非標(biāo)準(zhǔn)方式啟動(dòng)的線程納入ThreadManager管理的額外方式。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容