You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "tuaris (via GitHub)" <gi...@apache.org> on 2026/02/23 04:09:49 UTC
[PR] feat: improve FreeBSD platform support (brpc)
tuaris opened a new pull request, #3224:
URL: https://github.com/apache/brpc/pull/3224
### What problem does this PR solve?
Issue Number: N/A
Problem Summary:
brpc has partial FreeBSD support (27 source files already contain `OS_FREEBSD`/`__FreeBSD__` references), but it does not compile or run on FreeBSD. This PR completes that support.
The most critical issue is a **static initialization deadlock**: bthread interposes `pthread_mutex_lock` for contention profiling. On FreeBSD, during early process init, `libc++.so`'s `ios_base::Init::Init()` calls `pthread_mutex_lock` to initialize `std::locale::classic()`. bthread's interposed version calls `pthread_once(init_sys_mutex_lock)`, which itself needs `pthread_mutex_lock` — causing infinite recursion. Without the `NO_PTHREAD_MUTEX_HOOK` fix in this PR, **any FreeBSD application linking brpc will deadlock at startup**.
This is PR 2 of 3 for FreeBSD support:
1. PR #XXXX — Remove dead `tracked_objects.h` references
2. **This PR** — FreeBSD platform support
3. PR 3 (upcoming) — FreeBSD CI workflow via GitHub Actions
### What is changed and the side effects?
Changed:
**All source changes are guarded by `#if defined(OS_FREEBSD)` — zero impact on Linux or macOS builds.** Most changes piggyback on existing macOS/Darwin code paths since both use kqueue.
CMakeLists.txt:
- Add `NO_PTHREAD_MUTEX_HOOK` on FreeBSD to prevent static init deadlock caused by bthread's `pthread_mutex_lock` interposition conflicting with `libc++` `ios_base::Init` during early process initialization
- Add `-lexecinfo` for backtrace support
- Add FreeBSD-specific source files (`platform_thread_freebsd.cc`, `sys_string_conversions_posix.cc`)
Source changes:
- `butil/compat.h`: Add FreeBSD kqueue path and `pthread_getthreadid_np()`
- `butil/process_util.cc`: FreeBSD `ReadCommandLine` via `ps` (like macOS)
- `butil/fd_utility.cpp`: kqueue fd handling
- `butil/logging.cc`: Rename `FLAGS_v`/`vmodule`/`minloglevel` to avoid gflags duplicate registration with system glog on FreeBSD
- `brpc/socket.cpp`: FreeBSD `TCP_INFO` support
- `bthread/`: FreeBSD futex emulation, context switching, fd handling
- `bvar/default_variables.cpp`: FreeBSD `/proc` fallback
Side effects:
- Performance effects:
None — all changes are behind `OS_FREEBSD` guards and do not affect existing platforms.
- Breaking backward compatibility:
None — this is additive FreeBSD support only.
---
### Check List:
- Please make sure your changes are compatible.
- When providing us with a new feature, it is best to add related tests.
- Please follow [Contributor Covenant Code of Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: improve FreeBSD platform support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3224:
URL: https://github.com/apache/brpc/pull/3224#discussion_r2903737651
##########
src/butil/logging.cc:
##########
@@ -129,22 +129,41 @@ DEFINE_bool(print_stack_on_check, true,
"Print the stack trace when a CHECK was failed");
BUTIL_VALIDATE_GFLAG(print_stack_on_check, butil::PassValidate);
+#if defined(OS_FREEBSD)
+// Rename flags to avoid gflags duplicate registration with system glog.
+// glog defines "v" and "vmodule"; we define "brpc_verbose" and "brpc_vmodule"
+// and create local aliases so brpc code can still use FLAGS_v / FLAGS_vmodule.
+DEFINE_int32(brpc_verbose, 0, "Show all VLOG(m) messages for m <= this."
+ " Overridable by --brpc_vmodule.");
+DEFINE_string(brpc_vmodule, "", "per-module verbose level.");
+#define FLAGS_v FLAGS_brpc_verbose
+#define FLAGS_vmodule FLAGS_brpc_vmodule
+#else
DEFINE_int32(v, 0, "Show all VLOG(m) messages for m <= this."
" Overridable by --vmodule.");
DEFINE_string(vmodule, "", "per-module verbose level."
" Argument is a comma-separated list of MODULE_NAME=LOG_LEVEL."
" MODULE_NAME is a glob pattern, matched against the filename base"
" (that is, name ignoring .cpp/.h)."
" LOG_LEVEL overrides any value given by --v.");
+#endif
DEFINE_bool(log_pid, false, "Log process id");
DEFINE_bool(log_bid, true, "Log bthread id");
+#if defined(OS_FREEBSD)
+DEFINE_int32(brpc_minloglevel, 0, "Any log at or above this level will be "
+ "displayed. Anything below this level will be silently ignored. "
+ "0=INFO 1=NOTICE 2=WARNING 3=ERROR 4=FATAL");
+BUTIL_VALIDATE_GFLAG(brpc_minloglevel, butil::NonNegativeInteger);
+#define FLAGS_minloglevel FLAGS_brpc_minloglevel
+#else
DEFINE_int32(minloglevel, 0, "Any log at or above this level will be "
"displayed. Anything below this level will be silently ignored. "
"0=INFO 1=NOTICE 2=WARNING 3=ERROR 4=FATAL");
BUTIL_VALIDATE_GFLAG(minloglevel, butil::NonNegativeInteger);
+#endif
Review Comment:
On FreeBSD this file renames the gflags from `v`/`vmodule`/`minloglevel` to `brpc_*` and only adds `#define FLAGS_v ...` aliases in this .cc file. However `src/butil/logging.h` still has `DECLARE_int32(v);` and uses `::logging::FLAGS_v` (and the code registers validators against `FLAGS_vmodule`), so other translation units will still expect the `logging::FLAGS_v`/`FLAGS_vmodule`/`FLAGS_minloglevel` symbols to exist and will fail to link on FreeBSD. The aliases need to be made visible to all users (e.g., add corresponding `#if defined(OS_FREEBSD)` mapping and `DECLARE_*` updates in `logging.h`, and adjust any flag-name based code/tests as needed).
##########
CMakeLists.txt:
##########
@@ -147,6 +147,11 @@ endif()
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__=__unused__ -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DBRPC_REVISION=\\\"${BRPC_REVISION}\\\" -D__STRICT_ANSI__")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEBUG_SYMBOL} ${THRIFT_CPP_FLAG}")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} ${CMAKE_CXX_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer")
+
+if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNO_PTHREAD_MUTEX_HOOK")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_PTHREAD_MUTEX_HOOK")
Review Comment:
The FreeBSD `-DNO_PTHREAD_MUTEX_HOOK` addition to `CMAKE_C_FLAGS` is effectively lost because `CMAKE_C_FLAGS` is re-assigned unconditionally a few lines later (line 155), overwriting the earlier append (line 153). If this define is intended for all C/C++ compilation, add it to `CMAKE_CPP_FLAGS` (so both C and CXX inherit it) or move the FreeBSD block after the final `set(CMAKE_C_FLAGS ...)` assignment.
```suggestion
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DNO_PTHREAD_MUTEX_HOOK")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: improve FreeBSD platform support (brpc)
Posted by "Huixxi (via GitHub)" <gi...@apache.org>.
Huixxi commented on PR #3224:
URL: https://github.com/apache/brpc/pull/3224#issuecomment-4019143982
LGTM
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org