Skip to content

build(windows): remove devdeps-pthreads dependency, adapt internal sync to std/Win32 - #2792

Draft
hnwyllmm wants to merge 1 commit into
masterfrom
windows/remove-devdeps-pthreads
Draft

hnwyllmm wants to merge 1 commit into
masterfrom
windows/remove-devdeps-pthreads

Conversation

@hnwyllmm

@hnwyllmm hnwyllmm commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

任务描述

Windows 构建目前依赖第三方包 devdeps-pthreads(PThreads4W / pthreads-win32)为 oblib 提供 pthread API。该包在服务进程里引入了独立的 pthread 实现层:其内部对象基于内核句柄(CreateEvent 等),在内核对象配额耗尽时 pthread_mutex_init/pthread_cond_init 返回 ENOSPC,若未处理会在 pthreadVC3.dll 内部空指针解引用形成 AccessViolation(此前的快速失败补丁只是把崩溃点提前)。本 PR 把 seekdb 引擎 Windows 构建对 devdeps-pthreads 的依赖整个移除:先把所有内部 pthread 使用点适配为 C++ 标准库 / Windows 原生 API(不引入仓库内 pthread shim),最后删除 deps/init/oceanbase.windows.x86_64.deps 中的 devdeps-pthreads-20260506.tar.gz 行。

关联工单:暂无 SEEK 编号(draft 阶段,如需挂工单请补充)。不关联 issue。

解决方案

全部改动位于 _WIN32 / __APPLE__ 平台分支,POSIX 编译路径不动。不采用"把 pthread 移植进来"的 shim 方案(用户明确否决),理由:shim 会继续引入第二套同步语义与 pthreads4w 的内核对象消耗,而 std/Win32 原语覆盖了全部实际使用点。

逐项对应:

  • 线程创建(lib/thread/thread.cpp/.h、ob_common_utility.cpp 启动栈探测线程):Windows 用 _beginthreadex(UCRT 接口,微软推荐),配 unsigned __stdcall 桩 th_start_win,保持 pthread_create 的错误码语义(不用会抛异常的 std::thread 构造)。线程句柄 pth_ 完全私有化,不外露;wait()/try_wait()/destroy() 改 WaitForSingleObject + CloseHandle,join 失败 ob_abort() 语义保留。删除无调用者的死代码 get_pthread()(threads.h)与 ob_pthread_get_pth(ob_pthread.cpp)。SMART_CALL 栈检查不受影响(GetCurrentThreadStackLimits 查询真实栈界)。QoS 仅 macOS 需要(ob_pthread_attr_set_qos 包 #ifndef _WIN32)。
  • 互斥/条件(obutil::ObUtilMutex、obutil::Cond、ObThreadCond、ob_queue_thread ObCond、ob_multi_version_schema_service ObSchemaConstructTask):Windows 成员改 std::mutex / std::condition_variable。关键语义:pthread 的"返回时仍持锁"通过 std::unique_lock(adopt_lock) + (void)ul.release() 保持(ObUtilMutex::unlock(LockState&) 只记录指针不解锁)。pthreads4w 的 ENOSPC fast-fail 分支随 std 类型自然消失(std::mutex::lock 无错误路径)。ObThreadCond::init/destroy 只翻生命周期标志位,复用语义保留。
  • 读写锁(ob_rwlock.h/.cpp):Windows 用 std::shared_mutex(MSVC 上即 SRWLOCK)。此前已核查全部 ObRWLock 使用者无递归读锁、读写不重叠,shared_mutex 语义安全;pthreads4w rwlock 死锁/EINVAL 问题的绕行注释随之收束。
  • 自旋锁(ob_platform_utils.h):Windows 上 ob_pthread_spinlock_t 定义为 std::atomic<bool> TATAS 真自旋类(不用 atomic_flag::test,它要求 C++20;atomic<bool> 兼容 C++17 且在 x64 上 lock-free)。ob_pthread_spin_* helper 接口三平台统一,现有别名 ob_pthread_spinlock_t 保留不改名。
  • hash 同步层(ob_hashutils.h):MutexLocker/MutexIniter/MutexWaiter/MutexBroadCaster、ReadLocker/WriteLocker/RWLockIniter 及全部 DefendMode 策略结构体补 Windows std 类型分支;MutexWaiter 把调用方传入的绝对 epoch-µs timespec 转回相对 wait_for(steady_clock),调用点(ob_hashtable 超时 get)不变;init_hash_sync/destroy_hash_sync 的 pthread 重载包 #if !defined(_WIN32),std 类型走泛型 fallback。
  • 可移植定时等待(ob_thread_hook.cpp + ob_define.h):ob_pthread_cond_timedwait_us 提供双签名——Windows 重载为 std::condition_variable*/std::mutex*(steady_clock 相对等待),POSIX 保持 extern "C" pthread 签名;调用点(cond.h timed_wait、schema service)名字不变。
  • 线程局部存储(ob_tsi_utils.cpp):pthread_key_t → Windows FlsAlloc(带线程退出回调;TlsAlloc 无回调),itid 回收语义保留。
  • 信号处理(ob_signal_handle.h/.cpp):pthread_t thread_id_ 成员与 pthread_self() 赋值限 __APPLE__(仅为 macOS sigwait 唤醒)。
  • 杂项:ob_dedup_queue ThreadMeta 的 pthread_t pthread_id_ 仅用于 to_string 打印,Windows 改存 GetCurrentThreadId();ob_print_utils.h 的 is_pthread_like 在 Windows 恒 false;utility.cpp hexdump 的线程号 Windows 用 GetCurrentThreadId();Windows 存根的 pthread_sigmask(ob_signal_handle.h 内)与新结构无冲突。
  • 清理:不再被 Windows 编译需要的 #include <pthread.h> 包 #ifndef _WIN32(hash 三件套、fifo_allocator、spin_lock/spin_rwlock、reentrant_thread、list、warning_buffer、trace_id、timer_service、clock_generator、ob_th_worker、ob_mysql_global 等,逐文件核实过体内确无其他 pthread 符号);ob_mutex.h 里全仓零调用的 ObMutex0/proxy mutex helper 段在 Windows 隐藏(保留 POSIX 原样)。
  • deps:删除 deps/init/oceanbase.windows.x86_64.deps 中 devdeps-pthreads-20260506.tar.gz 一行(BOM 保留,diff 仅 1 deletion)。

考虑过并否决的替代方案:pthread shim(见上);std::thread(抛异常构造 + 无法保持错误码返回语义);自旋锁映射为 mutex(用户确认 Windows 用 std::atomic 更忠实,macOS libc 无 pthread_spin 的问题由既有 mutex 别名分支处理,不属于本 PR)。

验证

验证范围与结果,如实区分:

  • 语法与类型检查(当前重新验证,macOS / Apple clang 17):对全部 43 个改动文件中可独立编译的 29 个头/TU 跑 clang++ -std=gnu++17 -fsyntax-only(含 ob_hashutils.h、mutex/cond/rwlock/thread_cond、thread.h/threads.h/thread.cpp、ob_queue_thread、ob_tsi_utils、ob_dedup_queue、ob_print_utils、ob_platform_utils、ob_common_utility 等)。缺 cbindgen 生成头(rust/logger.h 等)的文件用最小桩头补齐后通过——桩只声明符号、不参与被检代码,被检区域不涉及桩实现。
  • 模板实例化检查(当前重新验证,macOS):实例化 harness 强制实例化 5 种 DefendMode(LatchReadWrite/ReadWrite/NoPthread/MultiWrite/SpinMutex)的 ObHashMap/ObHashSet 并执行 create/set/get/erase/destroy,类型检查通过。这覆盖了本次改动最容易错的 Locker/Waiter/BroadCaster 管线(过程中抓出并修复了 2 个真实错误:ob_define.h 悬空 #else、WriteLocker 类头缺失)。
  • 既有失败(非本次引入):ob_thread_hook.cpp、utility.cpp 在 macOS 单文件编译报缺符号(生成头依赖),用未修改的 master 基线同样编译失败逐字对照确认,属既有失败。
  • 本地未验证项:ob_multi_version_schema_service.h 及 observer 侧文件因缺生成头无法本地全量编译(改动块做了片段级检查);Windows 分支完全未编译——本机无 MSVC/clang-cl/SDK,本 PR 以 draft 状态主要就是为了跑 Windows CI 把关。
  • 验证对象与 PR head 的对应关系:本地验证的提交与 PR head 由同一 tree 构成(tree OID 一致 0a0f9d0e…,因本地 git push 传输失败,远端提交经 GitHub Git Data API 按 blob→tree→commit 逐字节重建);本地 macOS 检查结论对 PR head 成立。
  • POSIX 路径零改动声明:除 include 行包裹与两处死代码注释外,POSIX 分支语句未动;macOS 语法检查 + 实例化检查即为对"未破坏 POSIX"的直接验证。Linux 侧共享同一 POSIX 分支,本次未在 Linux 实测(推断依据:POSIX 分支无改动)。
展开详细复现与验证记录
  1. 基线:本地克隆 master 4031d847([SEEK-1513]),40 个待改文件与 HEAD 逐字节校验一致后开始编辑。
  2. 环境:macOS(Apple clang 17.0.0,-std=gnu++17);rust/*.h 与 share/inner_table/ob_inner_table_schema.h 为构建期生成件不在工作树,语法检查用最小桩头(.cache-stubs/rust/*.h,仅声明 ob_log.h/ob_string.h 链路用到的 logger::、compression::Code、crypto::certificate_expiry 符号,签名从调用点反推)。
  3. 每轮编辑后重跑受影响文件检查;ob_define.h 悬空 #else、WriteLocker 类头缺失两个错误由该检查暴露并修复后全绿。
  4. 实例化 harness(.cache-stubs/hash_instantiate.cpp):include ob_hashmap/hashset/hashtable,实例化五种 DefendMode 容器并调用 create/set_refactored/get_refactored/erase_refactored/destroy,clang++ -fsyntax-only 通过。
  5. 基线对照:git show HEAD:… 导出 ob_thread_hook.cpp、utility.cpp 原文件同参数编译,报错与改动后一致(pthread_cond_timedwait_relative_np/errno 未声明、ObPlatformDir 未声明),判定既有失败。
  6. 未执行:Windows 编译、Windows 单元测试、seekdb_test 下游集成测试、dumpbin /dependents 检查。
  • seekdb_test 验证:待验证。原因:本机无 Windows 工具链,无法构建可运行 seekdb_test 的 Windows 产物;且本次改动不触碰 POSIX 行为,Linux/macOS 下游用例的既有断言不因本 PR 改变(POSIX 分支零改动)。剩余验证项:Windows CI 编译 + Windows 侧 seekdb_test;用例本身预计无需更新(接口名与调用点均未变),如 Windows CI 暴露行为差异再补充用例。

影响范围

  • Windows:本次改动的目标平台。依赖删除 + 全部使用点适配已完成,但编译与运行均未实测(无本地工具链),由本 PR 的 Windows CI 首次验证。第三方预编译包(boost/vsag/libcurl)是否隐式依赖 pthreadVC3.dll 尚未核查——CI 通过后建议跑 dumpbin /dependents seekdb.exe 确认;若有隐式依赖需另行处理(与本 PR 的源码改动无关)。
  • Linux:共享 POSIX 源码路径,路径零改动,未在 Linux 实测(源码共用证明路径共用,不证明测试结论共用;Linux 构建由仓库 CI 常规门禁覆盖)。
  • macOS:POSIX 路径零改动,另做了本 PR 中的语法/实例化检查(见验证节);未做完整构建。
  • 基线 SHA:master 4031d847;无版本迁移/兼容面(纯构建依赖与内部实现替换,外部 ABI 接口名不变)。

合入理由

  • 真实影响:pthreads4w 在内核对象耗尽场景下存在 DLL 内空指针崩溃路径(此前已有 fast-fail 补丁缓解但根因仍在第三方包内);移除该 devdep 后 Windows 构建不再携带 pthreadVC3.dll,少一个需随发布维护的第三方包,也消除对应崩溃面。
  • 改动边界清晰:全部在 _WIN32/__APPLE__ 分支,POSIX 路径零改动,接口名与调用点不变(ob_pthread_cond_timedwait_us 双签名、ob_pthread_spin_* 三平台统一),回归面集中在 Windows 构建本身。
  • 已知回归与验证边界(如实):Windows 编译/运行未验证,依赖 CI;std::condition_variable 相对等待替代 pthreads4w 绝对时间等待的时钟语义差异(wall-clock → steady_clock)在源码层推导为更安全(免墙上时钟跳变),但无 Windows 实测证据;hash 桶 MutexWaiter 的绝对→相对换算在调用方循环外层本就有 get_cur_microseconds_time() > abs_timeout_us 的墙钟超时判断,行为边界保持。
  • 以 draft 提交即为让 Windows CI 与团队成员先行评审,非请求立即合入。

其他信息

  • 配套改动:seekdb-bindings(C SDK)仓库的 find_package(Threads)/Threads::Threads Windows 移除已在本 PR 之前单独完成,不在本 PR 范围。
  • 验证用桩与脚本(本地辅助产物,不随 PR 提交):.cache-stubs/ 下 rust 头桩、hash 实例化 harness、API 推送脚本。
  • 遗留讨论:PR 标题是否需挂 SEEK 工单号(当前按仓库近期 PR 惯例用 conventional commit 风格);如需可在标题前补 [SEEK-xxx]。

…td/Win32

Remove the PThreads4W (pthreads-win32) devdep from the Windows build of
the engine and adapt every internal pthread usage point to C++ standard
library / Win32 primitives instead of vendoring a pthread shim.

Platform branches only (_WIN32); POSIX compile paths are unchanged.

- lib/thread: Thread uses _beginthreadex (UCRT) with pthread_create-style
  error codes; thread handle (pth_) is private, wait/try_wait/destroy use
  WaitForSingleObject/CloseHandle; dead get_pthread()/ob_pthread_get_pth
  removed.
- obutil Mutex/Cond: std::mutex / std::condition_variable on Windows
  (pthreads4w ENOSPC fast-fail branches disappear with it); wait keeps
  the pthread 'still locked on return' contract via adopt_lock+release.
- ob_rwlock: std::shared_mutex (SRWLOCK) on Windows; recursive-read-free
  usage verified.
- ob_thread_cond: std::mutex/std::condition_variable members, init/
  destroy only flip lifecycle flags.
- ob_hashutils: Locker/Initer/Waiter/BroadCaster and all DefendModes get
  Windows std-type variants; MutexWaiter converts the absolute epoch-us
  timespec to a relative wait_for.
- ob_platform_utils: ob_pthread_spinlock_t on Windows is a true spinlock
  (std::atomic<bool> TATAS); the ob_pthread_spin_* helpers stay uniform
  across the three platforms; QoS helpers hidden on Windows.
- ob_common_utility: startup stack probe thread uses _beginthreadex.
- ob_thread_hook/ob_define: ob_pthread_cond_timedwait_us gets a Windows
  overload on std types (steady_clock wait_for), same call-site name.
- ob_tsi_utils: pthread_key -> FlsAlloc (thread-exit callback preserved).
- ob_signal_handle: pthread_t thread_id_ limited to __APPLE__.
- ob_queue_thread/ob_dedup_queue/ob_print_utils/utility: std-based or
  native thread ids on Windows.
- cleanup: leftover #include <pthread.h> guarded for !_WIN32; unused
  ObMutex0/proxy mutex helpers hidden on Windows.
- deps: drop devdeps-pthreads-20260506.tar.gz from oceanbase.windows deps.

Verified: clang -fsyntax-only over all changed headers/TUs on POSIX plus
forced instantiation of hash containers for every DefendMode; Windows
compile to be validated by CI.
@hnwyllmm hnwyllmm changed the title windows: remove devdeps-pthreads dependency, adapt internal sync to std/Win32 build(windows): remove devdeps-pthreads dependency, adapt internal sync to std/Win32 Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant