wwbmmm commented on code in PR #2086:
URL: https://github.com/apache/brpc/pull/2086#discussion_r1083705058
##########
src/bthread/butex.cpp:
##########
@@ -137,27 +137,45 @@ static void wakeup_pthread(ButexPthreadWaiter* pw) {
bool erase_from_butex(ButexWaiter*, bool, WaiterState);
-int wait_pthread(ButexPthreadWaiter& pw, timespec* ptimeout) {
+int wait_pthread(ButexPthreadWaiter& pw, const timespec* abstime) {
+ timespec * ptimeout = NULL;
+ timespec timeout;
+ int64_t timeout_us = 0;
+ int rc;
+
while (true) {
- const int rc = futex_wait_private(&pw.sig, PTHREAD_NOT_SIGNALLED,
ptimeout);
- if (PTHREAD_NOT_SIGNALLED != pw.sig.load(butil::memory_order_acquire))
{
- // If `sig' is changed, wakeup_pthread() must be called and `pw'
- // is already removed from the butex.
- // Acquire fence makes this thread sees changes before wakeup.
- return rc;
+ if (abstime != NULL) {
+ timeout_us = butil::timespec_to_microseconds(*abstime) -
butil::gettimeofday_us();
+ timeout = butil::microseconds_to_timespec(timeout_us);
+ ptimeout = &timeout;
}
- if (rc != 0 && errno == ETIMEDOUT) {
- // Note that we don't handle the EINTR from futex_wait here since
- // pthreads waiting on a butex should behave similarly as bthreads
- // which are not able to be woken-up by signals.
- // EINTR on butex is only producible by TaskGroup::interrupt().
-
- // `pw' is still in the queue, remove it.
+ if (timeout_us > MIN_SLEEP_US || abstime == NULL) {
+ rc = futex_wait_private(&pw.sig, PTHREAD_NOT_SIGNALLED, ptimeout);
+ if (PTHREAD_NOT_SIGNALLED !=
pw.sig.load(butil::memory_order_acquire)) {
+ // If `sig' is changed, wakeup_pthread() must be called and
`pw'
+ // is already removed from the butex.
+ // Acquire fence makes this thread sees changes before wakeup.
+ return rc;
+ }
+ }
+ else {
+ errno = ETIMEDOUT;
+ rc = -1;
+ }
+ // Handle EINTR or ETIMEDOUT when abstime is valid
+ if (rc != 0 && abstime != NULL) {
+ // Handle the EINTR from futex_wait
+ if (errno != ETIMEDOUT) {
Review Comment:
可以把上一个判断恢复成rc != 0 && errno == ETIMEDOUT。这里就不用再判断了。
##########
src/bthread/butex.cpp:
##########
@@ -137,27 +137,45 @@ static void wakeup_pthread(ButexPthreadWaiter* pw) {
bool erase_from_butex(ButexWaiter*, bool, WaiterState);
-int wait_pthread(ButexPthreadWaiter& pw, timespec* ptimeout) {
+int wait_pthread(ButexPthreadWaiter& pw, const timespec* abstime) {
+ timespec * ptimeout = NULL;
+ timespec timeout;
+ int64_t timeout_us = 0;
+ int rc;
+
while (true) {
- const int rc = futex_wait_private(&pw.sig, PTHREAD_NOT_SIGNALLED,
ptimeout);
- if (PTHREAD_NOT_SIGNALLED != pw.sig.load(butil::memory_order_acquire))
{
- // If `sig' is changed, wakeup_pthread() must be called and `pw'
- // is already removed from the butex.
- // Acquire fence makes this thread sees changes before wakeup.
- return rc;
+ if (abstime != NULL) {
+ timeout_us = butil::timespec_to_microseconds(*abstime) -
butil::gettimeofday_us();
+ timeout = butil::microseconds_to_timespec(timeout_us);
+ ptimeout = &timeout;
}
- if (rc != 0 && errno == ETIMEDOUT) {
- // Note that we don't handle the EINTR from futex_wait here since
- // pthreads waiting on a butex should behave similarly as bthreads
- // which are not able to be woken-up by signals.
- // EINTR on butex is only producible by TaskGroup::interrupt().
-
- // `pw' is still in the queue, remove it.
+ if (timeout_us > MIN_SLEEP_US || abstime == NULL) {
+ rc = futex_wait_private(&pw.sig, PTHREAD_NOT_SIGNALLED, ptimeout);
+ if (PTHREAD_NOT_SIGNALLED !=
pw.sig.load(butil::memory_order_acquire)) {
+ // If `sig' is changed, wakeup_pthread() must be called and
`pw'
+ // is already removed from the butex.
+ // Acquire fence makes this thread sees changes before wakeup.
+ return rc;
+ }
+ }
+ else {
Review Comment:
else不用换行
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]