Conversation
spholz
left a comment
There was a problem hiding this comment.
Thank you very much for working on this!
Hendiadyoin1
left a comment
There was a problem hiding this comment.
@spholz reading through this, I am not sure if this works towards solving the issue we have in the xhci code, whats your opinion on that?
Also quite nice getting this out of the less linear to read blocking code
2761932 to
6646950
Compare
|
Took a break, and recalled that the original article suggested using linked lists. I've replaced the Vector with a linked list-based approach, so that we're fully allocation-free (whereas previously the Vector's inline capacity would run out at some point). This is actually pretty similar to the |
Why wouldn't it help for xHCI? We would just use a boolean flag similar to the FinalizerTask code in this PR. Alternatively, we could maybe even check if the event ring itself has events for
I'm actually kind of hoping that we can maybe replace some Blockers with |
6a98f14 to
eeccb0e
Compare
eeccb0e to
d5659a6
Compare
f73d4c0 to
4f4149d
Compare
|
I guess this is ready for review again. I still had to add some extra thread functions so that we can tell if a signal dispatch occurred, and return EINTR if it did (since the signal will already be gone when we check this). Internal kernel threads can probably just use I wrote a patch that allows interacting with --- a/Kernel/API/Syscall.h
+++ b/Kernel/API/Syscall.h
@@ -52,6 +52,7 @@ enum class NeedsBigProcessLock {
S(annotate_mapping, NeedsBigProcessLock::No) \
S(bind, NeedsBigProcessLock::No) \
S(bindmount, NeedsBigProcessLock::No) \
+ S(block, NeedsBigProcessLock::No) \
S(chdir, NeedsBigProcessLock::No) \
S(chmod, NeedsBigProcessLock::No) \
S(chown, NeedsBigProcessLock::No) \
--- a/Kernel/CMakeLists.txt
+++ b/Kernel/CMakeLists.txt
@@ -302,6 +302,7 @@ set(KERNEL_SOURCES
Security/UBSanitizer.cpp
Syscalls/anon_create.cpp
Syscalls/alarm.cpp
+ Syscalls/block.cpp
Syscalls/chdir.cpp
Syscalls/chmod.cpp
Syscalls/chown.cpp
--- /dev/null
+++ b/Kernel/Syscalls/block.cpp
@@ -0,0 +1,12 @@
+#include <Kernel/Tasks/Process.h>
+#include <Kernel/Tasks/WaitQueue.h>
+
+ErrorOr<FlatPtr> Kernel::Process::sys$block()
+{
+ WaitQueue queue;
+ Spinlock<LockRank::None> lock;
+ (void)queue.wait_until(lock, []() -> bool {
+ return false;
+ });
+ return 0;
+}
--- a/Kernel/Tasks/Process.h
+++ b/Kernel/Tasks/Process.h
@@ -351,6 +351,7 @@ public:
ErrorOr<FlatPtr> sys$yield();
ErrorOr<FlatPtr> sys$sync();
ErrorOr<FlatPtr> sys$beep(int tone);
+ ErrorOr<FlatPtr> sys$block();
ErrorOr<FlatPtr> sys$create_inode_watcher(u32 flags);
ErrorOr<FlatPtr> sys$inode_watcher_add_watch(Userspace<Syscall::SC_inode_watcher_add_watch_params const*> user_params);
ErrorOr<FlatPtr> sys$inode_watcher_remove_watch(int fd, int wd);
--- a/Userland/Utilities/CMakeLists.txt
+++ b/Userland/Utilities/CMakeLists.txt
@@ -10,6 +10,7 @@ set(CMD_SOURCES_CPP
base64.cpp
basename.cpp
beep.cpp
+ block.cpp
blockdev.cpp
bt.cpp
cal.cpp
@@ -302,6 +303,7 @@ target_link_libraries(aconv PRIVATE LibAudio LibFileSystem)
target_link_libraries(animation PRIVATE LibGfx)
target_link_libraries(aplay PRIVATE LibAudio LibFileSystem LibIPC)
target_link_libraries(asctl PRIVATE LibAudio LibIPC)
+target_link_libraries(block PRIVATE LibSystem)
target_link_libraries(bt PRIVATE LibSymbolication LibURL)
target_link_libraries(checksum PRIVATE LibCrypto)
target_link_libraries(chres PRIVATE LibGUI LibIPC)
--- /dev/null
+++ b/Userland/Utilities/block.cpp
@@ -0,0 +1,8 @@
+#include <LibMain/Main.h>
+#include <syscall.h>
+
+ErrorOr<int> serenity_main(Main::Arguments)
+{
+ syscall(SC_block);
+ return 0;
+} |
4f4149d to
1a2dec0
Compare
| bool m_is_crashing { false }; | ||
| bool m_is_promise_violation_pending { false }; | ||
| Atomic<bool> m_have_any_unmasked_pending_signals { false }; | ||
| Atomic<bool> m_was_interrupted { false }; |
There was a problem hiding this comment.
When setting this variable you use seq_cst (the default memory order) and when reading, consume.
Consume is a weird memory order and I wouldn't recommend you use it, especially since it's scheduled for deprecation in C++26.
Using seq_cst is a bit eh, but I'm also not quite sure what memory order we would need here.
There was a problem hiding this comment.
I guess we might as well use seq_cst here as well then (though I guess acquire would have the same result). has_unmasked_pending_signals() also uses consume, but I'm not sure why.
One thing that just having a bool for this causes is that a SIGSTOP / SIGCONT sequence (or even just a SIGCONT to a running process) will lead to this being interrupted, but that seems to match how blockers work, so it might be better to fix this globally at some later point.
There was a problem hiding this comment.
It'd be nice to get rid of the seq_cst in the future, but for now it's good enough I guess.
We now also track if such a thread was interrupted at some point, which will be needed to determine if threads that are in a WaitQueue should stop blocking.
This implementation is centered around block conditions, and as a result no wakeups will be lost.
1a2dec0 to
ce62ba1
Compare
|
Thanks for reviewing this (and the thread/signal/scheduler cleanup PR). It's kind of nice that this had the side effect of making all of that slightly easier to follow. |
Part of #26108.
The old
WaitQueueis still around (it's just been renamed toDeprecatedWaitQueue) since not everything has been ported to the newWaitQueue.There's now also a
ConditionWaiterclass (can be renamed if we can think of a better name) which allows blocking on a specific (atomic) boolean in a slightly less verbose way as compared to using aWaiterdirectly.Porting to the new
WaitQueueshouldn't be too hard (there's two such ports in this PR already), though at leastKernelRngwill require special attention (not done in this PR) because it currently deadlocks when entropy exhaustion occurs.