mirror of
https://github.com/torvalds/linux.git
synced 2024-11-10 14:11:52 +00:00
workqueue: destroy worker directly in the idle timeout handler
Since destroy_worker() doesn't need to sleep nor require manager_mutex, destroy_worker() can be directly called in the idle timeout handler, it helps us remove POOL_MANAGE_WORKERS and maybe_destroy_worker() and simplify the manage_workers() After POOL_MANAGE_WORKERS is removed, worker_thread() doesn't need to test whether it needs to manage after processed works. So we can remove the test branch. Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
This commit is contained in:
parent
60f5a4bcf8
commit
3347fc9f36
@ -68,7 +68,6 @@ enum {
|
|||||||
* manager_mutex to avoid changing binding state while
|
* manager_mutex to avoid changing binding state while
|
||||||
* create_worker() is in progress.
|
* create_worker() is in progress.
|
||||||
*/
|
*/
|
||||||
POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
|
|
||||||
POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
|
POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
|
||||||
POOL_FREEZING = 1 << 3, /* freeze in progress */
|
POOL_FREEZING = 1 << 3, /* freeze in progress */
|
||||||
|
|
||||||
@ -752,13 +751,6 @@ static bool need_to_create_worker(struct worker_pool *pool)
|
|||||||
return need_more_worker(pool) && !may_start_working(pool);
|
return need_more_worker(pool) && !may_start_working(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do I need to be the manager? */
|
|
||||||
static bool need_to_manage_workers(struct worker_pool *pool)
|
|
||||||
{
|
|
||||||
return need_to_create_worker(pool) ||
|
|
||||||
(pool->flags & POOL_MANAGE_WORKERS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Do we have too many workers and should some go away? */
|
/* Do we have too many workers and should some go away? */
|
||||||
static bool too_many_workers(struct worker_pool *pool)
|
static bool too_many_workers(struct worker_pool *pool)
|
||||||
{
|
{
|
||||||
@ -1868,7 +1860,7 @@ static void idle_worker_timeout(unsigned long __pool)
|
|||||||
|
|
||||||
spin_lock_irq(&pool->lock);
|
spin_lock_irq(&pool->lock);
|
||||||
|
|
||||||
if (too_many_workers(pool)) {
|
while (too_many_workers(pool)) {
|
||||||
struct worker *worker;
|
struct worker *worker;
|
||||||
unsigned long expires;
|
unsigned long expires;
|
||||||
|
|
||||||
@ -1876,13 +1868,12 @@ static void idle_worker_timeout(unsigned long __pool)
|
|||||||
worker = list_entry(pool->idle_list.prev, struct worker, entry);
|
worker = list_entry(pool->idle_list.prev, struct worker, entry);
|
||||||
expires = worker->last_active + IDLE_WORKER_TIMEOUT;
|
expires = worker->last_active + IDLE_WORKER_TIMEOUT;
|
||||||
|
|
||||||
if (time_before(jiffies, expires))
|
if (time_before(jiffies, expires)) {
|
||||||
mod_timer(&pool->idle_timer, expires);
|
mod_timer(&pool->idle_timer, expires);
|
||||||
else {
|
break;
|
||||||
/* it's been idle for too long, wake up manager */
|
|
||||||
pool->flags |= POOL_MANAGE_WORKERS;
|
|
||||||
wake_up_worker(pool);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
destroy_worker(worker);
|
||||||
}
|
}
|
||||||
|
|
||||||
spin_unlock_irq(&pool->lock);
|
spin_unlock_irq(&pool->lock);
|
||||||
@ -2000,44 +1991,6 @@ restart:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* maybe_destroy_worker - destroy workers which have been idle for a while
|
|
||||||
* @pool: pool to destroy workers for
|
|
||||||
*
|
|
||||||
* Destroy @pool workers which have been idle for longer than
|
|
||||||
* IDLE_WORKER_TIMEOUT.
|
|
||||||
*
|
|
||||||
* LOCKING:
|
|
||||||
* spin_lock_irq(pool->lock) which may be released and regrabbed
|
|
||||||
* multiple times. Called only from manager.
|
|
||||||
*
|
|
||||||
* Return:
|
|
||||||
* %false if no action was taken and pool->lock stayed locked, %true
|
|
||||||
* otherwise.
|
|
||||||
*/
|
|
||||||
static bool maybe_destroy_workers(struct worker_pool *pool)
|
|
||||||
{
|
|
||||||
bool ret = false;
|
|
||||||
|
|
||||||
while (too_many_workers(pool)) {
|
|
||||||
struct worker *worker;
|
|
||||||
unsigned long expires;
|
|
||||||
|
|
||||||
worker = list_entry(pool->idle_list.prev, struct worker, entry);
|
|
||||||
expires = worker->last_active + IDLE_WORKER_TIMEOUT;
|
|
||||||
|
|
||||||
if (time_before(jiffies, expires)) {
|
|
||||||
mod_timer(&pool->idle_timer, expires);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
destroy_worker(worker);
|
|
||||||
ret = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* manage_workers - manage worker pool
|
* manage_workers - manage worker pool
|
||||||
* @worker: self
|
* @worker: self
|
||||||
@ -2101,13 +2054,6 @@ static bool manage_workers(struct worker *worker)
|
|||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pool->flags &= ~POOL_MANAGE_WORKERS;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Destroy and then create so that may_start_working() is true
|
|
||||||
* on return.
|
|
||||||
*/
|
|
||||||
ret |= maybe_destroy_workers(pool);
|
|
||||||
ret |= maybe_create_worker(pool);
|
ret |= maybe_create_worker(pool);
|
||||||
|
|
||||||
mutex_unlock(&pool->manager_mutex);
|
mutex_unlock(&pool->manager_mutex);
|
||||||
@ -2349,9 +2295,6 @@ recheck:
|
|||||||
|
|
||||||
worker_set_flags(worker, WORKER_PREP, false);
|
worker_set_flags(worker, WORKER_PREP, false);
|
||||||
sleep:
|
sleep:
|
||||||
if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
|
|
||||||
goto recheck;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* pool->lock is held and there's no work to process and no need to
|
* pool->lock is held and there's no work to process and no need to
|
||||||
* manage, sleep. Workers are woken up only while holding
|
* manage, sleep. Workers are woken up only while holding
|
||||||
|
Loading…
Reference in New Issue
Block a user