From 2a318149f8d5094c82306b8091a7a8b5194bf9c1 Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Tue, 7 Jan 2020 18:08:24 +0000
Subject: [PATCH] Add support for BSD systems

webrtc/rtc_base/checks.cc:158:28: error: use of undeclared identifier 'LAST_SYSTEM_ERROR'
  158 |                file, line, LAST_SYSTEM_ERROR, message);
      |                            ^
webrtc/rtc_base/checks.cc:220:16: error: use of undeclared identifier 'LAST_SYSTEM_ERROR'
  220 |                LAST_SYSTEM_ERROR);
      |                ^
In file included from webrtc/rtc_base/platform_thread_types.cc:11:
webrtc/rtc_base/platform_thread_types.h:47:1: error: unknown type name 'PlatformThreadId'
   47 | PlatformThreadId CurrentThreadId();
      | ^
webrtc/rtc_base/platform_thread_types.h:52:1: error: unknown type name 'PlatformThreadRef'
   52 | PlatformThreadRef CurrentThreadRef();
      | ^
webrtc/rtc_base/platform_thread_types.h:55:29: error: unknown type name 'PlatformThreadRef'
   55 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
      |                             ^
webrtc/rtc_base/platform_thread_types.h:55:57: error: unknown type name 'PlatformThreadRef'
   55 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
      |                                                         ^
webrtc/rtc_base/platform_thread_types.cc:37:1: error: unknown type name 'PlatformThreadId'
   37 | PlatformThreadId CurrentThreadId() {
      | ^
webrtc/rtc_base/platform_thread_types.cc:58:1: error: unknown type name 'PlatformThreadRef'
   58 | PlatformThreadRef CurrentThreadRef() {
      | ^
webrtc/rtc_base/platform_thread_types.cc:68:29: error: unknown type name 'PlatformThreadRef'
   68 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) {
      |                             ^
webrtc/rtc_base/platform_thread_types.cc:68:57: error: unknown type name 'PlatformThreadRef'
   68 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) {
      |                                                         ^
In file included from webrtc/rtc_base/event_tracer.cc:30:
In file included from webrtc/api/sequence_checker.h:15:
In file included from webrtc/rtc_base/synchronization/sequence_checker_internal.h:18:
webrtc/rtc_base/synchronization/mutex.h:28:2: error: Unsupported platform.
   28 | #error Unsupported platform.
      |  ^
webrtc/rtc_base/synchronization/mutex.h:52:3: error: unknown type name 'MutexImpl'
   52 |   MutexImpl impl_;
      |   ^
---
 meson.build                              |  5 +++++
 webrtc/rtc_base/platform_thread_types.cc | 16 ++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/meson.build b/meson.build
index 8d85d56..05a434a 100644
--- a/meson.build
+++ b/meson.build
@@ -87,6 +87,11 @@ elif host_system == 'linux'
   os_deps += [cc.find_library('rt', required : false)]
   os_deps += [dependency('threads')]
   have_posix = true
+elif (host_system == 'dragonfly' or host_system == 'freebsd' or
+      host_system == 'netbsd' or host_system == 'openbsd')
+  os_cflags += ['-DWEBRTC_BSD', '-DWEBRTC_THREAD_RR']
+  os_deps += [dependency('threads')]
+  have_posix = true
 elif host_system == 'windows'
   platform_cflags += ['-DWEBRTC_WIN', '-D_WIN32']
   # this one is for MinGW to get format specifiers from inttypes.h in C++
diff --git a/webrtc/rtc_base/platform_thread_types.cc b/webrtc/rtc_base/platform_thread_types.cc
index d64ea68..e98e8ec 100644
--- a/webrtc/rtc_base/platform_thread_types.cc
+++ b/webrtc/rtc_base/platform_thread_types.cc
@@ -15,6 +15,12 @@
 #include <sys/syscall.h>
 #endif
 
+#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) // WEBRTC_BSD
+#include <pthread_np.h>
+#elif defined(__NetBSD__) // WEBRTC_BSD
+#include <lwp.h>
+#endif
+
 #if defined(WEBRTC_WIN)
 #include "rtc_base/arraysize.h"
 
@@ -46,6 +52,12 @@ PlatformThreadId CurrentThreadId() {
   return zx_thread_self();
 #elif defined(WEBRTC_LINUX)
   return syscall(__NR_gettid);
+#elif defined(__DragonFly__) || defined(__FreeBSD__) // WEBRTC_BSD
+  return pthread_getthreadid_np();
+#elif defined(__NetBSD__) // WEBRTC_BSD
+  return _lwp_self();
+#elif defined(__OpenBSD__) // WEBRTC_BSD
+  return getthrid();
 #elif defined(__EMSCRIPTEN__)
   return static_cast<PlatformThreadId>(pthread_self());
 #else
@@ -116,6 +128,10 @@ void SetCurrentThreadName(const char* name) {
   prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name));  // NOLINT
 #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
   pthread_setname_np(name);
+#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) // WEBRTC_BSD
+  pthread_set_name_np(pthread_self(), name);
+#elif defined(__NetBSD__) // WEBRTC_BSD
+  pthread_setname_np(pthread_self(), "%s", (void*)name);
 #elif defined(WEBRTC_FUCHSIA)
   zx_status_t status = zx_object_set_property(zx_thread_self(), ZX_PROP_NAME,
                                               name, strlen(name));
-- 
2.47.1

