Pārlūkot izejas kodu

Merge pull request #1193 from ustccy/hotfix_epollwait_about_to_quit

add flag BTHREAD_NEVER_QUIT
Zhangyi Chen 2 gadi atpakaļ
vecāks
revīzija
60f2a47c72

+ 5 - 1
src/brpc/event_dispatcher.cpp

@@ -105,13 +105,17 @@ int EventDispatcher::Start(const bthread_attr_t* consumer_thread_attr) {
     _consumer_thread_attr = (consumer_thread_attr  ?
                              *consumer_thread_attr : BTHREAD_ATTR_NORMAL);
 
+    //_consumer_thread_attr is used in StartInputEvent(), assign flag NEVER_QUIT to it will cause new bthread
+    // that created by epoll_wait() never to quit.
+    _epoll_thread_attr = _consumer_thread_attr | BTHREAD_NEVER_QUIT;
+
     // Polling thread uses the same attr for consumer threads (NORMAL right
     // now). Previously, we used small stack (32KB) which may be overflowed
     // when the older comlog (e.g. 3.1.85) calls com_openlog_r(). Since this
     // is also a potential issue for consumer threads, using the same attr
     // should be a reasonable solution.
     int rc = bthread_start_background(
-        &_tid, &_consumer_thread_attr, RunThis, this);
+        &_tid, &_epoll_thread_attr, RunThis, this);
     if (rc) {
         LOG(FATAL) << "Fail to create epoll/kqueue thread: " << berror(rc);
         return -1;

+ 3 - 0
src/brpc/event_dispatcher.h

@@ -94,6 +94,9 @@ private:
     // The attribute of bthreads calling user callbacks.
     bthread_attr_t _consumer_thread_attr;
 
+    // The attribute of bthread epoll_wait.
+    bthread_attr_t _epoll_thread_attr;
+
     // Pipe fds to wakeup EventDispatcher from `epoll_wait' in order to quit
     int _wakeup_fds[2];
 };

+ 4 - 1
src/bthread/bthread.cpp

@@ -316,7 +316,10 @@ int bthread_setconcurrency(int num) {
 int bthread_about_to_quit() {
     bthread::TaskGroup* g = bthread::tls_task_group;
     if (g != NULL) {
-        g->current_task()->about_to_quit = true;
+        bthread::TaskMeta* current_task = g->current_task();
+        if(!(current_task->attr.flags & BTHREAD_NEVER_QUIT)) {
+            current_task->about_to_quit = true;
+        }
         return 0;
     }
     return EPERM;

+ 1 - 0
src/bthread/types.h

@@ -45,6 +45,7 @@ typedef unsigned bthread_attrflags_t;
 static const bthread_attrflags_t BTHREAD_LOG_START_AND_FINISH = 8;
 static const bthread_attrflags_t BTHREAD_LOG_CONTEXT_SWITCH = 16;
 static const bthread_attrflags_t BTHREAD_NOSIGNAL = 32;
+static const bthread_attrflags_t BTHREAD_NEVER_QUIT = 64;
 
 // Key of thread-local data, created by bthread_key_create.
 typedef struct {