apache / brpc

brpc is an Industrial-grade RPC framework using C++ Language, which is often used in high performance system such as Search, Storage, Machine learning, Advertisement, Recommendation etc. "brpc" means "better RPC".
https://brpc.apache.org
Apache License 2.0
16.42k stars 3.96k forks source link

多线程池与butex_wake_*的批量唤醒有冲突 #2777

Open yanglimingcn opened 2 hours ago

yanglimingcn commented 2 hours ago

Describe the bug (描述bug) 最初提过一个butexwake*支持批量唤醒的功能,目的是为了减少锁唤醒的频率。之后又提交了一个给bRPC的worker线程池分组的功能。但是目前bthread在不同分组里面访问共享资源做互斥的时候,如果使用批量唤醒的功能就会产生bug,举例来说: 分组1的bthread1 和 分组2的bthread2共同访问一个互斥锁 mutex。 bthread1获得锁,bthread2在等待锁。bthread1使用完互斥锁要唤醒bthread2,但是bthread1如果使用批量唤醒(即使用bthread_flush来唤醒)的时候就会有问题,因为bthread_flush运行在bthread1的上下文,所以它只能唤醒bthread1所在的分组1等待锁的bthread或者tls_task_group_nosignal所在分组等待的锁,不能唤醒其它分组等待锁的bthread。

现在想到的方案是: 在butexwake*的时候,会判断等待的bthread所在的分组,如果分组不是当前分组或者tls_task_group_nosignal所在的分组,就直接唤醒这些bthread,也就是跨分组不支持批量唤醒。 本身批量唤醒是为了提高性能,在组内使用比较符合预期,正常使用分组是为了做隔离,组之间的互斥应该是比较低频的,负责性能本身也不会太好。因此组间的批量唤醒不支持也没啥问题。

To Reproduce (复现方法)

Expected behavior (期望行为) 期望批量唤醒的功能正常。

Versions (各种版本) OS: Compiler: brpc: protobuf:

Additional context/screenshots (更多上下文/截图)

yanglimingcn commented 2 hours ago

@wwbmmm 这块你看有啥建议吗?