You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "chenBright (via GitHub)" <gi...@apache.org> on 2024/01/20 08:57:12 UTC
[PR] Support bthread_usleep to wait for Singleton in bthread (brpc)
chenBright opened a new pull request, #2517:
URL: https://github.com/apache/brpc/pull/2517
### What problem does this PR solve?
Issue Number:
Problem Summary:
对于::Singleton<T>,在T构造的时候,并发调用`get()`,只有一个线程会调用T的构造函数,其他线程会在`WaitForInstance`函数中等待(自旋+`sched_yield`)T完成构造。如果T构造函数中有挂起bthrea的操作,并发大的情况下,有可能会导致“死锁”问题:所有worker线程都在`WaitForInstance`函数中等待T完成构造,而此时已经没有worker可以调度执行T构造函数的bthread了,即T已经无法完成构造。
### What is changed and the side effects?
Changed:
在链接了bthread时,使用bthread_yield,这样在bthread上使用的:Singleton<T>就不会阻塞所有worker线程了。
Side effects:
- Performance effects(性能影响):
- Breaking backward compatibility(向后兼容性):
---
### Check List:
- Please make sure your changes are compilable(请确保你的更改可以通过编译).
- When providing us with a new feature, it is best to add related tests(如果你向我们增加一个新的功能, 请添加相关测试).
- Please follow [Contributor Covenant Code of Conduct](../../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] Support bthread_usleep to wait for Singleton on bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on PR #2517:
URL: https://github.com/apache/brpc/pull/2517#issuecomment-1910054663
关闭该PR,因为可能会导致线程锁里调Singleton场景出现死锁问题。
有在构造函数中挂起协程操作的需求,可以使用 #2520 的特性。
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461769853
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
或者自旋一定次数后,再调bthread_yield,这样可以自适应地防止死锁问题,不需要用户设置。因为很多时候,用户需要等到死锁了才会发现有bthread_yield的需求。可以自适应的话,可以完全规避这个问题。
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461769853
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
或者自旋一定次数后,再调bthread_yield/bthread_usleep,这样可以自适应地防止死锁问题,不需要使用者设置。因为很多时候,使用者需要等到死锁了才会发现有bthread_yield的需求。可以自适应的话,可以完全规避这个问题。
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461769853
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
或者自旋一定次数后,进入slow path(bthread_usleep 1ms),这样可以自适应地防止死锁问题,不需要使用者设置。因为很多时候,使用者需要等到死锁了才会发现有挂起协程的需求。可以自适应的话,可以完全规避这个问题。
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #2517:
URL: https://github.com/apache/brpc/pull/2517#issuecomment-1905236057
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
Re: [PR] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461997899
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
mutex/cond比bthread_usleep优雅,但是没有合适的位置初始化mutex/cond。
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461431437
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
是不是加个开关控制一下?毕竟大部分Singleton场景,是不需要bthread_yield的
--
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] Support bthread_usleep to wait for Singleton on bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright closed pull request #2517: Support bthread_usleep to wait for Singleton on bthread
URL: https://github.com/apache/brpc/pull/2517
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461474105
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
Singleton加个模板参数,控制是否使用bthread_yield,默认值为false,如何?
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461769853
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
或者自旋一定次数后,再调bthread_usleep,这样可以自适应地防止死锁问题,不需要使用者设置。因为很多时候,使用者需要等到死锁了才会发现有挂起协程的需求。可以自适应的话,可以完全规避这个问题。
--
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] Support bthread_yield to wait for Singleton in bthread (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #2517:
URL: https://github.com/apache/brpc/pull/2517#discussion_r1461769853
##########
src/butil/memory/singleton.cc:
##########
@@ -5,9 +5,23 @@
#include "butil/memory/singleton.h"
#include "butil/threading/platform_thread.h"
+__BEGIN_DECLS
+// Defined in bthread/bthread.cpp
+void BAIDU_WEAK bthread_yield();
+__END_DECLS
+
namespace butil {
namespace internal {
+void yield() {
+ if (bthread_yield) {
Review Comment:
或者自旋一定次数后,再调bthread_yield/bthread_usleep,这样可以自适应地防止死锁问题,不需要用户设置。因为很多时候,用户需要等到死锁了才会发现有bthread_yield的需求。可以自适应的话,可以完全规避这个问题。
--
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