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:
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user