issue#I5KOL1 Use pthread_cond_timedwait instead of pthread_cond_wait to handle

m_readySessionList when all workers are idle
This commit is contained in:
Chunling Wang
2022-07-28 18:28:43 +08:00
parent a4bdb7450b
commit 573423cac6
4 changed files with 21 additions and 4 deletions

View File

@ -380,6 +380,7 @@ void ThreadPoolWorker::WaitNextSession()
/* Return worker to pool unless we can get a task right now. */
ThreadPoolListener* lsn = m_group->GetListener();
Assert(lsn != NULL);
struct timespec ts;
while (true) {
/* Wait if the thread was turned into pending mode. */
@ -406,11 +407,14 @@ void ThreadPoolWorker::WaitNextSession()
WaitState oldStatus = pgstat_report_waitstatus(STATE_WAIT_COMM);
pthread_mutex_lock(m_mutex);
while (!m_currentSession) {
while (!m_currentSession && lsn->hasNoReadySession()) {
if (unlikely(m_threadStatus == THREAD_PENDING || m_threadStatus == THREAD_EXIT)) {
break;
}
pthread_cond_wait(m_cond, m_mutex);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 10; // 10s
ts.tv_nsec = 0;
pthread_cond_timedwait(m_cond, m_mutex, &ts);
}
pthread_mutex_unlock(m_mutex);
m_group->GetListener()->RemoveWorkerFromList(this);
@ -428,10 +432,14 @@ void ThreadPoolWorker::WaitNextSession()
void ThreadPoolWorker::Pending()
{
struct timespec ts;
pg_atomic_fetch_sub_u32((volatile uint32*)&m_group->m_workerNum, 1);
pthread_mutex_lock(m_mutex);
while (m_threadStatus == THREAD_PENDING) {
pthread_cond_wait(m_cond, m_mutex);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 10; // 10s
ts.tv_nsec = 0;
pthread_cond_timedwait(m_cond, m_mutex, &ts);
}
pthread_mutex_unlock(m_mutex);
pg_atomic_fetch_add_u32((volatile uint32*)&m_group->m_workerNum, 1);