WebRTC-线程模型(1)

来源:互联网 发布:蜗居宋思明知乎 编辑:程序博客网 时间:2024/05/21 10:13

在介绍WebRTC的线程模型之前,先介绍webrtc线程模型中用到的几个简单、常用的模块或函数。webrtc是一个代码宝库,且它本身跨平台(windows,linux,ios,android),不管是哪个平台上面开发,都可以从中学习到很多有用的东西。

一、 设置线程名

platform_thread.cc

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) {  }#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)  prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name));#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)  pthread_setname_np(name);#endif}

调用示例 thread.cc Thread::SetName

bool Thread::SetName(const std::string& name, const void* obj) {  if (running()) return false;  name_ = name;  if (obj) {    char buf[20];    sprintfn(buf, sizeof(buf), " 0x%p", obj);    name_ += buf;  }  return true;}

线程名

二、 原子操作

atomicops.h

class AtomicOps { public:#if defined(WEBRTC_WIN)  // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.  static int Increment(volatile int* i) {    return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));  }  static int Decrement(volatile int* i) {    return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));  }  static int AcquireLoad(volatile const int* i) {    return *i;  }  static void ReleaseStore(volatile int* i, int value) {    *i = value;  }  static int CompareAndSwap(volatile int* i, int old_value, int new_value) {    return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i),                                        new_value,                                        old_value);  }  // Pointer variants.  template <typename T>  static T* AcquireLoadPtr(T* volatile* ptr) {    return *ptr;  }  template <typename T>  static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) {    return static_cast<T*>(::InterlockedCompareExchangePointer(        reinterpret_cast<PVOID volatile*>(ptr), new_value, old_value));  }#else  static int Increment(volatile int* i) {    return __sync_add_and_fetch(i, 1);  }  static int Decrement(volatile int* i) {    return __sync_sub_and_fetch(i, 1);  }  static int AcquireLoad(volatile const int* i) {    return __atomic_load_n(i, __ATOMIC_ACQUIRE);  }  static void ReleaseStore(volatile int* i, int value) {    __atomic_store_n(i, value, __ATOMIC_RELEASE);  }  static int CompareAndSwap(volatile int* i, int old_value, int new_value) {    return __sync_val_compare_and_swap(i, old_value, new_value);  }  // Pointer variants.  template <typename T>  static T* AcquireLoadPtr(T* volatile* ptr) {    return __atomic_load_n(ptr, __ATOMIC_ACQUIRE);  }  template <typename T>  static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) {    return __sync_val_compare_and_swap(ptr, old_value, new_value);  }#endif};

使用示例

bool MessageQueue::IsQuitting() {  return AtomicOps::AcquireLoad(&stop_) != 0;}
原创粉丝点击