forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 5
unix: Wake sleeping operations immediately on scheduled callbacks. #9
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
Closed
andrewleech
wants to merge
3
commits into
review/unix-sleep-process-pending
from
unix-sleep-process-pending
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,12 +30,62 @@ | |||||||||||
| #include <time.h> | ||||||||||||
| #include <sys/time.h> | ||||||||||||
| #include <fcntl.h> | ||||||||||||
| #include <signal.h> | ||||||||||||
|
|
||||||||||||
| #include "py/mphal.h" | ||||||||||||
| #include "py/mpthread.h" | ||||||||||||
| #include "py/runtime.h" | ||||||||||||
| #include "extmod/misc.h" | ||||||||||||
|
|
||||||||||||
| #ifndef _WIN32 | ||||||||||||
|
|
||||||||||||
| // Use a real-time signal if available, avoiding conflicts with GC | ||||||||||||
| // (SIGRTMIN + 5) and thread terminate (SIGRTMIN + 6). Fall back to | ||||||||||||
| // SIGURG which is ignored by default and rarely used. | ||||||||||||
| #ifdef SIGRTMIN | ||||||||||||
| #define MP_SCHED_SIGNAL (SIGRTMIN + 7) | ||||||||||||
| #else | ||||||||||||
| #define MP_SCHED_SIGNAL (SIGURG) | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| static void sched_sighandler(int signum) { | ||||||||||||
| (void)signum; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void mp_unix_init_sched_signal(void) { | ||||||||||||
| struct sigaction sa; | ||||||||||||
|
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.
Suggested change
|
||||||||||||
| sa.sa_flags = 0; // No SA_RESTART: select() returns EINTR. | ||||||||||||
| sa.sa_handler = sched_sighandler; | ||||||||||||
| sigemptyset(&sa.sa_mask); | ||||||||||||
| sigaction(MP_SCHED_SIGNAL, &sa, NULL); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void mp_unix_deinit_sched_signal(void) { | ||||||||||||
| signal(MP_SCHED_SIGNAL, SIG_DFL); | ||||||||||||
|
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. Use
Suggested change
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void mp_hal_signal_event(void) { | ||||||||||||
| kill(getpid(), MP_SCHED_SIGNAL); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Wait for an event or timeout. Returns early if callbacks are already | ||||||||||||
| // pending (checked via sched_state) or if a signal interrupts select(). | ||||||||||||
| // | ||||||||||||
| // Note: there is a narrow race on threaded builds where a signal could | ||||||||||||
| // arrive between the sched_state check and the select() call, causing | ||||||||||||
| // select() to block despite a newly-pending callback. The impact is | ||||||||||||
| // bounded -- the caller's next timeout expiry will process it. This is | ||||||||||||
| // a deliberate simplification over pselect() with process-wide signal | ||||||||||||
| // masking. | ||||||||||||
| void mp_unix_sched_sleep(uint32_t timeout_ms) { | ||||||||||||
| if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| struct timeval tv = {timeout_ms / 1000, (timeout_ms % 1000) * 1000}; | ||||||||||||
| select(0, NULL, NULL, NULL, &tv); | ||||||||||||
| } | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| #if defined(__GLIBC__) && defined(__GLIBC_PREREQ) | ||||||||||||
| #if __GLIBC_PREREQ(2, 25) | ||||||||||||
| #include <sys/random.h> | ||||||||||||
|
|
@@ -44,8 +94,6 @@ | |||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| #ifndef _WIN32 | ||||||||||||
| #include <signal.h> | ||||||||||||
|
|
||||||||||||
| static void sighandler(int signum) { | ||||||||||||
| if (signum == SIGINT) { | ||||||||||||
| #if MICROPY_ASYNC_KBD_INTR | ||||||||||||
|
|
||||||||||||
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Test time.sleep() signal-based scheduler integration on unix port. | ||
| # | ||
| # The signal-based EINTR wakeup during select() cannot be reliably tested | ||
| # from Python because kill(getpid()) delivers to an arbitrary thread, and | ||
| # the scheduling thread may receive it instead of the sleeping thread. | ||
| # Instead, test the drain loop (processes already-pending callbacks before | ||
| # sleeping) and ValueError for negative values. | ||
|
|
||
| import micropython | ||
| import time | ||
|
|
||
| # Test 1: ValueError for negative sleep. | ||
| try: | ||
| time.sleep(-1) | ||
| print("FAIL: no ValueError") | ||
| except ValueError: | ||
| print("ValueError") | ||
|
|
||
| # Test 2: Pending callbacks are processed during time.sleep(). | ||
| # Schedule a chain of callbacks from within a callback so they accumulate | ||
| # while the scheduler is locked. time.sleep()'s drain loop should process | ||
| # them all before entering select(). | ||
| results = [] | ||
|
|
||
|
|
||
| def callback(n): | ||
| results.append(n) | ||
| if n < 3: | ||
| micropython.schedule(callback, n + 1) | ||
|
|
||
|
|
||
| micropython.schedule(callback, 0) | ||
|
|
||
| # Small sleep to allow drain loop to process the chain. | ||
| time.sleep(0.01) | ||
|
|
||
| # All callbacks in the chain should have been processed. | ||
| print("callbacks:", sorted(results)) | ||
|
|
||
| # Test 3: Basic sleep timing still works (not broken by signal changes). | ||
| start = time.ticks_ms() | ||
| time.sleep(0.05) | ||
| elapsed = time.ticks_diff(time.ticks_ms(), start) | ||
| # Should be at least 40ms (allowing for timing imprecision) and under 500ms. | ||
| if 40 <= elapsed < 500: | ||
| print("timing ok") | ||
| else: | ||
| print("timing FAIL:", elapsed, "ms") |
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ValueError | ||
| callbacks: [0, 1, 2, 3] | ||
| timing ok |
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.
"select()" → "pselect()".