-
Notifications
You must be signed in to change notification settings - Fork 8
fix(cmsis_rtthread): 防止事件标志设置在未保护状态下更改 #12
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
wdfk-prog
wants to merge
1
commit into
RT-Thread-packages:master
Choose a base branch
from
wdfk-prog:event
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.
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
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.
🧩 Analysis chain
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 92
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 1705
🌐 Web query:
CMSIS-RTOS2 osEventFlagsSet specification return value requirements💡 Result:
Per the CMSIS‑RTOS2 specification,
osEventFlagsSet(ef_id, flags)shall return:osFlagsErrorParameter(invalidef_idor ifflagshas the highest bit set),osFlagsErrorResource,osFlagsErrorUnknown,osFlagsErrorSafetyClass, etc. [1]Implication: the input
flagsvalue must not set bit 31, because that bit is reserved to indicate an error return. [1]Source: CMSIS‑RTOS2 Event Flags API documentation. [1]
References
[1] ARM CMSIS‑RTOS2 “Event Flags” (
osEventFlagsSet) documentation, CMSIS v6: (arm-software.github.io)Citations:
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 1012
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 752
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 563
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 56
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 182
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 330
🏁 Script executed:
Repository: RT-Thread-packages/CMSIS_RTOS2
Length of output: 1011
🌐 Web query:
RT-Thread rt_event_send behavior and atomicity - does it modify rt_event->set field💡 Result:
Yes.
rt_event_send(event, set)does modify the event object’sevent->setfield: it ORs in the bits you send (event->set |= set). While scanning the suspend list, if a waiting thread’s receive options includeRT_EVENT_FLAG_CLEAR, the implementation may also clear the corresponding bits fromevent->set(event->set &= ~thread->event_set) when the condition is satisfied and the thread is resumed. [1]Atomicity / concurrency behavior:
event->setand the walk of the waiting-thread list are done inside a critical section created by disabling interrupts (rt_hw_interrupt_disable()…rt_hw_interrupt_enable()), so on a typical single-core RT-Thread system this makes the operation atomic w.r.t. ISRs and preempting threads. [1][2]rt_event_send()as “setting the event flag value of the event set object” according to thesetparameter, then checking the wait list to wake matching threads—i.e., it is meant to mutate the event flags in the object. [3]Implication: events are level-triggered / non-queued: sending the same bit multiple times before it’s cleared has the same effect as sending once (the bit is just already set). [4]
Sources
[1] RT-Thread
rt_event_sendimplementation (showsevent->set |= setunder interrupt disable, and conditional clear) — https://www.cnblogs.com/kehuadong/p/13923485.html[2] RT-Thread interrupt lock description (
rt_hw_interrupt_disable/enablecritical section semantics) — https://www.rt-thread.io/document/site/programming-manual/interrupt/interrupt/[3] RT-Thread docs:
rt_event_send()sets event flags then traverses suspend list to wake threads — https://rt-thread.github.io/rt-thread/page_thread_sync.html[4] RT-Thread docs: events are not queuing (“sending the same event…effect is equivalent to sending only once”) — https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/
Return value can be stale if flags change between read and send.
Per CMSIS-RTOS2 specification,
osEventFlagsSetmust return the event flags value after setting. The current implementation at line 1370 computesset_flagsbefore callingrt_event_send(), which creates a race condition: another thread may modify flags between the read and the send operation, causing the returned value to not reflect the actual flags stored after the operation.Since
rt_event_send()performsevent->set |= flagsinside its own critical section but the pre-read at line 1370 is unprotected, the computed value can become stale. Readevent_cb->event.setafterrt_event_send()under a critical section (usingrt_hw_interrupt_disable/enable()) to ensure the return value matches the actual stored flags—consistent with the pattern already used inosEventFlagsClear().🔧 Suggested adjustment
📝 Committable suggestion
🤖 Prompt for AI Agents