Repository navigation
[drivers][workqueue] 延迟改立即提交时停掉旧定时器 #12059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cms19859230182-lang
wants to merge
1
commit into
RT-Thread:master
Choose a base branch
from
cms19859230182-lang:fix/workqueue-submit-stop-timer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,15 +112,48 @@ static void _workqueue_thread_entry(void *parameter) | |
| } | ||
| } | ||
|
|
||
| /* lock order: _workqueue_owner_lock first, then at most one queue->spinlock */ | ||
| static RT_DEFINE_SPINLOCK(_workqueue_owner_lock); | ||
|
|
||
| static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work); | ||
| /* same as _workqueue_cancel_work() but queue->spinlock must be held */ | ||
| static rt_err_t _workqueue_cancel_work_locked(struct rt_workqueue *queue, struct rt_work *work); | ||
| static rt_err_t _workqueue_cancel_work_ex(struct rt_workqueue *queue, struct rt_work *work, | ||
| rt_bool_t *migrated); | ||
|
|
||
| static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, | ||
| struct rt_work *work, rt_tick_t ticks) | ||
| { | ||
| rt_base_t level; | ||
| rt_base_t owner_level; | ||
| rt_err_t err = RT_EOK; | ||
| struct rt_work *work_tmp; | ||
| rt_list_t *list_tmp; | ||
| struct rt_workqueue *owner; | ||
|
|
||
| level = rt_spin_lock_irqsave(&(queue->spinlock)); | ||
| owner_level = rt_spin_lock_irqsave(&_workqueue_owner_lock); | ||
|
|
||
| for (;;) | ||
| { | ||
| owner = work->workqueue; | ||
| if (owner != RT_NULL && owner != queue) | ||
| { | ||
| level = rt_spin_lock_irqsave(&(owner->spinlock)); | ||
| if (work->workqueue == owner) | ||
| { | ||
| _workqueue_cancel_work_locked(owner, work); | ||
| } | ||
| rt_spin_unlock_irqrestore(&(owner->spinlock), level); | ||
| } | ||
|
|
||
| level = rt_spin_lock_irqsave(&(queue->spinlock)); | ||
| if (work->workqueue == RT_NULL || work->workqueue == queue) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| rt_spin_unlock_irqrestore(&(queue->spinlock), level); | ||
| } | ||
| /* remove list */ | ||
| rt_list_remove(&(work->list)); | ||
| work->flags = 0; | ||
|
|
@@ -162,23 +195,62 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, | |
| err = -RT_ERROR; | ||
| } | ||
| rt_spin_unlock_irqrestore(&(queue->spinlock), level); | ||
| rt_spin_unlock_irqrestore(&_workqueue_owner_lock, owner_level); | ||
|
|
||
| return err; | ||
| } | ||
|
|
||
| static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) | ||
| static rt_err_t _workqueue_cancel_work_locked(struct rt_workqueue *queue, struct rt_work *work) | ||
| { | ||
| rt_base_t level; | ||
| rt_err_t err; | ||
| rt_err_t err; | ||
|
|
||
| level = rt_spin_lock_irqsave(&(queue->spinlock)); | ||
| rt_list_remove(&(work->list)); | ||
| work->flags = 0; | ||
| err = queue->work_current != work ? RT_EOK : -RT_EBUSY; | ||
| work->flags = 0; | ||
| err = queue->work_current != work ? RT_EOK : -RT_EBUSY; | ||
| work->workqueue = RT_NULL; | ||
| return err; | ||
| } | ||
|
|
||
| static rt_err_t _workqueue_cancel_work_ex(struct rt_workqueue *queue, struct rt_work *work, | ||
| rt_bool_t *migrated) | ||
| { | ||
| rt_base_t level; | ||
| rt_base_t owner_level; | ||
| rt_err_t err; | ||
|
|
||
| if (migrated != RT_NULL) | ||
| { | ||
| *migrated = RT_FALSE; | ||
| } | ||
|
|
||
| owner_level = rt_spin_lock_irqsave(&_workqueue_owner_lock); | ||
| level = rt_spin_lock_irqsave(&(queue->spinlock)); | ||
|
|
||
| if ((work->workqueue != RT_NULL) && (work->workqueue != queue)) | ||
| { | ||
| /* the list node belongs to another queue, its lock is not held here */ | ||
| err = -RT_EBUSY; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 工作项移到另一队列后,这里会返回
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okok |
||
| if (migrated != RT_NULL) | ||
| { | ||
| *migrated = RT_TRUE; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| err = _workqueue_cancel_work_locked(queue, work); | ||
| } | ||
|
|
||
| rt_spin_unlock_irqrestore(&(queue->spinlock), level); | ||
| rt_spin_unlock_irqrestore(&_workqueue_owner_lock, owner_level); | ||
|
|
||
| return err; | ||
| } | ||
|
|
||
| static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) | ||
| { | ||
| return _workqueue_cancel_work_ex(queue, work, RT_NULL); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Initialize a work item, binding with a callback function. | ||
| * | ||
|
|
@@ -315,17 +387,34 @@ rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *wo | |
| rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *work) | ||
| { | ||
| rt_base_t level; | ||
| rt_base_t owner_level; | ||
| struct rt_workqueue *owner; | ||
|
|
||
| RT_ASSERT(queue != RT_NULL); | ||
| RT_ASSERT(work != RT_NULL); | ||
|
|
||
| owner_level = rt_spin_lock_irqsave(&_workqueue_owner_lock); | ||
|
|
||
| owner = work->workqueue; | ||
| if (owner != RT_NULL && owner != queue) | ||
| { | ||
| level = rt_spin_lock_irqsave(&(owner->spinlock)); | ||
| if (work->workqueue == owner) | ||
| { | ||
| _workqueue_cancel_work_locked(owner, work); | ||
| } | ||
| rt_spin_unlock_irqrestore(&(owner->spinlock), level); | ||
| } | ||
|
|
||
| level = rt_spin_lock_irqsave(&(queue->spinlock)); | ||
| /* NOTE: the work MUST be initialized firstly */ | ||
| rt_list_remove(&(work->list)); | ||
| rt_list_insert_after(&queue->work_list, &(work->list)); | ||
| work->workqueue = queue; | ||
|
|
||
| rt_completion_done(&(queue->wakeup_completion)); | ||
| rt_spin_unlock_irqrestore(&(queue->spinlock), level); | ||
| rt_spin_unlock_irqrestore(&_workqueue_owner_lock, owner_level); | ||
|
|
||
| return RT_EOK; | ||
| } | ||
|
|
@@ -338,7 +427,8 @@ rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *wo | |
| * @param work is a pointer to the work item object. | ||
| * | ||
| * @return RT_EOK Success. | ||
| * -RT_EBUSY This work item is executing. | ||
| * -RT_EBUSY This work item is executing, or it is owned by another | ||
| * work queue and was therefore not cancelled. | ||
| */ | ||
| rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) | ||
| { | ||
|
|
@@ -356,24 +446,38 @@ rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *wo | |
| * @param work is a pointer to the work item object. | ||
| * | ||
| * @return RT_EOK Success. | ||
| * -RT_EBUSY This work item is owned by another work queue, so it was | ||
| * not cancelled and may still run. | ||
| */ | ||
| rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work) | ||
| { | ||
| rt_err_t err; | ||
| rt_bool_t migrated; | ||
|
|
||
| RT_ASSERT(queue != RT_NULL); | ||
| RT_ASSERT(work != RT_NULL); | ||
|
|
||
| if (queue->work_current == work) /* it's current work in the queue */ | ||
| { | ||
| /* wait for work completion */ | ||
| rt_sem_take(&(queue->sem), RT_WAITING_FOREVER); | ||
| /* Note that because work items are automatically deleted after execution, they do not need to be deleted again */ | ||
| } | ||
| else | ||
| for (;;) | ||
| { | ||
| _workqueue_cancel_work(queue, work); | ||
| } | ||
| if (queue->work_current == work) /* it's current work in the queue */ | ||
| { | ||
| /* wait for work completion */ | ||
| rt_sem_take(&(queue->sem), RT_WAITING_FOREVER); | ||
| /* Note that because work items are automatically deleted after execution, they do not need to be deleted again */ | ||
| return RT_EOK; | ||
| } | ||
|
|
||
| return RT_EOK; | ||
| err = _workqueue_cancel_work_ex(queue, work, &migrated); | ||
| if (err != -RT_EBUSY) | ||
| { | ||
| return err; | ||
| } | ||
|
|
||
| if (migrated) | ||
| { | ||
| return -RT_EBUSY; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
两个线程同时把空闲的
work提交到不同队列时,都可能读到空的owner。后提交者会只持本队列锁摘除另一队列的节点。请串行化同一工作项的归属变更,再操作所属队列的链表。