diff options
author | Gennadiy Civil <misterg@google.com> | 2019-02-13 19:07:32 (GMT) |
---|---|---|
committer | Gennadiy Civil <misterg@google.com> | 2019-02-13 19:07:33 (GMT) |
commit | 54ec41f000189e89c5cfa8b115c6512cfdc0a21b (patch) | |
tree | a699730ce7bb9c56c62a8e78af3c0ae8d91509ee /googletest/src | |
parent | f73b2fb39efcac92d3e9fe079e4f7c809baa975e (diff) | |
parent | 96826743ea0addf96d84a28497c000999e9c1da9 (diff) | |
download | googletest-54ec41f000189e89c5cfa8b115c6512cfdc0a21b.zip googletest-54ec41f000189e89c5cfa8b115c6512cfdc0a21b.tar.gz googletest-54ec41f000189e89c5cfa8b115c6512cfdc0a21b.tar.bz2 |
Merge pull request #2121 from ngie-eign:add-dragonflybsd-and-kfreebsd-support
PiperOrigin-RevId: 233789488
Diffstat (limited to 'googletest/src')
-rw-r--r-- | googletest/src/gtest-port.cc | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index 989778c..ca7136a 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -55,6 +55,14 @@ # include <mach/vm_map.h> #endif // GTEST_OS_MAC +#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ + GTEST_OS_NETBSD || GTEST_OS_OPENBSD +# include <sys/sysctl.h> +# if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD +# include <sys/user.h> +# endif +#endif + #if GTEST_OS_QNX # include <devctl.h> # include <fcntl.h> @@ -131,6 +139,81 @@ size_t GetThreadCount() { } } +#elif GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ + GTEST_OS_NETBSD + +#if GTEST_OS_NETBSD +#undef KERN_PROC +#define KERN_PROC KERN_PROC2 +#define kinfo_proc kinfo_proc2 +#endif + +#if GTEST_OS_DRAGONFLY +#define KP_NLWP(kp) (kp.kp_nthreads) +#elif GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD +#define KP_NLWP(kp) (kp.ki_numthreads) +#elif GTEST_OS_NETBSD +#define KP_NLWP(kp) (kp.p_nlwps) +#endif + +// Returns the number of threads running in the process, or 0 to indicate that +// we cannot detect it. +size_t GetThreadCount() { + int mib[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PID, + getpid(), +#if GTEST_OS_NETBSD + sizeof(struct kinfo_proc), + 1, +#endif + }; + u_int miblen = sizeof(mib) / sizeof(mib[0]); + struct kinfo_proc info; + size_t size = sizeof(info); + if (sysctl(mib, miblen, &info, &size, NULL, 0)) { + return 0; + } + return KP_NLWP(info); +} +#elif GTEST_OS_OPENBSD + +// Returns the number of threads running in the process, or 0 to indicate that +// we cannot detect it. +size_t GetThreadCount() { + int mib[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PID | KERN_PROC_SHOW_THREADS, + getpid(), + sizeof(struct kinfo_proc), + 0, + }; + u_int miblen = sizeof(mib) / sizeof(mib[0]); + + // get number of structs + size_t size; + if (sysctl(mib, miblen, NULL, &size, NULL, 0)) { + return 0; + } + mib[5] = size / mib[4]; + + // populate array of structs + struct kinfo_proc info[mib[5]]; + if (sysctl(mib, miblen, &info, &size, NULL, 0)) { + return 0; + } + + // exclude empty members + int nthreads = 0; + for (int i = 0; i < size / mib[4]; i++) { + if (info[i].p_tid != -1) + nthreads++; + } + return nthreads; +} + #elif GTEST_OS_QNX // Returns the number of threads running in the process, or 0 to indicate that |